2016-12-28 56 views
0

顯示GUI表決程序的結果,我被要求做一個GUI的投票程序,我幾乎沒有,但我有這麼一個小問題,當涉及到顯示結果在Java

這是我的計劃

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
public class Voting extends JFrame implements ActionListener 

{ 
    JLabel label1; 
    JTextField txtfield1; 
    JPanel pan1; 
    JButton b1; 

    public Voting() 
    { 
     super("VoteDialog"); 
     setSize(320,180); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     label1 = new JLabel("Enter how many candidates : "); 
     txtfield1 = new JTextField(20); 
     b1 = new JButton("OK"); 
     pan1 = new JPanel(); 
     pan1.add(label1); 
     pan1.add(txtfield1); 
     pan1.add(b1); 
     b1.addActionListener(this); 
     add(pan1); 

     setVisible(true); 

    } 
    public void actionPerformed(ActionEvent e) 
    { 
     int num = Integer.parseInt(txtfield1.getText()); 
     Window2 w = new Window2(num); 
     setVisible(false); 


    } 
} 

這是第一類「投票」,它顯示了一個窗口要求候選人數量。

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
public class Window2 extends JFrame implements ActionListener 
{ 
    JLabel label2; 
    JTextField txtFields; 
    JPanel p1,p2,p3; 
    JButton button; 
    JTextField[] f1; 
    private int y; 
    public Window2(int y) 
    { 
     super("VoteDialog 2"); 
     this.y=y; 
     setSize(1000,1000); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setLayout(new GridLayout(3,1)); 
     label2 = new JLabel("Enter the names of candidates : "); 
     p1 = new JPanel(); 
     p2 = new JPanel(); 
     p3 = new JPanel(); 
     p1.add(label2); 
     f1 = new JTextField[y]; 

     for(int j = 0; j<y; j++) 
     { 
      JTextField txtFields = new JTextField(15); 
      f1[j] = txtFields; 
      p2.add(txtFields); 


     } 

     button = new JButton("OK"); 
     p3.add(button); 
     button.addActionListener(this); 
     add(p1); 
     add(p2); 
     add(p3); 
     pack(); 
     setVisible(true); 



    } 
    public void actionPerformed(ActionEvent e) 
    { 
     Window3 w1 = new Window3(y); 
     setVisible(false); 
    } 
    private class Window3 extends JFrame implements ActionListener{ 

     JPanel p1,p2,p4,p5; 
     JPanel[] p3; 
     JButton button1; 
     JLabel label1,label2,label3; 
     JRadioButton[] b1; 
     private int count[]; 
     private int x; 
     public Window3(int x) 
     { 
      super("Window number 3 "); 
      this.x = x; 
      setSize(500,500); 
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      setLayout(new GridLayout((4+x),1)); 
      p1 = new JPanel(); 
      p2 = new JPanel(); 
      p3 = new JPanel[x]; 
      p4 = new JPanel(); 
      p5 = new JPanel(); 
      label1 = new JLabel("click the vote button once you have selected a candidate ."); 
      label2 = new JLabel("The candidates are : "); 
      p1.add(label1); 
      p2.add(label2); 
      b1 = new JRadioButton[x]; 
      add(p1); 
      add(p2); 

       for(int j = 0; j<x; j++) 
       { 
        JRadioButton radioButtons = new JRadioButton("Candidate " + (j+1) + " : " + f1[j].getText()); 
        b1[j] = radioButtons; 
        p3[j] = new JPanel(); 
        p3[j].add(radioButtons); 
        add(p3[j]); 
       } 

      button1 = new JButton("Vote!"); 
      button1.addActionListener(this); 
      p4.add(button1); 
      label3 = new JLabel("Vote now!"); 
      add(p4); 
      p5.add(label3); 
      add(p5); 
      pack(); 
      setVisible(true); 

     } 

     public void actionPerformed(ActionEvent e) 
     { 
      count = new int[b1.length]; 
      for(int i = 0; i<b1.length; i++) 
      { 
       if(b1[i].isSelected()){ 
        count[i]++; 
       } 


      } 
      setVisible(false); 
      Window4 w2 = new Window4(x); 

     } 
     private class Window4 extends JFrame 
     { 
      JLabel label1; 
      JLabel[] label2; 
      JPanel p1; 
      JPanel[] p2; 
      public Window4(int t) 
      { 
       super("Window 4"); 
       setSize(200,300); 
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       setLayout(new GridLayout(1+t,1)); 
       label1 = new JLabel("The result :"); 
       p1 = new JPanel(); 
       p1.add(label1); 
       add(p1); 
       for(int i = 0; i<y; i++) 
       { 

        label2[i] = new JLabel("Candidate " + i+1 + " : " + count[i]); 
        JPanel panels = new JPanel(); 
        p2[i].add(label2[i]); 
        p2[i] = panels; 
        add(panels); 
       } 
       pack(); 
       setVisible(true); 
      } 
     } 

    } 
} 

Window2第二類要求用戶輸入候選人的姓名,另外,它具有一個內部類Window3顯示voing窗口給用戶,類Window3還具有內類Window4到顯示結果,你可以看到

這裏的問題是一切工作正常,直到它得到的顯示結果的一部分,它給了我的錯誤,我不知道爲什麼

這是測試類

public class TestVoting { 

    public static void main(String[] args) { 
     Voting v = new Voting(); 

    } 

} 

這些都是錯誤

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
    at Window2$Window3$Window4.<init>(Window2.java:135) 
    at Window2$Window3.actionPerformed(Window2.java:115) 
    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$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) 
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.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$JavaSecurityAccessImpl.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

你得到了什麼樣的錯誤? – Aaron

+0

Java **不是Javascript Javascript! *「,它給了我錯誤」*哪些錯誤?請發佈堆棧跟蹤 – Frakcool

+0

@Aaron您可以在問題 – Bbnnbbnnbbnn

回答

0

空指針異常

https://docs.oracle.com/javase/7/docs/api/java/lang/NullPointerException.html

的問題應固定在Window4:

private class Window4 extends JFrame 
     { 
      JLabel label1; 
      JLabel[] label2; 
      JPanel p1; 
      JPanel[] p2; //<-------This variable isnt initialized anywhere 
      public Window4(int t) 
      { 
       super("Window 4"); 
       setSize(200,300); 
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       setLayout(new GridLayout(1+t,1)); 
       label1 = new JLabel("The result :"); 
       p1 = new JPanel(); 
       p1.add(label1); 
       add(p1); 
       for(int i = 0; i<y; i++) 
       { 

        label2[i] = new JLabel("Candidate " + i+1 + " : " + count[i]); 
        JPanel panels = new JPanel(); 
        p2[i].add(label2[i]); //<--------Yet, It is being worked with here 
        p2[i] = panels; 
        add(panels); 
       } 
       pack(); 
       setVisible(true); 
      } 
     } 

因此,在使用它之前,只需初始化p2 JPanel數組。