Nothing happens when running GUI(运行 GUI 时没有任何反应)
问题描述
我正在为我的班级做一个项目,我现在正在开发 GUI.我没有太多,因为,好吧,它没有出现,而且令人愤怒.这是我的代码.
I'm doing a project for my class and I'm working on the GUI right now. I don't have much because, well, it's not showing up and it's infuriating. Here's my code.
public class BookQuizGUI extends JFrame implements ActionListener
{
private Container c;
private JPanel pnlButtons;
private JButton addQs;
private JButton takeQuiz;
private JButton quit;
private Container c2;
private JPanel pnlButtons2;
private JComboBox qType;
private JComboBox ans;
private JTextField q;
private JTextField cA;
private JTextField cB;
private JTextField cC;
private JTextField cD;
private JButton add;
private JButton writeAll;
private JButton done;
/**
*
*/
public BookQuizGUI()
{
//The main screen for when the program starts
c = getContentPane();
pnlButtons = new JPanel();
pnlButtons.setLayout(new GridLayout(1, 3));
addQs = new JButton("Add Questions");
takeQuiz = new JButton("Take Quiz");
quit = new JButton("Quit");
setTitle("Book Quiz");
setSize(800, 400);
setLocation(400, 250);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pnlButtons.add(addQs);
pnlButtons.add(takeQuiz);
pnlButtons.add(quit);
addQs.addActionListener(this);
takeQuiz.addActionListener(this);
quit.addActionListener(this);
c.add(pnlButtons, BorderLayout.NORTH);
c.setVisible(true);
//The screen for when the user presses "Add questions"
c2 = getContentPane();
pnlButtons2 = new JPanel();
qType = new JComboBox();
qType.addItem("Elementary Question");
qType.addItem("Standard Question");
qType.addItem("Advanced Question");
pnlButtons2.add(qType);
}
/**
* @param args
*/
public static void main(String[] args)
{
BookQuizGUI gui = new BookQuizGUI();
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == quit)
{
System.exit(0);
}
else if(e.getSource() == addQs)
{
c2.setVisible(true);
}
}
}
我的另一个基本问题是如何在 GUI 屏幕之间切换?这段代码应该像书本测验一样,您可以添加问题.我是否只是让一个容器不可见而另一个容器可见?
Another fundamental question of mine is how to go about switching between GUI screens? This code is supposed to have like a book quiz and you can add questions. Do I just make one container not visible and another visible?
推荐答案
需要打包并设置为可见!
You need to pack it and set it to be visible!
frame.pack();
frame.setVisible(true);
至于回答你的第二个问题,为什么不只是改变元素并 repaint() 屏幕,如果我理解你希望它像一个有多个问题的测验,对吧?
As for answering your second question, why not just change the elements and repaint() the screen, if I understand you want it to be like a quiz with multiple questions, right?
这篇关于运行 GUI 时没有任何反应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:运行 GUI 时没有任何反应
基础教程推荐
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 在螺旋中写一个字符串 2022-01-01