gpt4 book ai didi

java - 当我在主方法中创建 GUI 时,它似乎没有显示

转载 作者:行者123 更新时间:2023-12-01 18:43:05 25 4
gpt4 key购买 nike

我在显示 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 内容放入它自己的类文件中来清理我的项目。感谢您的帮助。

最佳答案

SorcGUI 的构造函数中删除 void 关键字,以便可以将组件添加到父容器

public SorcGUI() {

阅读:Providing Constructors for Your Classes

关于java - 当我在主方法中创建 GUI 时,它似乎没有显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19083589/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com