2016-03-20 7 views
1

私は非常に単純な電卓を作って、ボタンの数(加算、減算、除算、乗算)は数字入力より下にある必要があり、結果は固定位置になります。 pictureには私が持っているべきことが示され、反対側には私が現在持っているものが示されています。 topPanelbuttonsPanel固定された位置にボタンの行をどのようにして取得しますか?

package calculator; 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import java.awt.BorderLayout; 




public class Calculator extends JFrame { 
private JTextField Number1TxtField; 
private JTextField Number2TxtField; 
private JTextField ResultTxtField; 
private JButton add; 
private JButton subtract; 
private JButton multiply; 
private JButton divide; 



public Calculator() { // class for the calculator 




     setLayout(new FlowLayout(FlowLayout.LEFT,8,10)); 
     add(new JLabel("Number 1")); 
     Number1TxtField=new JTextField(8); add(Number1TxtField); 
     add(new JLabel("Number 2")); 
     Number2TxtField=new JTextField(8); 
     Number2TxtField=new JTextField(8); add(Number2TxtField); 
     add(new JLabel("Result")); 
     ResultTxtField= new JTextField(8); add(ResultTxtField); 
     ResultTxtField.setEditable(false); add(ResultTxtField); 




     //new JPanel 
     add = new JButton("Add"); add(add); 

     subtract = new JButton ("Subtract"); add(subtract); 

     multiply = new JButton ("Multiply"); add(multiply); 

     divide = new JButton ("Divide"); add(divide); 


     ButtonListener btnlistener = new ButtonListener(); 

     add.addActionListener(btnlistener); 

     subtract.addActionListener(btnlistener); 

     multiply.addActionListener(btnlistener); 

     divide.addActionListener(btnlistener); 
    } 

class ButtonListener implements ActionListener { 

     @Override 

     public void actionPerformed(ActionEvent e) { 

String num1str = Number1TxtField.getText(); 

double num1 = Double.parseDouble(num1str); 

String num2str = Number2TxtField.getText(); 

double num2 = Double.parseDouble(num2str); 

double result; 

     if (e.getSource() == add) 
      result = num1+num2; 

     else if (e.getSource() == subtract) 
    result = num1-num2; 

     else if (e.getSource() == multiply) 
    result = num1*num2; 

     else //DivideButton 
    result = num1/num2; 


ResultTxtField.setText(String.valueOf(result)); 

    } 
    } 



     public static void main(String[] args) { 



     Calculator frame = new Calculator(); 

     frame.setTitle("Calculator"); 

     frame.setSize(600,120); 

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     frame.setVisible(true); 
    } 
} 

答えて

1

は、二つのパネルを作成します。ボタンをbuttonsPanelに追加し、JFrameCENTERの位置にこのパネルを追加します。次にテキストフィールドをtopPanelに追加し、topPanelJFrameの北の位置に置きます。

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.FlowLayout; 
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 Calculator extends JFrame { 
    private JTextField Number1TxtField; 
    private JTextField Number2TxtField; 
    private JTextField ResultTxtField; 
    private JButton add; 
    private JButton subtract; 
    private JButton multiply; 
    private JButton divide; 

    public Calculator() { // class for the calculator 

     JPanel topPanel = new JPanel(); 

     topPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 8, 5)); 
     topPanel.add(new JLabel("Number 1")); 
     Number1TxtField = new JTextField(5); 
     topPanel.add(Number1TxtField); 
     topPanel.add(new JLabel("Number 2")); 
     Number2TxtField = new JTextField(5); 
     topPanel.add(Number2TxtField); 
     topPanel.add(new JLabel("Result")); 
     ResultTxtField = new JTextField(8); 
     topPanel.add(ResultTxtField); 
     ResultTxtField.setEditable(false); 
     topPanel.add(ResultTxtField); 

     // new JPanel 
     JPanel buttonsPanel = new JPanel(); 
     buttonsPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 8, 5)); 
     add = new JButton("Add"); 
     buttonsPanel.add(add); 

     subtract = new JButton("Subtract"); 
     buttonsPanel.add(subtract); 

     multiply = new JButton("Multiply"); 
     buttonsPanel.add(multiply); 

     divide = new JButton("Divide"); 
     buttonsPanel.add(divide); 

     ButtonListener btnlistener = new ButtonListener(); 

     add.addActionListener(btnlistener); 

     subtract.addActionListener(btnlistener); 

     multiply.addActionListener(btnlistener); 

     divide.addActionListener(btnlistener); 

     getContentPane().add(topPanel, BorderLayout.NORTH); 
     getContentPane().add(buttonsPanel); 
    } 

    class ButtonListener implements ActionListener { 

     @Override 

     public void actionPerformed(ActionEvent e) { 

      String num1str = Number1TxtField.getText(); 

      double num1 = Double.parseDouble(num1str); 

      String num2str = Number2TxtField.getText(); 

      double num2 = Double.parseDouble(num2str); 

      double result; 

      if (e.getSource() == add) 
       result = num1 + num2; 

      else if (e.getSource() == subtract) 
       result = num1 - num2; 

      else if (e.getSource() == multiply) 
       result = num1 * num2; 

      else // DivideButton 
       result = num1/num2; 

      ResultTxtField.setText(String.valueOf(result)); 

     } 
    } 

    public static void main(String[] args) { 

     Calculator frame = new Calculator(); 

     frame.setTitle("Calculator"); 

     frame.pack(); 

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     frame.setVisible(true); 
    } 
} 
関連する問題