[英]Swing Component not displaying on the frame
I don't understand why the RackBuilder
object I added to the frame
does not get displayed. 我不明白为什么我添加到frame
的RackBuilder
对象无法显示。
The code runs and the frame
gets generated. 代码运行并生成frame
。 I expect to see a panel with 42 rows, each row containing a JLabel
"test". 我希望看到一个包含42行的面板,每行包含一个JLabel
“测试”。 Is there something incorrect/missing from my constructor? 我的构造函数是否存在某些错误/缺失?
public class RackBuilderTool extends JPanel{
public RackBuilderTool() {
super(new GridLayout(42, 0));
for (int i = 0; i < 42; i++) {
add(new JLabel("test"));
}
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("Rack Builder Tool");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
RackBuilderTool rackBuilder = new RackBuilderTool();
rackBuilder.setOpaque(true);
frame.setContentPane(rackBuilder);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Thanks! 谢谢!
import javax.swing.*;
import java.awt.*;
public class RackBuilderTool extends JPanel{
public RackBuilderTool() {
super(new GridLayout(42, 0));
for (int i = 0; i < 42; i++) {
add(new JLabel("test"));
}
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("Rack Builder Tool");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
RackBuilderTool rackBuilder = new RackBuilderTool();
rackBuilder.setOpaque(true);
frame.setContentPane(rackBuilder);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
now it will show you 42 rows of your label. 现在它将向您显示标签的42行。
Realized that the "run" button on Netbeans IDE is to run the entire project. 意识到Netbeans IDE上的“运行”按钮是要运行整个项目。 As a result it was running another java file under the same project. 结果,它正在同一项目下运行另一个Java文件。
Once I right clicked on the java file I wanted to compile and clicked run it worked. 右键单击Java文件后,我想进行编译并单击运行,它就起作用了。
Thanks everyone for the help. 谢谢大家的帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.