2016-03-28 70 views
0

我想弄清楚如何在當前代碼中實現幾個按鈕,但經過多次嘗試後,我似乎無法弄清楚。我需要執行以下操作:Java gui鞦韆計算器運營商

所以我的問題是:當我將2個數字加在一起,如果它們不是0(例如x = 10和y = 10),當我點擊加號按鈕時, y字段消失了,我該如何解決這個問題?

此外,我清除按鈕不清除x,y和結果字段中的所有內容,我該如何解決這個問題?

如何正確對齊我的x和y字段?

package democalc; 

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.GridLayout; 
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; 
import javax.swing.border.EtchedBorder; 


class Calculator implements ActionListener { 

    private JFrame frame; 
    private JTextField xfield, yfield; 
    private JLabel rslt; 
    private JButton multiButton, clearButton, addButton, subButton, divideButton; 
    private JPanel xpanel, buttonPanel; 

    public Calculator() { 

     frame = new JFrame(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setLayout(new BorderLayout()); 

     xpanel = new JPanel(); 
     xpanel.setBorder(new EtchedBorder()); 
     xpanel.setLayout(new GridLayout(3, 2)); 

     xpanel.add(new JLabel("x =")); 
     xfield = new JTextField("0", 5); 
     xpanel.add(xfield); 

     xpanel.add(new JLabel("y =")); 
     yfield = new JTextField("0", 5); 
     xpanel.add(yfield); 

     xpanel.add(new JLabel()); 
     rslt = new JLabel("0"); 
     rslt.setBackground(Color.green); 
     rslt.setBorder(new EtchedBorder()); 
     rslt.setOpaque(true); 
     xpanel.add(rslt); 
     frame.add(xpanel, BorderLayout.NORTH); 

     buttonPanel = new JPanel(); 
     frame.add(buttonPanel, BorderLayout.CENTER); 

     buttonPanel.add(addButton = new JButton("+")); 
     addButton.addActionListener(this); 

     buttonPanel.add(subButton = new JButton("-")); 
     subButton.addActionListener(this); 

     buttonPanel.add(multiButton = new JButton("*")); 
     multiButton.addActionListener(this); 

     buttonPanel.add(divideButton = new JButton("/")); 
     divideButton.addActionListener(this); 

     buttonPanel.add(clearButton = new JButton("Clear")); 
     clearButton.addActionListener(this); 

     frame.pack(); 
     frame.setVisible(true); 
    } 

    public void actionPerformed(ActionEvent event) { 

     int x = 0, y = 0; 

     try { 
      String xStr = xfield.getText(); 
      x = Integer.parseInt(xStr); 
     } 
     catch (NumberFormatException e) { 
       // The string xStr is not a legal number. 
      rslt.setText("Illegal data for x."); 
      xfield.requestFocusInWindow(); 
      return; 
     } 

     /* Get a number from yfield in the same way. */ 

     try { 
      String yStr = yfield.getText(); 
      y = Integer.parseInt(yStr); 
     } 
     catch (NumberFormatException e) { 
      rslt.setText("Illegal data for y."); 
      yfield.requestFocusInWindow(); 
      return; 
     } 

     /* Perform the operation based on the action command 
      from the button. Note that division by zero produces 
      an error message. */ 

     String op = event.getActionCommand(); 
     if (op.equals("+")) 
      rslt.setText("x + y = " + (x+y)); 
     else if (op.equals("-")) 
      rslt.setText("x - y = " + (x-y)); 
     else if (op.equals("*")) 
      rslt.setText("x * y = " + (x*y)); 
     else if (op.equals("/")) { 
      if (y == 0) 
       rslt.setText("ERROR"); 
      else 
       rslt.setText("x/y = " + (x/y)); 


     } 

     if (event.getSource() == clearButton) 
      xfield.setText("0"); 
      yfield.setText("0"); 


    } 
} 
+3

您的問題是? –

+0

我已更新我的問題和代碼 – user3325133

+1

*「我已更新我的問題」*在該說明中,SO是問答網站,而不是幫助臺。理想情況下,每個問題線程應該有一個明確的,具體的問題。其他問題需要分解成單獨的問題線索。 –

回答

1

如果大括號缺失。那就是爲什麼

yfield.setText("0"); 

每次都在執行。 用正確代碼替換您的代碼

if (event.getSource() == clearButton){ 
    xfield.setText("0"); 
    yfield.setText("0"); 
}