-2
所以,我有一個問題。我知道問題是什麼,但我不知道如何解決。我會給你的代碼。setVisible(true/false)錯誤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)
你啓動JFrame? –
通過發佈長代碼,您限制了願意幫助您的人數。考慮簡化您的示例併發布[SSCCE](http://sscce.org/) – Pshemo
解決此類錯誤的方法很簡單:您可以閱讀堆棧跟蹤,注意該異常是從Frame1的第182行拋出的NullPointerException異常。 java,因此去那條線問問自己在那條線上可能是空的。使用調試器可以提供幫助。然後,你要麼修復代碼,以確保什麼是空的不是,因爲它不應該,或者你通過添加一個'if'測試來檢查它是否爲null,然後訪問它的一個成員。我們不知道在代碼的貼牆上有什麼第182行,所以我們不打擾猜測。 –