2014-11-05 67 views
-2

我的程序没有显示计算器的JTextField部分,并且清晰的JButton不会重置计算器,所以当我清除按钮并尝试执行新的计算时,出现运行时错误.BTW Boolean s的应该是operationchosen为什么不是我的JButton工作和JTextField不显示?

import java.awt.event.*; 
import java.awt.*; 
import javax.swing.*; 


public class calculator extends JFrame 
{ 
private JButton num1; 
private JButton num2; 
private JButton num3; 
private JButton num4; 
private JButton num5; 
private JButton num6; 
private JButton num7; 
private JButton num8; 
private JButton num9; 
private JButton num0; 
private JButton equal; 
private JButton addition; 
private JButton sub; 
private JButton mult; 
private JButton divide; 
private JButton modulus; 
private JButton solve; 
private JButton clear; 
private boolean s=false; 
private boolean equalsClicked=false; 
private double val; 
private String temp1,temp2,answer; 
private JTextField answerfield; 
private JPanel contentPane; 
private char operation=' '; 


public calculator(String x) 
{ 
    super(x); 
    answerfield=new JTextField(); 
    answerfield.setEditable(false); 

    answerfield.setPreferredSize(new Dimension(160, 20)); 
    answerfield.setBackground(Color.WHITE); 
    answerfield.setEnabled(false); 
    answerfield.setHorizontalAlignment(4); 
    answerfield.setDisabledTextColor(Color.BLACK); 

    contentPane = new JPanel(); 
    contentPane.setLayout(null); 
    contentPane.add(answerfield,BorderLayout.NORTH); 




    setBounds(400,300,255,400); 
    this.setContentPane(contentPane); 


    Numbers n=new Numbers(); 
    Calculation c=new Calculation(); 

    num1=new JButton("1"); 
    num1.setBounds(10, 130, 52, 37); 
    add(num1); 
    num1.setActionCommand("1"); 
    num1.addActionListener(n); 


    num2=new JButton("2"); 
    num2.setBounds(72, 130, 52, 37); 
    add(num2); 
    num2.setActionCommand("2"); 
    num2.addActionListener(n); 


    num3=new JButton("3"); 
    num3.setBounds(134, 130, 52, 37); 
    add(num3); 
    num3.setActionCommand("3"); 
    num3.addActionListener(n); 


    num4=new JButton("4"); 
    num4.setBounds(10, 178, 52, 37); 
    add(num4); 
    num4.setActionCommand("4"); 
    num4.addActionListener(n); 


    num5=new JButton("5"); 
    num5.setBounds(72, 178, 52, 37); 
    add(num5); 
    num5.setActionCommand("5"); 
    num5.addActionListener(n); 


    num6=new JButton("6"); 
    num6.setBounds(134, 178, 52, 37); 
    add(num6); 
    num6.setActionCommand("6"); 
    num6.addActionListener(n); 


    num7=new JButton("7"); 
    num7.setBounds(10, 226, 52, 37); 
    add(num7); 
    num7.setActionCommand("7"); 
    num7.addActionListener(n); 


    num8=new JButton("8"); 
    num8.setBounds(72, 226, 52, 37); 
    add(num8); 
    num8.setActionCommand("8"); 
    num8.addActionListener(n); 



    num9=new JButton("9"); 
    num9.setBounds(134, 226, 52, 37); 
    add(num9); 
    num9.setActionCommand("9"); 
    num9.addActionListener(n); 


    num0=new JButton("0"); 
    num0.setBounds(10, 274, 52, 37); 
    add(num0); 
    num0.setActionCommand("0"); 
    num0.addActionListener(n); 


    equal=new JButton("="); 
    equal.setBounds(72, 274, 52, 37); 
    add(equal); 
    equal.setActionCommand("="); 
    equal.addActionListener(c); 


    addition=new JButton("+"); 
    addition.setBounds(193, 130,45, 35); 
    add(addition); 
    addition.setActionCommand("+"); 

    addition.addActionListener(c); 

    sub=new JButton("-"); 
    sub.setBounds(193, 179, 45, 35); 
    add(sub); 
    sub.setActionCommand("-"); 

    sub.addActionListener(c); 

    mult=new JButton("*"); 
    mult.setBounds(193,226, 45, 35); 
    add(mult); 
    mult.setActionCommand("*"); 

    mult.addActionListener(c); 

    divide=new JButton("/"); 
    divide.setBounds(193, 274, 45, 35); 
    add(divide); 
    divide.setActionCommand("/"); 

    divide.addActionListener(c); 

    modulus=new JButton("%"); 
    modulus.setBounds(193,322, 45, 35); 
    add(modulus); 
    modulus.setActionCommand("%"); 

    modulus.addActionListener(c); 


    clear=new JButton("C"); 
    clear.setBounds(53, 343, 89, 23); 
    add(clear); 
    clear.setActionCommand("C"); 
    clear.addActionListener(c); 







    setVisible(true); 
    show(); 
    setResizable(false); 


} 
private class Numbers implements ActionListener 
{ 
    public void actionPerformed(ActionEvent event) 
    { 
     JButton x=(JButton)event.getSource(); 
     if(x.equals(num1)) 
     { 
      if(s==false) 
      { 
       if(temp1==null) 
       { 
        temp1="1"; 
       } 
       else 
        temp1+="1"; 
      } 
      else 
      { 
       if(temp2==null) 
       { 
        temp2="1"; 
       } 
       else 
        temp2+="1"; 
      } 

     } 
     if(x.equals(num2)) 
     { 
      if(s==false) 
      { 
       if(temp1==null) 
       { 
        temp1="2"; 
       } 
       else 
        temp1+="2"; 
      } 
      else 
      { 
       if(temp2==null) 
       { 
        temp2="2"; 
       } 
       else 
        temp2+="2"; 
      } 

     } 
     if(x.equals(num3)) 
     { 
      if(s==false) 
      { 
       if(temp1==null) 
       { 
        temp1="3"; 
       } 
       else 
        temp1+="3"; 
      } 
      else 
      { 
       if(temp2==null) 
       { 
        temp2="3"; 
       } 
       else 
        temp2+="3"; 
      } 

     } 
     if(x.equals(num4)) 
     { 
      if(s==false) 
      { 
       if(temp1==null) 
       { 
        temp1="4"; 
       } 
       else 
        temp1+="4"; 
      } 
      else 
      { 
       if(temp2==null) 
       { 
        temp2="4"; 
       } 
       else 
        temp2+="4"; 
      } 

     } 
     if(x.equals(num5)) 
     { 
      if(s==false) 
      { 
       if(temp1==null) 
       { 
        temp1="5"; 
       } 
       else 
        temp1+="5"; 
      } 
      else 
      { 
       if(temp2==null) 
       { 
        temp2="5"; 
       } 
       else 
        temp2+="5"; 
      } 

     } 
     if(x.equals(num6)) 
     { 
      if(s==false) 
      { 
       if(temp1==null) 
       { 
        temp1="6"; 
       } 
       else 
        temp1+="6"; 
      } 
      else 
      { 
       if(temp2==null) 
       { 
        temp2="6"; 
       } 
       else 
        temp2+="6"; 
      } 

     } 
     if(x.equals(num7)) 
     { 
      if(s==false) 
      { 
       if(temp1==null) 
       { 
        temp1="7"; 
       } 
       else 
        temp1+="7"; 
      } 
      else 
      { 
       if(temp2==null) 
       { 
        temp2="7"; 
       } 
       else 
        temp2+="7"; 
      } 

     } 
     if(x.equals(num8)) 
     { 
      if(s==false) 
      { 
       if(temp1==null) 
       { 
        temp1="8"; 
       } 
       else 
        temp1+="8"; 
      } 
      else 
      { 
       if(temp2==null) 
       { 
        temp2="8"; 
       } 
       else 
        temp2+="8"; 
      } 

     } 
     if(x.equals(num9)) 
     { 
      if(s==false) 
      { 
       if(temp1==null) 
       { 
        temp1="9"; 
       } 
       else 
        temp1+="9"; 
      } 
      else 
      { 
       if(temp2==null) 
       { 
        temp2="9"; 
       } 
       else 
        temp2+="9"; 
      } 

     } 
     if(x.equals(num0)) 
     { 
      if(s==false) 
      { 
       if(temp1==null) 
       { 
        temp1="0"; 
       } 
       else 
        temp1+="0"; 
      } 
      else 
      { 
       if(temp2==null) 
       { 
        temp2="0"; 
       } 
       else 
        temp2+="0"; 
      } 

     } 
     if(equalsClicked==false) 
     { 
      if(s==false) 
      { 
       answerfield.setText(temp1); 
      } 
      else 
      { 
       answerfield.setText(temp2); 
      } 
     } 



    } 
} 
public void Clear() 
{ 
    temp1=""; 
    temp2=""; 
    s=false; 
    operation=' '; 
    val=0; 
    answer=""; 
    equalsClicked=false; 
    System.out.println("hey"); 
} 
private class Calculation implements ActionListener 
{ 
    public void actionPerformed(ActionEvent event) 
    { 
     JButton x=(JButton)event.getSource(); 
     if(x.equals(addition)) 
     { 
      if(temp1==null) 
       System.out.println("choose numbers"); 
      else 
      { 
       if(temp1!=null&&temp2==null) 
       { 
        s=true; 
        operation='+'; 

       } 

      } 
     } 
     if(x.equals(sub)) 
     { 
      if(temp1==null) 
       System.out.println("choose numbers"); 
      else 
      { 
       if(temp1!=null&&temp2==null) 
       { 
        s=true; 
        operation='-'; 

       } 

      } 
     } 
     if(x.equals(mult)) 
     { 
      if(temp1==null) 
       System.out.println("choose numbers"); 
      else 
      { 
       if(temp1!=null&&temp2==null) 
       { 
        s=true; 
        operation='*'; 

       } 

      } 
     } 
     if(x.equals(divide)) 
     { 
      if(temp1==null) 
       System.out.println("choose numbers"); 
      else 
      { 
       if(temp1!=null&&temp2==null) 
       { 
        s=true; 
        operation='/'; 

       } 

      } 
     } 
     if(x.equals(modulus)) 
     { 
      if(temp1==null) 
       System.out.println("choose numbers"); 
      else 
      { 
       if(temp1!=null&&temp2==null) 
       { 
        s=true; 
        operation='%'; 

       } 

      } 
     } 
     if(x.equals(equal)) 
     { 
      if(temp1==null) 
       System.out.println("choose numbers"); 
      else 
      { 
       if(temp1!=null&&temp2==null) 
        System.out.println("choose numbers"); 


      } 
      if(temp1!=null&&temp2!=null) 
      { 
       double d=0.0; 
       double s=0.0; 
       d=Double.parseDouble(temp1); 
       s=Double.parseDouble(temp2); 
       switch(operation) 
       { 
        case '+': 
        { 
         val=d+s; 
         break; 
        } 
        case '-': 
        { 
         val=d-s; 
         break; 
        } 
        case'*': 
        { 
         val=d*s; 
         break; 
        } 
        case'/': 
        { 
         val=d/s; 
         break; 
        } 
        case'%': 
        { 
         val=d%s; 
         break; 
        } 

       } 
       answer=Double.toString(val); 
       answerfield.setText(answer); 
       System.out.println(answer); 
      } 
     } 
     if(x.equals(clear)) 
     { 
      Clear(); 
     } 
    } 
} 

public static void main(String args[]) 
{ 
    new calculator("math"); 
} 

}

+0

圣$ H | T这就是大量的代码在一个问题 - 我没有投下来,仅仅是为了记录 – 2014-11-05 03:55:13

+0

@Abdul艾哈迈德我只是不知道我在哪里出了错。 – user4216980 2014-11-05 03:57:50

+1

'contentPane.setLayout(null);'会很好地表明你的问题... – MadProgrammer 2014-11-05 03:59:16

回答

1

首先,你这样做......

answerfield = new JTextField(); 
answerfield.setEditable(false); 

answerfield.setPreferredSize(new Dimension(160, 20)); 
answerfield.setBackground(Color.WHITE); 
answerfield.setEnabled(false); 
answerfield.setHorizontalAlignment(4); 
answerfield.setDisabledTextColor(Color.BLACK); 

这一切似乎很酷...

但你这样做......

contentPane = new JPanel(); 
contentPane.setLayout(null); 
contentPane.add(answerfield, BorderLayout.NORTH); 

... answerfield没有定义的大小或位置,所以它的大小是0x0,它的位置是0x0。 Swing是足够聪明,不把它漆成...

答案是,不使用null布局...

避免使用null布局,像素完美的布局是现代的UI设计中的错觉。影响组件的个体大小的因素太多,其中没有一个可以控制。摇摆设计为核心与布局管理工作,丢弃这些会导致没有问题,问题是,你将花费更多的时间,试图结束整顿

