我正在设计一个简单的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