【问题标题】:Nothing happens when running GUI运行 GUI 时没有任何反应
【发布时间】:2015-01-26 12:47:26
【问题描述】:

我正在为我的班级做一个项目,我现在正在开发 GUI。我没有太多,因为,好吧,它没有出现,而且令人愤怒。这是我的代码。

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 屏幕之间切换?这段代码应该像书本测验一样,您可以添加问题。我是否只是让一个容器不可见而另一个容器可见?

【问题讨论】:

  • 你不想猜测 - 阅读教程。您可以在此处找到 Swing 教程和其他 Swing 资源的链接:Swing Info
  • 谢谢,我会读下去的!

标签: java swing user-interface jframe


【解决方案1】:

你需要把它打包并设置为可见!

    frame.pack();
    frame.setVisible(true);

至于回答你的第二个问题,为什么不只是改变元素并 repaint() 屏幕,如果我理解你希望它像一个有多个问题的测验,对吧?

【讨论】:

  • 这是主要的吗?
  • 把它放在pnlButtons2.add(qType); 之后
  • 其实你可以把它放在任何可以访问框架变量的地方。
  • 谢谢,非常感谢!
  • 如果可以,请将其标记为正确答案! :) 谢谢
猜你喜欢
  • 1970-01-01
  • 2021-04-06
  • 1970-01-01
  • 2013-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-12
  • 1970-01-01
相关资源
最近更新 更多