看看Why is it frowned upon to use a null layout in SWING?Laying Out Components Within a Container更多细节

更新与布置的例子

本例使用GridBagLayout这可能是最强大的默认布局管理器中的一个,但也是最困难的使用一个...

Layout

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.EventQueue; 
import java.awt.Font; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Insets; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 
import javax.swing.border.EmptyBorder; 

public class Calculator extends JFrame { 

    private boolean s = false; 
    private boolean equalsClicked = false; 
    private double val; 
    private String temp1, temp2, answer; 
    private JTextField answerfield; 
    private char operation = ' '; 

    public Calculator(String x) { 
     super(x); 
     answerfield = new JTextField(); 
     answerfield.setEditable(false); 

     answerfield.setBackground(Color.WHITE); 
     answerfield.setEnabled(false); 
     answerfield.setHorizontalAlignment(4); 
     answerfield.setDisabledTextColor(Color.BLACK); 
     answerfield.setFont(answerfield.getFont().deriveFont(Font.BOLD, 24)); 

     JPanel field = new JPanel(new BorderLayout()); 
     field.setBorder(new EmptyBorder(4, 4, 0, 4)); 
     field.add(answerfield); 
     add(field, BorderLayout.NORTH); 

     JPanel numbers = new JPanel(new GridBagLayout()); 

     Numbers n = new Numbers(); 
     Calculation c = new Calculation(); 

     addButtonTo(numbers, "1", n, 0, 0); 
     addButtonTo(numbers, "2", n, 1, 0); 
     addButtonTo(numbers, "3", n, 2, 0); 
     addButtonTo(numbers, "+", c, 3, 0); 
     addButtonTo(numbers, "4", n, 0, 1); 
     addButtonTo(numbers, "5", n, 1, 1); 
     addButtonTo(numbers, "6", n, 2, 1); 
     addButtonTo(numbers, "-", c, 3, 1); 
     addButtonTo(numbers, "7", n, 0, 2); 
     addButtonTo(numbers, "8", n, 1, 2); 
     addButtonTo(numbers, "9", n, 2, 2); 
     addButtonTo(numbers, "*", c, 3, 2); 
     addButtonTo(numbers, "0", n, 0, 3); 
     addButtonTo(numbers, "=", c, 1, 3); 
     addButtonTo(numbers, "", null, 2, 3); 
     addButtonTo(numbers, "/", n, 3, 3); 
     addButtonTo(numbers, "C", c, 0, 4, 3); 
     addButtonTo(numbers, "%", c, 3, 4); 

     add(numbers); 

     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     pack(); 
     setVisible(true); 

    } 

