簡體   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