我正在設計一個簡單的GUI(在Eclipse中使用WindowBuilder Pro),該按鈕只在按下按鈕(測試)後在textArea
之後顯示「Hello World」。按鈕不會顯示文本區域中的文本
然而,當我按下按鈕,它不會在文本區域顯示出來!有人可以調整代碼或至少告訴我該怎麼做?
public class TextA {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TextA window = new TextA();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public TextA() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JTextArea textArea = new JTextArea();
textArea.setBounds(113, 44, 226, 96);
frame.getContentPane().add(textArea);
JButton btnTesting = new JButton("Testing");
btnTesting.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JTextArea textArea = new JTextArea();
textArea.setText("Hello World!");
}
});
btnTesting.setBounds(168, 167, 117, 29);
frame.getContentPane().add(btnTesting);
}
}
您正在動作偵聽器內部創建一個新的'JTextArea',並在其上設置文本。這與您添加到「JFrame」中的文本區域不同。 – crush
Java GUI可能需要在多種平臺上工作,使用不同的屏幕分辨率並使用不同的PLAF。因此,它們不利於組件的準確放置。爲了組織強大的圖形用戶界面,請使用佈局管理器或[它們的組合](http://stackoverflow.com/a/5630271/418556)以及[空格]的佈局填充和邊框(http: //stackoverflow.com/q/17874717/418556)。 –
或者,使用JavaFX,您可以製作更豐富,更精確的GUI。 – SnakeDoc