    protected void addButtonTo(JPanel panel, String text, ActionListener listener, int gridX, int gridY) { 

     addButtonTo(panel, text, listener, gridX, gridY, 1); 

    } 

    protected void addButtonTo(JPanel panel, String text, ActionListener listener, int gridX, int gridY, int gridWidth) { 

     JComponent comp = null; 
     if (text == null || text.trim().isEmpty()) { 
      comp = new JPanel(); 
     } else { 

      JButton button = new JButton(text); 
      button.setActionCommand(text); 
      button.addActionListener(listener); 
      comp = button; 

     } 

     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.gridx = gridX; 
     gbc.gridy = gridY; 
     gbc.gridwidth = gridWidth; 
     gbc.insets = new Insets(4, 4, 4, 4); 
     gbc.fill = GridBagConstraints.BOTH; 

     panel.add(comp, gbc); 

    } 

    private class Numbers implements ActionListener { 

     public void actionPerformed(ActionEvent event) { 
      JButton x = (JButton) event.getSource(); 
      String text = x.getActionCommand(); 

      if (s == false) { 
       if (temp1 == null) { 
        temp1 = text; 
       } else { 
        temp1 += text; 
       } 
      } else { 
       if (temp2 == null) { 
        temp2 = text; 
       } else { 
        temp2 += text; 
       } 
      } 

      if (equalsClicked == false) { 
       if (s == false) { 
        answerfield.setText(temp1); 
       } else { 
        answerfield.setText(temp2); 
       } 
      } 

     } 
    } 

