2015-06-08 17 views
0

文本框和標籤我有這樣的代碼:添加上點擊鼠標不使用的Java

import java.awt.BorderLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 

public class Interface2 { 

    private JLabel label; 
    private JLabel label2; 
    private JLabel label3; 
    private JTextField textfield; 
    private JTextField textfield2; 
    private JTextField textfield3; 
    private JButton button; 
    private JButton button2; 
    private JButton button3; 
    private JPanel panel; 

    public static void main(String[] args) { 
     new Interface(); 
    } 

    public Interface2() { 
     JFrame frame = new JFrame("Test"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(600, 600); 

     panel = new JPanel(); 

     JPanel buttonPane = new JPanel(); 
     label = new JLabel("Button 1"); 
     label2 = new JLabel("Button 2"); 
     label3 = new JLabel("Button 3"); 

     textfield = new JTextField(5); 

     JButton button = new JButton("cirkel"); 
     button.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       panel.removeAll(); 
       panel.add(label); 
       panel.add(textfield); 
       panel.revalidate(); 
       panel.repaint(); 
       String text = textfield.getText(); 

       // Van de String 'text' een double maken 

//  cirkel C1 = new cirkel(); 
//  C1.setDiam(diameter); 
       label.setText("De diameter = " + 1 + " cm \n\n"); 
       label.setText("De straal = " + 2 + " cm"); 
       label.setText("De oppervlakte van de cirkel = " + 3 + " cm2"); 
      } 
     }); 

     JButton button2 = new JButton("driehoek"); 
     button2.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       panel.removeAll(); 
       panel.add(label2); 
       panel.add(textfield2); 
       panel.revalidate(); 
       panel.repaint(); 
       // driehoek 
      } 
     }); 

     JButton button3 = new JButton("vierhoek"); 
     button3.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       panel.removeAll(); 
       panel.add(label3); 
       panel.add(textfield3); 
       panel.revalidate(); 
       panel.repaint(); 
       // vierhoek 
      } 
     }); 

     buttonPane.add(button); 
     buttonPane.add(button2); 
     buttonPane.add(button3); 

     frame.add(buttonPane, BorderLayout.NORTH); 
     frame.add(panel); 
     frame.setVisible(true); 
    } 
} 

我想添加一個標籤和一個文本框爲三個按鈕。

但是,如果我在這種情況下點擊任何按鈕,它會給我一個錯誤。

我不明白爲什麼它不工作,我是新來的Java ...

這是錯誤:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
    at Interface.actionPerformed(Interface.java:68) 
    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

*「在這種情況下,它給了我一個錯誤。」*我們獲得獎品來猜測錯誤是什麼嗎?始終複製/粘貼錯誤和異常輸出! –

+0

@AndrewThompson我複製了錯誤.. – user3024879

+2

'public static void main(String [] args){ new Interface(); } public Interface2(){'爲什麼'Interface2'類的'main'方法創建'Interface'的實例?爲了更快地獲得更好的幫助,請發佈[MCVE](http://stackoverflow.com/help/mcve)(最小完整可驗證示例)或[SSCCE](http://www.sscce.org/)(Short,Self Contained ,正確的例子)。 –

回答

2
public static void main(String[] args) { 
    new Interface(); 
} 

public Interface2() { 
    // .. 

這是可能的Interface2類的main方法不應該被創造Interface一個實例!

2

請確保文字欄和文字欄被初始化。

textfield2 = new JTextField(5); 
textfield3 = new JTextField(5); 

否則,您將收到NullPointerException。