2016-07-27 40 views
0

除按鈕之外的所有東西都顯示在窗口中。有我失蹤的東西嗎? 這是第一次使用按鈕,我不知道發生了什麼問題。這可能是一個格式問題。 有人可以告訴我,如果setLocation()和setSize()有問題嗎?按鈕沒有出現在我的Jframe中

import java.awt.BorderLayout; 
import java.awt.Button; 
import java.awt.FlowLayout; 
import java.awt.Font; 
import java.awt.TextField; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 

public class HashString extends JPanel { 


    public static void hashString() { 
    } 

    public void window() { 
    JLabel label1 = new JLabel(
      "Enter Your Strings separated by a comma, below. "); 
    label1.setHorizontalAlignment(JLabel.CENTER); 
    label1.setFont(new Font("Times New Roman", Font.BOLD, 12)); 
    label1.setVerticalAlignment(JLabel.TOP); 

    JTextField field = new JTextField(50); 
    field.setVisible(true); 
    field.setText("Enter Strings Here"); 
    field.setSize(300, 251); 
    field.setHorizontalAlignment(JTextField.CENTER); 
    field.setLocation(135, 60); 

    Button btn = new Button("Enter These Values"); 
    btn.setLocation(240 ,420); 
    btn.setSize(100, 100); 
    btn.setVisible(true); 
    btn.setFont(new Font("Times new roman",Font.BOLD,12)); 

    JFrame frame = new JFrame("Test1"); 
    frame.add(new HashString()); 
    frame.add(btn); 
    frame.setVisible(true); 
    frame.add(field); 
    frame.setLocationRelativeTo(null); 
    frame.add(label1); 
    frame.setSize(600, 450); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

} 

}

+0

這是,shoudlve在最後添加了setVisible。 –

回答

2

我想你應該改變你是如何構成的程序。不要將每個職位都放在JFrame內部的手動位置,而應該簡單地使用佈局管理器,如BorderLayout,這可以很容易地在這裏實現。

此外,您應該始終處理從EventQueue而不是任何其他線程的一切。此外,混合和匹配AWT組件如Button和Swing組件如JButton是不可取的。雖然它比以前好得多 - 比如在Java 1.6中 - 但它仍然會出現一些問題。

import java.awt.BorderLayout; 
import java.awt.EventQueue; 
import java.awt.Font; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 
import javax.swing.border.EmptyBorder; 

public class HashString { // It seems that this isn't a JPanel. Rather, it is an application. 

    JFrame frame; 

    public void initialise() { 

     frame = new JFrame("Test 1"); // You can create the frame of the application here and set title 

     frame.setLocation(200, 200); 
     frame.setSize(300, 300); 

     JPanel contentPanel = new JPanel();  // To allow a border to be set, I've declared a content panel inside the 
               // frame. 
     contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); // This sets a border to make everything look nice 
     contentPanel.setLayout(new BorderLayout(5, 5)); // This creates the BorderLayout, which manages the layout of 
                 // the components easily 
     frame.setContentPane(contentPanel); 

     JLabel instructionsLabel = new JLabel("Enter Your Strings separated by a comma, below. "); 
     instructionsLabel.setFont(new Font("Times New Roman", Font.BOLD, 12)); 
     contentPanel.add(instructionsLabel, BorderLayout.NORTH); // BorderLayout.NORTH tells the layout manager where 
                    // to put the component. 

     JTextField txtField = new JTextField(); 
     txtField.setText("Enter Strings Here"); 
     contentPanel.add(txtField, BorderLayout.CENTER); 

     JButton btn = new JButton("Enter These Values"); 
     btn.setFont(new Font("Times new roman", Font.BOLD, 12)); 
     btn.addActionListener(new ActionListener() { 
      @Override public void actionPerformed(ActionEvent e) { 
       // Call whatever method that you want to call when this is relevant. Set textField and other variables 
       // here. You can do things like 'txtField.setText(methodOperationOnString (txtField.getText()))' or 
       // something of the like. 
      } 
     }); 
     contentPanel.add(btn, BorderLayout.SOUTH); 

     frame.setVisible(true); 

    } 

    public static void main(String[] args) { 
     // This tells it to create the entire thing on the GUI thread 
     EventQueue.invokeLater(new Runnable() { 
      @Override public void run() { 
       HashString b = new HashString(); 
       b.initialise(); 
      } 
     }); 
    } 
} 
+1

哇!這真的很有幫助!我習慣於手動完成所有工作 –

+0

如果你的主要方法是在另一個類中 - 把'public void initialise()'改爲'public HashString()'。然後當你調用'new HashString()'時它會自動構造。如果是的話,你也可以刪除'main'。 – ifly6

+0

不要混合使用JavaFx和Swing。這是一個Swing應用程序。不要試圖從這裏初始化它。如果你這樣做了(並且你在Eclipse中),你需要刪除運行配置。單擊「播放」按鈕>「運行配置」>「HashString」旁邊的箭頭,然後單擊該運行配置中的刪除。 – ifly6