setVisible(true / false)错误JFrame

时间:2015-05-01 14:51:18

标签: java swing error-handling jframe

所以,我有一个问题。我知道问题是什么,但我不知道如何解决它。我会给你代码。

public void initialize2(){

    txtWaaropGokken = new JTextField();
    txtWaaropGokken.setFont(new Font("Tahoma", Font.BOLD, 13));
    txtWaaropGokken.setHorizontalAlignment(SwingConstants.CENTER);
    txtWaaropGokken.setText("Waar wil je op gokken?");
    txtWaaropGokken.setBounds(10, 11, 414, 32);
    frame.getContentPane().add(txtWaaropGokken);
    txtWaaropGokken.setColumns(10);
    txtWaaropGokken.setEditable(false);

    JButton ChansesSimples = new JButton("Chanses Simples");
    ChansesSimples.setBounds(50, 65, 155, 32);
    frame.getContentPane().add(ChansesSimples);
    ChansesSimples.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {



            Random getal = new Random();
            int nummer;

            nummer=getal.nextInt(37); 

            txtHetBalletjeRolt = new JTextField();
            txtHetBalletjeRolt.setFont(new Font("Tahoma", Font.BOLD, 13));
            txtHetBalletjeRolt.setHorizontalAlignment(SwingConstants.CENTER);
            txtHetBalletjeRolt.setText("Het balletje is nu aan het rollen.");
            txtHetBalletjeRolt.setBounds(10, 11, 414, 32);
            frame.getContentPane().add(txtHetBalletjeRolt);
            txtHetBalletjeRolt.setColumns(10);
            txtHetBalletjeRolt.setEditable(false);

            ChansesSimples.setVisible(false); 
            Douzaines.setVisible(false);   \\HERE I DO GET THE ERROR
            Colonne.setVisible(false);   \\HERE I DO GET THE ERROR
            TransversaleSimple.setVisible(false); \\HERE I DO GET THE ERROR
            Carre.setVisible(false);    \\HERE I DO GET THE ERROR
            TransversalePlein.setVisible(false); \\HERE I DO GET THE ERROR
            Cheval.setVisible(false);   \\HERE I DO GET THE ERROR
            Plein.setVisible(false);   \\HERE I DO GET THE ERROR
            Cheval.setVisible(false);   \\HERE I DO GET THE ERROR






            if(nummer == 1 || nummer == 3 || nummer == 5 || nummer == 7 || nummer == 9 || nummer == 12 || nummer == 14 || nummer == 16 || nummer == 18 || nummer == 19 || nummer == 21 || nummer == 23 || nummer == 25 || nummer == 27 || nummer == 30 || nummer == 32 || nummer == 34 || nummer == 36){    

                txtRoodGewonnen = new JTextField();
                txtRoodGewonnen.setFont(new Font("Tahoma", Font.BOLD, 13));
                txtRoodGewonnen.setHorizontalAlignment(SwingConstants.CENTER);
                txtRoodGewonnen.setText("Gefeliciteerd! " + nummer + " is een rood nummer. Je hebt gewonnen!");
                txtRoodGewonnen.setBounds(10, 11, 414, 32);
                frame.getContentPane().add(txtRoodGewonnen);
                txtRoodGewonnen.setColumns(10);
                txtRoodGewonnen.setEditable(false);

                bedrag = bedrag + inzet * 1;

            }
            else{

                txtRoodVerloren = new JTextField();
                txtRoodVerloren.setFont(new Font("Tahoma", Font.BOLD, 13));
                txtRoodVerloren.setHorizontalAlignment(SwingConstants.CENTER);
                txtRoodVerloren.setText(nummer + " is geen rood nummer. Je hebt verloren.");
                txtRoodVerloren.setBounds(10, 11, 414, 32);
                frame.getContentPane().add(txtRoodVerloren);
                txtRoodVerloren.setColumns(10);
                txtRoodVerloren.setEditable(false);

                bedrag = bedrag - inzet;


            }
            txtBedrag = new JTextField();
            txtBedrag.setFont(new Font("Tahoma", Font.BOLD, 13));
            txtBedrag.setHorizontalAlignment(SwingConstants.CENTER);
            txtBedrag.setText("Je hebt nu " + bedrag + " euro.");
            txtBedrag.setBounds(10, 50, 414, 32);
            frame.getContentPane().add(txtBedrag);
            txtBedrag.setColumns(10);
            txtBedrag.setEditable(false);           
        }
    });

    JButton Douzaines = new JButton("Douzaines");
    Douzaines.setBounds(50, 105, 155, 32);
    frame.getContentPane().add(Douzaines);
    Douzaines.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {


        }
    });

    JButton Colonne = new JButton("Colonne");
    Colonne.setBounds(50, 145, 155, 32);
    frame.getContentPane().add(Colonne);
    Colonne.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {


        }
    });

    JButton TransversaleSimple = new JButton("Transversale Simple");
    TransversaleSimple.setBounds(50, 185, 155, 32);
    frame.getContentPane().add(TransversaleSimple);
    TransversaleSimple.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {


        }
    });

    JButton Carre = new JButton("Carré");
    Carre.setBounds(240, 65, 155, 32);
    frame.getContentPane().add(Carre);
    Carre.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {


        }
    });

    JButton TransversalePlein = new JButton("Transversale Plein");
    TransversalePlein.setBounds(240, 105, 155, 32);
    frame.getContentPane().add(TransversalePlein);
    TransversalePlein.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {


        }
    });

    JButton Cheval = new JButton("Cheval");
    Cheval.setBounds(240, 145, 155, 32);
    frame.getContentPane().add(Cheval);
    Cheval.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {


        }
    });

    JButton Plein = new JButton("Plein");
    Plein.setBounds(240, 185, 155, 32);
    frame.getContentPane().add(Plein);
    Plein.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            ChansesSimples.setVisible(false);    \\HERE I DONT GET AN ERROR
            Douzaines.setVisible(false);      \\HERE I DONT GET AN ERROR
            Colonne.setVisible(false);     \\HERE I DONT GET AN ERROR
            TransversaleSimple.setVisible(false); \\HERE I DONT GET AN ERROR
            Carre.setVisible(false);     \\HERE I DONT GET AN ERROR
            TransversalePlein.setVisible(false); \\HERE I DONT GET AN ERROR
            Cheval.setVisible(false);     \\HERE I DONT GET AN ERROR
            Plein.setVisible(false);     \\HERE I DONT GET AN ERROR
            Cheval.setVisible(false);     \\HERE I DONT GET AN ERROR


        }
    });

}

问题在于,如果该计划已经通过' JButton,如果我然后setVisible(false)。有用。正如你在Plein JButton中看到的那样,我没有遇到任何代码错误。但是在Chanses Simples JButton中我只能让按钮本身消失,如果我不想得到任何错误。当然我希望玩家可以选择第一个按钮。有什么想法吗?

这是我得到的错误代码:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Frame1$3.actionPerformed(Frame1.java:182)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

0 个答案:

没有答案