我无法让我的 GUI 显示,而且我确定我遗漏了一些非常简单的东西。这是为了家庭作业。包含 GUI 的类在这里:
public class SorcGUI extends JFrame{
public void SorcGUI(){
JTextArea jta = new JTextArea();
JPanel jp1 = new JPanel();
setTitle ("Sorcerers Cave");
setSize (600, 600);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
JButton jbsearch = new JButton("Search");
JTree jTree = null;
JScrollPane treeView = new JScrollPane(jTree);
JButton jbshow = new JButton("Show Contents");
JButton jbread = new JButton("Import File");
JScrollPane jsoutput = new JScrollPane(jta);
JTextField jtfsearch = new JTextField(" enter search term ");
JLabel jlsearch = new JLabel("Search Type : ");
JComboBox jcbsearch = new JComboBox <> ();
jcbsearch.addItem ("index");
jcbsearch.addItem ("Type");
jcbsearch.addItem ("weight");
//create a panel to hold the buttons and text fields
jp1.add(jbread);
jp1.add(jbshow);
jp1.add(jtfsearch);
jp1.add(jlsearch);
jp1.add(jcbsearch);
jp1.add(jbsearch);
jp1.add(treeView);
//add the components to the GUI
add(jp1, BorderLayout.PAGE_START);
add(jsoutput, BorderLayout.CENTER);
setVisible(true);
jbread.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
SorcCave sc = new SorcCave();
}
});
jbshow.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("show");
}
});
jbsearch.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("search");
}
});
我创建 GUI 的方式只是将以下内容添加到我的主要方法中:
SorcGUI gui = new SorcGUI();
该程序简单地从运行到三秒钟内构建成功,从不显示 GUI。我正在尝试通过将 GUI 内容放入它自己的类文件来清理我的项目。感谢您的任何帮助。