繁体   English   中英

运行GUI时无反应

[英]Nothing happens when running GUI

我正在为我的课程做一个项目,现在正在使用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屏幕之间进行切换? 该代码应该像书本测验一样,您可以添加问题。 我是否仅使一个容器不可见而使另一个容器可见?

您需要打包并将其设置为可见!

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

至于回答第二个问题,如果我理解您希望它像一个具有多个问题的测验,那么为什么不只是更改元素并重新绘制屏幕呢?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM