用java的swing写了个计算器界面,但是数据结构忘光了,功能代码写不出来,不用数据结构真的想不出来唉?,把界面代码发出来,以后把功能补上。
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.accessibility.Accessible;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
//import protext.protext;
public class Windons extends JFrame implements ActionListener {
JButton one = new JButton("1");
JButton two =new JButton("2");
JButton three =new JButton("3");
JButton four =new JButton("4");
JButton five =new JButton("5");
JButton six =new JButton("6");
JButton senven =new JButton("7");
JButton eight =new JButton("8");
JButton nine =new JButton("9");
JButton zero =new JButton("0");
JButton reduce =new JButton("--");//减
JButton plus =new JButton("+"); //加
JButton ride =new JButton("*"); //乘
JButton except =new JButton("/");//除
JButton result =new JButton("=");//等于
JTextField field =new JTextField();
int k2;
static int k1;
int k3=0;
int a[] =new int[10];
int i=0;
int max=0;
int min=0;
public Windons() {
JPanel jp=new JPanel();
jp.setLayout(null);
field.setBounds(0, 0, 500, 100);
one.setBounds(0, 100, 100, 100);
two.setBounds(100, 100, 100, 100);
three.setBounds(200, 100, 100, 100);
four.setBounds(0, 200, 100, 100);
five.setBounds(100, 200, 100, 100);
six.setBounds(200, 200, 100, 100);
senven.setBounds(0, 300, 100, 100);
eight.setBounds(100, 300, 100, 100);
nine.setBounds(200, 300, 100, 100);
reduce.setBounds(300, 100, 100, 100);
plus.setBounds(400, 100, 100, 100);
ride.setBounds(300, 200, 100, 100);
except.setBounds(400, 200, 100, 100);
result.setBounds(400, 300, 100, 100);
zero.setBounds(300, 300, 100, 100);
jp.add(field);
jp.add(one);
jp.add(two);
jp.add(three);
jp.add(four);
jp.add(five);
jp.add(six);
jp.add(senven);
jp.add(eight);
jp.add(nine);
jp.add(zero);
jp.add(reduce);
jp.add(plus);
jp.add(result);
jp.add(ride);
jp.add(except);
one.addActionListener(this);
two.addActionListener(this);
three.addActionListener(this);
four.addActionListener(this);
five.addActionListener(this);
six.addActionListener(this);
senven.addActionListener(this);
eight.addActionListener(this);
nine.addActionListener(this);
zero.addActionListener(this);
reduce.addActionListener(this);
plus.addActionListener(this);
ride.addActionListener(this);
result.addActionListener(this);
except.addActionListener(this);
this.add(jp,BorderLayout.CENTER);
this.setLocationRelativeTo(null);
this.setTitle("计算器");
this.setSize(506, 435);
this.setVisible(true);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//输入框事件监听,只让其显示数字
field.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e)
{
System.out.println(e);
char code = e.getKeyChar();
if (!(code >= KeyEvent.VK_0 && code <= KeyEvent.VK_9))
{
e.consume();
}
}
});
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==one)
{
field.setText(field.getText()+"1");
}
else if(e.getSource()==two)
{
field.setText(field.getText()+"2");
}
else if(e.getSource()==three)
{
field.setText(field.getText()+"3");
}
else if(e.getSource()==four)
{
field.setText(field.getText()+"4");
}
else if(e.getSource()==five)
{
field.setText(field.getText()+"5");
}
else if(e.getSource()==six)
{
field.setText(field.getText()+"6");
}
else if(e.getSource()==senven)
{
field.setText(field.getText()+"7");
}
else if(e.getSource()==eight)
{
field.setText(field.getText()+"8");
}
else if(e.getSource()==nine)
{
field.setText(field.getText()+"9");
}
else if(e.getSource()==zero)
{
field.setText(field.getText()+"0");
}
else if(e.getSource()==reduce)
{
field.setText(field.getText()+"-");
}
else if(e.getSource()==plus)
{
field.setText(field.getText()+"+");
}
else if(e.getSource()==ride)
{
field.setText(field.getText()+"*");
}
else if(e.getSource()==except)
{
field.setText(field.getText()+"/");
}
else if(e.getSource()==result)
{
System.out.println(field.getText());
field.setText(field.getText()+"=");
// System.out.println(Integer.parseInt(field.getText().substring(0, a[0]))+field.getText().charAt(a[0])+3);
}
}
public static void main(String[] args) {
new Windons();
}
}