Java第二帧没有显示

时间:2017-11-20 17:18:27

标签: java swing jframe jpanel actionevent

我创建了一个项目,在表格中你看到4个按钮,当你点击它时,必须出现另一个框,但是当我这样做时,第二个窗口打开,两个窗口都关闭我无法访问代码应该从ActionPerformed()中出来,它只是弹出另一个窗口并在不打开的情况下在不到3秒内关闭,其他代码工作正常我累了,这是第一个框的代码:

public class MathoQuest extends JFrame implements ActionListener  {
    JButton boutConvert, boutGeo, boutFonc, boutOut;



    public MathoQuest() {
        setTitle("Bienvenue a MathoQuest");
        setSize(250,500);
        JPanel simplePanel = new JPanel();
        simplePanel.setLayout(null);
        add(simplePanel);
        Font helvb14 = new Font("Arial" , Font.BOLD , 30);


        boutConvert = new JButton("Convertir");
        boutConvert.setFont(helvb14);
        boutConvert.setForeground(Color.white);
        boutConvert.setBackground(new Color(63,107,220));
        simplePanel.add(boutConvert);
        boutConvert.setBounds(25,50,200,80);
        boutConvert.addActionListener(this);

        boutGeo = new JButton("Geometrie-\nEN CONSTRUCTION-");
        boutGeo.setFont(helvb14);
        boutConvert.setForeground(Color.white);
        boutGeo.setBackground(new Color(145,110,220));
        simplePanel.add(boutGeo);
        boutGeo.setBounds(25,150,200,80);
        boutGeo.addActionListener(this);

        boutFonc = new JButton("Fonction");
        boutFonc.setFont(helvb14);
        boutFonc.setForeground(Color.white);
        boutFonc.setBackground(new Color(150,200,80));
        simplePanel.add(boutFonc);
        boutFonc.setBounds(25,250,200,80);
        boutFonc.addActionListener(this);

        boutOut = new JButton("Quitter");
        boutOut.setFont(helvb14);
        boutOut.setForeground(Color.white);
        boutOut.setBackground(new Color(245,130,0));
        simplePanel.add(boutOut);
        boutOut.setBounds(25,350,200,80);
        boutOut.addActionListener(this);        


    }
    public static void main(String[] args) {
        MathoQuest mathframe = new MathoQuest();
    mathframe.setVisible(true);
        mathframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }    

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == boutConvert) {
                    ConversionFrame frame = new ConversionFrame();
                    frame.getContentPane();
                    frame.setVisible(true);
        }
        if (e.getSource() == boutGeo) {
             JOptionPane.showMessageDialog(null,"Ce mode est encore en construction merci de reessayer plus tard");
        }
        if (e.getSource() == boutFonc) {
                FonctionFrame dess = new FonctionFrame();
                dess.getContentPane();
        dess.setVisible(true);
        }
        if (e.getSource() == boutOut)
            JOptionPane.showMessageDialog(null,"Au revoir et merci d'avoir utilise cette application");
            System.exit(0);
    }


}

1 个答案:

答案 0 :(得分:1)

您是否尝试将最后if条件括在大括号中?

如果没有将其封闭,System.exit(0);将在任何情况下被调用,只有if之后的直线才会成为其中的一部分。

因此,exit方法将成为直接方法的一部分,而不是if块的一部分。

实际上,程序将如下所示:

if (e.getSource() == boutOut)
{
    JOptionPane.showMessageDialog(null,"Au revoir et merci d'avoir utilise cette application");
}

System.exit(0);

它应该是这样的:

if (e.getSource() == boutOut)
{
    JOptionPane.showMessageDialog(null,"Au revoir et merci d'avoir utilise cette application");
    System.exit(0);
}