[英]Nothing is appearing when i switch JPanels
Im rather new at Java, I am have created a home page and a few buttons, When i click one of the buttons it sets the homepage panel visibility to false, opens a new class and sets that classes Jpanel to visible. 我在Java上不是很新,我已经创建了一个主页和一些按钮,当我单击其中一个按钮时,它将主页面板的可见性设置为false,打开一个新类并将该Jpanel类设置为可见。
homePanel.setVisible(false);
Goodsin Barcode = new Goodsin();
Goodsin.setVisible(true);
However once it opens the new class "Goodsin" it wont show any of the Buttons or TextFileds. 但是,一旦打开新类“ Goodsin”,它将不会显示任何Buttons或TextFileds。 I know it is opening the new class as a System.out.println prints to the console but nothing displays in the JFrame and i do not know why.
我知道它正在打开新类,因为System.out.println将打印到控制台,但JFrame中没有任何显示,我也不知道为什么。
Here is my code of the new class 这是我新班级的代码
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Goodsin {
public JPanel Goodsin;
public JTextField item1;
public String code;
public JButton btn1;
public Goodsin() {
System.out.println("TEST");
Goodsin = new JPanel();
item1 = new JTextField(10);
btn1 = new JButton("Look up Barcode");
Goodsin.setVisible(true);
Goodsin.add(item1);
item1.setSize(80, 30);
Goodsin.add(btn1);
btn1.setSize(80, 30);
}
public void getString(String code) {
System.out.println(code);
}
}
Im sure i am not doing something correct with the Jpanel or adding the textfields or button but all the answers i have seen so far havnt worked. 我确定我没有对Jpanel进行任何正确的操作,也没有添加文本字段或按钮,但是到目前为止我所看到的所有答案都没有用。
I suggest you add your panels to a JFrame. 我建议您将面板添加到JFrame。 You can either do this by extending JFrame from the class or simply instantiating one in your constructor.
您可以通过从类扩展JFrame或仅在构造函数中实例化JFrame来实现。 Then you can simply add and remove (or set visible/invisible) as you wish.
然后,您可以根据需要简单地添加和删除(或设置可见/不可见)。 Be sure to validate your JFrame/JPanel after changing visibility though.
不过,请务必在更改可见性之后验证您的JFrame / JPanel。
Try to do something like this: 尝试做这样的事情:
Goodsin = new JPanel();
item1 = new JTextField(10);
btn1 = new JButton("Look up Barcode");
item1.setSize(80, 30);
Goodsin.add(item1);
btn1.setSize(80, 30);
Goodsin.add(btn1);
JFrame frame = new JFrame("JFrame Example");
Goodsin.setLayout(new FlowLayout());
frame.add(Goodsin);
I suggest you try the following code : 我建议您尝试以下代码:
public class Goodsin extends JFrame{
public static void main(String[] args) {
Goodsin ui = new Goodsin();
JTextField item1 = new JTextField(10);
JButton btn1 = new JButton("Look up Barcode");
JPanel centralPanel = new JPanel(new FlowLayout());
centralPanel.add(item1);
centralPanel.add(btn1);
item1.setSize(80, 30);
btn1.setSize(80, 30);
ui.add(centralPanel);
ui.pack();
ui.setVisible(true);
}
}
Everything is done in the main method in my example, however you still can improve this code and refactor it in a better way. 在我的示例中,所有操作都在main方法中完成,但是您仍然可以改进此代码并以更好的方式对其进行重构。
只是您必须设置商品边界面板或设置大小并添加首页框架
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.