    public void Clear() { 
     temp1 = ""; 
     temp2 = ""; 
     s = false; 
     operation = ' '; 
     val = 0; 
     answer = ""; 
     equalsClicked = false; 
     System.out.println("hey"); 
     answerfield.setText(""); 
    } 

    private class Calculation implements ActionListener { 

     public void actionPerformed(ActionEvent event) { 
      JButton x = (JButton) event.getSource(); 
      String text = x.getActionCommand(); 
      System.out.println(text); 
      switch (text) { 
       case "+": 
       case "-": 
       case "*": 
       case "/": 
       case "%": 
        if (temp1 == null) { 
         System.out.println("choose numbers"); 
        } else { 
         if (temp1 != null && temp2 == null) { 
          s = true; 
          operation = text.charAt(0); 
         } 
        } 
        break; 
       case "=": 
        if (temp1 == null) { 
         System.out.println("choose numbers"); 
        } else { 
         if (temp1 != null && temp2 == null) { 
          System.out.println("choose numbers"); 
         } 

        } 
        if (temp1 != null && temp2 != null) { 
         double d = 0.0; 
         double s = 0.0; 
         d = Double.parseDouble(temp1); 
         s = Double.parseDouble(temp2); 
         switch (operation) { 
          case '+': { 
           val = d + s; 
           break; 
          } 
          case '-': { 
           val = d - s; 
           break; 
          } 
          case '*': { 
           val = d * s; 
           break; 
          } 
          case '/': { 
           val = d/s; 
           break; 
          } 
          case '%': { 
           val = d % s; 
           break; 
          } 

         } 
         answer = Double.toString(val); 
         answerfield.setText(answer); 
         System.out.println(answer); 
        } 
        break; 
       case "C": 
        Clear(); 
        break; 
      } 
     } 
    } 

    public static void main(String args[]) { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
        ex.printStackTrace(); 
       } 

       new Calculator("math"); 
      } 
     }); 
    } 
} 
+0

对于这个特定的任务,我不得不将布局设置为空...所以你建议我将代码更改为 – user4216980 2014-11-05 04:06:38

+0

对于这个特定的任务,我必须将布局设置为空...那么你建议我改变我的代码为 – user4216980 2014-11-05 04:08:01

+0

*“对于此特定任务,我必须将布局设置为空”* - 为什么?你可以使用'BorderLayout'和'GridLayout'的组合,或者如果你有挑战,'GridBagLayout' ... – MadProgrammer 2014-11-05 04:09:23