[英]Swing & AWT: ContentPane items not showing in JFrame
我正在创建一个基本的Java Swing应用程序,但是我的内容窗格出现问题,因为它没有显示我的对象。
这是我的主班,以后将调用其他班,并且仅在调用Frame.java:
public class Main
{
public static void main(String[] args)
{
System.out.println("HI");
Frame frameObject = new Frame();
frameObject.main();
}
}
这是我创建框架的框架类:
import javax.swing.*;
import java.awt.*;
public class Frame
{
public static void main()
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
JFrame frame = new MainFrame("Warlock of Firetop Mountain");
//Implementing Toolkit to allow computer to get dimensions of screen and assign them to two int values
Toolkit tk = Toolkit.getDefaultToolkit();
int xsize = (int) tk.getScreenSize().getWidth();
int ysize = (int) tk.getScreenSize().getHeight();
frame = new JFrame("Warlock of Firetop Mountain");
frame.setTitle("Warlock of Firetop Mountain");
frame.setSize(new Dimension(xsize, ysize));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(true);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
这是我的mainFrame.java,用于存储组件:
public class MainFrame extends JFrame
{
public MainFrame(String title)
{
super(title);
setLayout(new BorderLayout());
//Components - Buttons
JButton saveButton =new JButton("Save");
JButton loadButton =new JButton("Load");
JButton optionsButton = new JButton("Options");
JButton inventoryButton =new JButton("Inventory");
Container buttonsContainer = getContentPane();
buttonsContainer.add(saveButton, BorderLayout.LINE_START);
buttonsContainer.add(loadButton, BorderLayout.CENTER);
buttonsContainer.add(optionsButton,BorderLayout.CENTER);
buttonsContainer.add(inventoryButton,BorderLayout.AFTER_LINE_ENDS);
//Components - Enter Button
JButton enterButton = new JButton("Enter");
// /Components - ComboBox
JComboBox pageTurner = new JComboBox();
//Components = JTextArea
JTextArea currentText = new JTextArea();
}
}
frame = new JFrame("Warlock of Firetop Mountain");
为什么用新的JFrame实例重置框架? 您不是已经用MainFrame设置了吗?
您正在做的就是将框架分配给包含所有组件的新MainWindow。 但是,然后,您将框架重新分配给新的JFrame,默认情况下,JFrame没有组件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.