简体   繁体   中英

java Calculator Text Field

I have to create a simple calculate for an exercise. There should be 4 buttons and a text field as a display. Here is my code for the 4 buttons:

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

class JFrameExample {

  JButton btn1 = new JButton("1");
  JButton btn2 = new JButton("2");
  JButton btn3 = new JButton("+");
  JButton btn4 = new JButton("=");

  public void calculator(){
    JFrame frame = new JFrame("SIMPLE JAVA CALCULATOR");
    frame.setSize(320,320);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(false);
    frame.setVisible(true);

    JPanel HeadPanel = new JPanel();
    JPanel NumberPanel = new JPanel();
    JPanel LabelPanel = new JPanel();

    NumberPanel.add(btn1);  
    NumberPanel.add(btn2);
    NumberPanel.add(btn3);
    NumberPanel.add(btn4);

    NumberPanel.setLayout(new GridLayout(2,2));
    frame.add(NumberPanel,BorderLayout.CENTER);
  }

  public static void main(String[] args) {
    JFrameExample jf = new JFrameExample();
    jf.calculator();
  }
}

My problem is I have no idea on how to insert the text field. I tried some methods from the internet but no result. Can you guys give me a solution? How should I do that? At least link to a tutorial how to do it.

You will need a JTextField. You can set and get the text content by calling setText() and getText() .

I would go for:

JPanel (BorderLayout)
   |
   +-- JPanel (GridLayout), BorderLayout.SOUTH
   |    |
   |    + Your buttons
   |
   +-- JTextField, BorderLayout.NORTH

Drag the JTextField on to the form .

Now use the jtextFieldObject.setText() method to set the values in it . jtextFieldObject..getText() method to retrieve the text inside the text field .

This might be helpful. And you must have basic knowledge about different components and its method before using it. So it's better to refer some basic tutorial first instead of direct implementing it.

hope this will help you

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

public class zzzz {
JFrame f;
JButton one,two,three,four,five,six,seven,eight,nine,zero,point,equal,plus,minus,divide,multiply;
Container c;
JTextField j;

public zzzz(){
f=new JFrame("Calculator GUI");

f.setSize(250, 220);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setResizable(false);


c=f.getContentPane();

Dimension d=new Dimension(42,25);
Dimension d1=new Dimension(220,35);
Dimension d2=new Dimension(165,200);
Dimension d3=new Dimension(45,200);
Dimension d4=new Dimension(200,30);

FlowLayout fl=new FlowLayout(FlowLayout.CENTER);



one=new JButton("1");
two=new JButton("2");
three=new JButton("3");
four=new JButton("4");
five=new JButton("5");
six=new JButton("6");
seven=new JButton("7");
eight=new JButton("8");
nine=new JButton("9");
zero=new JButton("0");
point=new JButton(".");
equal=new JButton("=");

plus=new JButton("+");
minus=new JButton("-");
divide=new JButton("/");
multiply=new JButton("x");

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

JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();

p1.setLayout(new FlowLayout(FlowLayout.RIGHT));
p2.setLayout(new FlowLayout(FlowLayout.CENTER));
p3.setLayout(new FlowLayout(FlowLayout.CENTER));

p1.setPreferredSize(d1);
p2.setPreferredSize(d2);
p3.setPreferredSize(d3);


p1.setBackground(Color.BLACK);
p2.setBackground(Color.BLACK);
p3.setBackground(Color.BLACK);


j.setPreferredSize(d4);
one.setPreferredSize(d);
two.setPreferredSize(d);
three.setPreferredSize(d);
four.setPreferredSize(d);
five.setPreferredSize(d);
six.setPreferredSize(d);
seven.setPreferredSize(d);
eight.setPreferredSize(d);
nine.setPreferredSize(d);
zero.setPreferredSize(d);
point.setPreferredSize(d);
equal.setPreferredSize(d);
plus.setPreferredSize(d);
minus.setPreferredSize(d);
divide.setPreferredSize(d);
multiply.setPreferredSize(d);

p1.add(j);
p2.add(seven);
p2.add(eight);
p2.add(nine);
p3.add(divide);
p2.add(four);   
p2.add(five);
p2.add(six);
p3.add(multiply);
p2.add(one);
p2.add(two);
p2.add(three);
p3.add(minus);
p2.add(zero);
p2.add(point);
p2.add(equal);
p3.add(plus);

c.setLayout(fl);
c.setBackground(Color.BLACK);

c.add(p1);
c.add(p2);
c.add(p3);

f.setVisible(true);
}
public static void main(String[] args) {
new zzzz();

}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM