用JAVA编写一个简单计算器????界面做出来了。

2024-11-03 01:48:18
推荐回答(2个)
回答1:

import sun.plugin.security.Broken11ClassFixer;

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JFrame;
import java.awt.FlowLayout;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
public class Test extends JFrame{
private JTextField t1;
private JTextField t2;
private JTextField t3;
private JButton b1;
public Test(){
JPanel p1=new JPanel();
JPanel p2=new JPanel();
p1.setLayout(new FlowLayout(FlowLayout.LEFT,4,3));
t1 = new JTextField("0",4);
t2 = new JTextField("0",4);
t3 = new JTextField(8);
b1 = new JButton("=");
p1.add(t1);
p1.add(new JTextField(" + "));
p1.add(t2);
p1.add(new JLabel("="));
p1.add(t3);
p2.add(b1,BorderLayout.CENTER);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
t3.setText(Integer.toString(Integer.parseInt(t1.getText())+Integer.parseInt(t2.getText())));
}
});
add(p1,BorderLayout.NORTH);
add(p2,BorderLayout.SOUTH);
}

public static void main(String[] args){
Test frame=new Test();
frame.setTitle("计算器");
frame.setSize(300,100);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
帮你完善了下,其他来不及写了都是一样的,稍微改一下就行

回答2:

这个行的话,留邮箱我发给你,或者到我空间里有代码!