[英]JPanel won't display JTextField
I am trying to make a GUI for a simple program. 我正在尝试制作一个简单程序的GUI。 I simply want a text field so that a user can input some text, but the JTextField will not display in my JPanel, even though both my buttons and labels disply with no problems.
我只是想要一个文本字段,以便用户可以输入一些文本,但是即使我的按钮和标签都没有问题,JTextField也不会显示在我的JPanel中。 any help on how to fix this problem much appreciated.
任何有关如何解决此问题的帮助,不胜感激。 Here is my code.
这是我的代码。
public static void main(String[] args) {
JFrame frame = new JFrame("David's cube program!");
frame.setVisible(true);
frame.setSize(800,350); // i used some code from
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(panel1);
panel1.setLayout(null);
JButton button = new JButton("Click to display moves");
panel1.add(button);
button.setLocation(150, 20);
button.setSize(200,50);
button.addActionListener (new Button1Action());
JLabel textArea = new JLabel("Click the button to run moves!");
panel1.add(textArea);
textArea.setLocation(15, 40);
textArea.setSize(600, 100);
textBox = new JTextField(20);
textBox.setFont(textBox.getFont().deriveFont(20f));
textBox.setLocation(15, 180);
textBox.addActionListener (new TextAction() );
textBox.setVisible(true);
panel1.add(textBox);
JButton button2 = new JButton("Click to change file path");
panel1.add(button2);
button2.setLocation(150, 160);
button2.setSize(200,50);
button2.addActionListener (new Button2Action());
JLabel textArea2 = new JLabel("the textfield should be just above me");
panel1.add(textArea2);
textArea2.setLocation(15, 200);
textArea2.setSize(600, 100);
}
Move frame.add(panel1)
to the end of main, just before 'frame.setVisible(true)'. 将
frame.add(panel1)
移动到main的末尾,恰好在“ frame.setVisible(true)”之前。 So, the last threelines of main
become: 因此,
main
的最后三行变为:
textArea2.setSize(600, 100);
frame.add(panel1);
frame.setVisible(true)
I put frame.setVisible(true)
as the last line and textBox.setSize(100, 50);
我将
frame.setVisible(true)
作为最后一行,并将textBox.setSize(100, 50);
in with the textBox
variables and this solved the problem. 在与
textBox
变量,这解决了问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.