用java 基于SWING的图形用户界面设计 编写一个应用程序
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Enumeration;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.plaf.FontUIResource;
public class NewFrame extends JFrame {
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
private int a;
private int b;
private int total = 0;
private int result = 0;
private Random random = new Random();
public NewFrame() {
initComponents();
}
public int getA() {
a = random.nextInt(100);
return a;
}
public void setA(int a) {
this.a = a;
}
public int getB() {
b = random.nextInt(100);
return b;
}
public void setB(int b) {
this.b = b;
}
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;
jTextField1 = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jTextField2 = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
jTextField3 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("加法测试");
getContentPane().setLayout(new java.awt.GridBagLayout());
jTextField1.setColumns(8);
jTextField1.setText(getA() + "");
jTextField1.setEditable(false);
getContentPane().add(jTextField1, new java.awt.GridBagConstraints());
jLabel2.setText("加");
getContentPane().add(jLabel2, new java.awt.GridBagConstraints());
jTextField2.setColumns(8);
jTextField2.setEditable(false);
jTextField2.setText(getB() + "");
jTextField3.addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if (KeyEvent.VK_ENTER == e.getKeyCode()) {
System.out.println("keyPressed");
action();
}
}
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
});
getContentPane().add(jTextField2, new java.awt.GridBagConstraints());
jLabel3.setText("等于");
getContentPane().add(jLabel3, new java.awt.GridBagConstraints());
jTextField3.setColumns(8);
getContentPane().add(jTextField3, new java.awt.GridBagConstraints());
jButton1.setText("确定");
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
action();
}
});
getContentPane().add(jButton1, new java.awt.GridBagConstraints());
jLabel1.setText("总分:0 ");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
getContentPane().add(jLabel1, gridBagConstraints);
pack();
}
private void action() {
try {
if (total >= 99) {
JOptionPane.showMessageDialog(null, "测试结束" + jLabel1.getText(),
"提示", 1);
jTextField3.setText("测试结束");
return;
}
int a = Integer.parseInt(jTextField1.getText());
int b = Integer.parseInt(jTextField2.getText());
int c = Integer.parseInt(jTextField3.getText().trim());
System.out.println(a + " " + b + " " + c);
if (a + b == c) {
result++;
JOptionPane.showMessageDialog(null, "正确", "正确提示", 1);
} else {
JOptionPane.showMessageDialog(null, "错误", "错误提示", 1);
}
total++;
jLabel1.setText("总分:" + result + " ");
jTextField1.setText(getA() + "");
jTextField2.setText(getB() + "");
jTextField3.setText("");
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "输入提示", "请输入数字", 1);
}
}
public static void main(String[] args) {
Font font = new Font("宋体", Font.PLAIN, 12);
for (Enumeration keys = UIManager.getDefaults().keys(); keys
.hasMoreElements();) {
Object key = keys.nextElement();
Object value = UIManager.get(key);
if (value instanceof FontUIResource) {
UIManager.put(key, font);
}
}
NewFrame frame = new NewFrame();
frame.setLocationRelativeTo(null);
frame.setSize(400, 200);
frame.setVisible(true);
}
}
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Enumeration;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.plaf.FontUIResource;
public class NewFrame extends JFrame {
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
private int a;
private int b;
private int total = 0;
private int result = 0;
private Random random = new Random();
public NewFrame() {
initComponents();
}
public int getA() {
a = random.nextInt(100);
return a;
}
public void setA(int a) {
this.a = a;
}
public int getB() {
b = random.nextInt(100);
return b;
}
public void setB(int b) {
this.b = b;
}
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;
jTextField1 = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jTextField2 = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
jTextField3 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("加法测试");
getContentPane().setLayout(new java.awt.GridBagLayout());
jTextField1.setColumns(8);
jTextField1.setText(getA() + "");
jTextField1.setEditable(false);
getContentPane().add(jTextField1, new java.awt.GridBagConstraints());
jLabel2.setText("加");
getContentPane().add(jLabel2, new java.awt.GridBagConstraints());
jTextField2.setColumns(8);
jTextField2.setEditable(false);
jTextField2.setText(getB() + "");
jTextField3.addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if (KeyEvent.VK_ENTER == e.getKeyCode()) {
System.out.println("keyPressed");
action();
}
}
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
});
getContentPane().add(jTextField2, new java.awt.GridBagConstraints());
jLabel3.setText("等于");
getContentPane().add(jLabel3, new java.awt.GridBagConstraints());
jTextField3.setColumns(8);
getContentPane().add(jTextField3, new java.awt.GridBagConstraints());
jButton1.setText("确定");
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
action();
}
});
getContentPane().add(jButton1, new java.awt.GridBagConstraints());
jLabel1.setText("总分:0 ");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
getContentPane().add(jLabel1, gridBagConstraints);
pack();
}
private void action() {
try {
if (total >= 99) {
JOptionPane.showMessageDialog(null, "测试结束" + jLabel1.getText(),
"提示", 1);
jTextField3.setText("测试结束");
return;
}
int a = Integer.parseInt(jTextField1.getText());
int b = Integer.parseInt(jTextField2.getText());
int c = Integer.parseInt(jTextField3.getText().trim());
System.out.println(a + " " + b + " " + c);
if (a + b == c) {
result++;
JOptionPane.showMessageDialog(null, "正确", "正确提示", 1);
} else {
JOptionPane.showMessageDialog(null, "错误", "错误提示", 1);
}
total++;
if (total < 3) {
jLabel1.setText("总分:" + result + " ");
jTextField1.setText(getA() + "");
jTextField2.setText(getB() + "");
jTextField3.setText("");
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "输入提示", "请输入数字", 1);
}
}
public static void main(String[] args) {
Font font = new Font("宋体", Font.PLAIN, 12);
for (Enumeration keys = UIManager.getDefaults().keys(); keys
.hasMoreElements();) {
Object key = keys.nextElement();
Object value = UIManager.get(key);
if (value instanceof FontUIResource) {
UIManager.put(key, font);
}
}
NewFrame frame = new NewFrame();
frame.setLocationRelativeTo(null);
frame.setSize(400, 200);
frame.setVisible(true);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.*;
public class Jsq extends JFrame
{
Panel pan1=new Panel();
Panel pan2=new Panel();
Panel pan3=new Panel();
Panel pan4=new Panel();
Panel pan5=new Panel();
Label la1=new Label("欢迎使用计算器");
Button bt1=new Button(" ");
Button bt2=new Button("MC");
Button bt3=new Button("MR");
Button bt4=new Button("MS");
Button bt5=new Button("M+");
Button bt11=new Button("Backspace");
Button bt12=new Button("CE");
Button bt13=new Button("C");
Button but[]=new Button[20];
String buta[]={"7","8","9","/","sqrt","4","5","6","*","%","1","2","3","-","1/x","0","-/+",".","+","="};
JTextField t1=new JTextField("0");//swing组件,可以设置对齐方式
double a=0;//记录计算中的值或结果
String b="";//“+,-,*,/”运算符号标记
int d=0; //是否刚刚按过运算符号标记
int x=0; //是否刚刚按过“=”号标记
int y=0; //屏幕刚刚出现过汉字的标记
public Jsq()
{
super("计算器");
add(pan1,BorderLayout.NORTH);
add(pan4,BorderLayout.CENTER);
add(pan3,BorderLayout.WEST);
pan3.setLayout(new GridLayout(0,1,10,10));
pan3.add(bt1);pan3.add(bt2);pan3.add(bt3);pan3.add(bt4);pan3.add(bt5);
pan4.setLayout(new BorderLayout());
pan4.add(pan5,BorderLayout.NORTH);
pan4.add(pan2,BorderLayout.CENTER);
pan5.setLayout(new GridLayout(1,3,5,5));
pan5.add(bt11);pan5.add(bt12);pan5.add(bt13);
pan1.setLayout(new GridLayout(3,1));
pan1.add(la1);
pan1.add(t1);
t1.setEditable(false);//文本框设置为不可修改
t1.setHorizontalAlignment(JTextField.RIGHT);//设置文本框对齐方式
pan2.setLayout(new GridLayout(4,5,10,10));
for(int i=0;i<20;i++)
{
but[i]=new Button(buta[i]);
pan2.add(but[i]);
}
but[0].addActionListener(new A());
but[1].addActionListener(new A());
but[2].addActionListener(new A());
but[3].addActionListener(new A());
but[4].addActionListener(new A());
but[5].addActionListener(new A());
but[6].addActionListener(new A());
but[7].addActionListener(new A());
but[8].addActionListener(new A());
but[9].addActionListener(new A());
but[10].addActionListener(new A());
but[11].addActionListener(new A());
but[12].addActionListener(new A());
but[13].addActionListener(new A());
but[14].addActionListener(new A());
but[15].addActionListener(new A());
but[16].addActionListener(new A());
but[17].addActionListener(new A());
but[18].addActionListener(new A());
but[19].addActionListener(new A());
bt11.addActionListener(new A());
bt12.addActionListener(new A());
bt13.addActionListener(new A());
bt1.addActionListener(new A());
bt2.addActionListener(new A());
bt3.addActionListener(new A());
bt4.addActionListener(new A());
bt5.addActionListener(new A());
}
class A implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==but[0]) //"7"
{
if(x==1) //判断是否刚刚按过"="
{
t1.setText("7");
x=0;
}
if(t1.getText().equals("0.0")||t1.getText().equals("0")||d==1||y==1)
{ //如果显示为"0"或"0.0"或者刚刚按过运算符号
t1.setText("7"); //或者屏幕显示为汉字 那么显示为"7"
y=0;//将标记设置为0
}
else
t1.setText(t1.getText()+"7");//将显示的字符串后面加"7"
d=0;//将标记设置为0
if(t1.getText().length()>40)//判断显示字符长度是否超过40
{
t1.setText("错误!数字过长,请按“C”从新输入");
a=0.0;
b="";
d=0;
y=1;
}
}
if(e.getSource()==but[1]) //"8"
{
if(x==1)
{
t1.setText("8");
x=0;
}
if(t1.getText().equals("0.0")||t1.getText().equals("0")||d==1||y==1)
{
t1.setText("8");
y=0;
}
else
t1.setText(t1.getText()+"8");
d=0;
if(t1.getText().length()>40)
{
t1.setText("错误!数字过长,请按“C”从新输入");
a=0.0;
b="";
d=0;
y=1;
}
}
if(e.getSource()==but[2]) //"9"
{
if(x==1)
{
t1.setText("9");
x=0;
}
if(t1.getText().equals("0.0")||t1.getText().equals("0")||d==1||y==1)
{
t1.setText("9");
y=0;
}
else
t1.setText(t1.getText()+"9");
d=0;
if(t1.getText().length()>40)
{
t1.setText("错误!数字过长,请按“C”从新输入");
a=0.0;
b="";
d=0;
y=1;
}
}
if(e.getSource()==but[3]) //"/"
{
if(y==1) //判断显示的是否为汉字
{
t1.setText("0");
y=0;
}
if(d==0) //判断是否刚刚按过运算符号 防止连续按运算符号产生错误
{
if(b.equals("+"))//四则混合运算时 判断上次计算是否为"+"
{
a=a+Double.valueOf(t1.getText()).doubleValue();
t1.setText(""+a);
}
if(b.equals("-"))//四则混合运算时 判断上次计算是否为"-"
{
a=a-Double.valueOf(t1.getText()).doubleValue();
t1.setText(""+a);
}
if(b.equals("*"))//四则混合运算时 判断上次计算是否为"*"
{
a=a*Double.valueOf(t1.getText()).doubleValue();
t1.setText(""+a);
}
if(b.equals("/"))//四则混合运算时 判断上次计算是否为"/"
{
double f=Double.valueOf(t1.getText()).doubleValue();
if(f==0)//判断除数是否为0
{
t1.setText("错误!请按“CE”清屏后继续计算");
y=1;
}
else
{
a=a/Double.valueOf(t1.getText()).doubleValue();
t1.setText(""+a);
}
}
if(b.equals(""))//判断进行的是不是四则混合运算的第一次运算
{
a=Double.valueOf(t1.getText()).doubleValue();
t1.setText(""+a);
}
}
if(!t1.getText().equals("错误!请按“CE”清屏后继续计算"))//是否出现了除数为零的错误
{
b="/";//运算符号标记设置为"/"
d=1; //标记已经按过运算符号
}
if(t1.getText().length()>40)//判断显示字符长度是否超过40
{
t1.setText("错误!数字过长,请按“C”从新输入");
a=0.0;//将记忆的数字抹去
b="";//运算符号标记 为未按过运算符号
d=0;//标记 刚刚没有按过运算符号
y=1;//标记 屏幕显示了汉字
}
}
if(e.getSource()==but[4]) //"sqrt"
{
if(y==1)
{
t1.setText("0");
y=0;
}
double f=Double.valueOf(t1.getText()).doubleValue();
if(f<0) //判断被开放数是否小于零
{
t1.setText("错误!被开方数小于0,请按“C”从新输入");
a=0.0;
b="";
d=0;
y=1;
}
else
{
f=Math.sqrt(f);
t1.setText(""+(f));
}
if(t1.getText().length()>40)
{
t1.setText("错误!数字过长,请按“C”从新输入");
a=0.0;
b="";
d=0;
y=1;
}
}
if(e.getSource()==but[5]) //"4"
{
if(x==1)
{
t1.setText("4");
x=0;
}
if(t1.getText().equals("0.0")||t1.getText().equals("0")||d==1||y==1)
{
t1.setText("4");
y=0;
}
else
t1.setText(t1.getText()+"4");
d=0;
if(t1.getText().length()>40)
{
t1.setText("错误!数字过长,请按“C”从新输入");
a=0.0;
b="";
d=0;
y=1;
}
}
if(e.getSource()==but[6]) //"5"
{
if(x==1)
{
t1.setText("5");
x=0;
}
if(t1.getText().equals("0.0")||t1.getText().equals("0")||d==1||y==1)
{
t1.setText("5");
y=0;
}
else
t1.setText(t1.getText()+"5");
d=0;
if(t1.getText().length()>40)
{
t1.setText("错误!数字过长,请按“C”从新输入");
a=0.0;
b="";
d=0;
y=1;
}
}
if(e.getSource()==but[7]) //"6"
{
if(x==1)
{
t1.setText("6");
x=0;
}
if(t1.getText().equals("0.0")||t1.getText().equals("0")||d==1||y==1)
{
t1.setText("6");
y=0;
}
else
t1.setText(t1.getText()+"6");
d=0;
if(t1.getText().length()>40)
{
t1.setText("错误!数字过长,请按“C”从新输入");
a=0.0;
b="";
d=0;
y=1;
}
}
if(e.getSource()==but[8]) //"*"
{
if(y==1)
{
t1.setText("0");
y=0;
}
if(d==0)
{
if(b.equals("+"))
{
a=a+Double.valueOf(t1.getText()).doubleValue();
t1.setText(""+a);
}
if(b.equals("-"))
{
a=a-Double.valueOf(t1.getText()).doubleValue();
t1.setText(""+a);
}
if(b.equals("*"))
{
a=a*Double.valueOf(t1.getText()).doubleValue();
t1.setText(""+a);
}
if(b.equals("/"))
{
double f=Double.valueOf(t1.getText()).doubleValue();
if(f==0)
{
t1.setText("错误!请按“CE”清屏后继续计算");
y=1;
}
else
{
a=a/Double.valueOf(t1.getText()).doubleValue();
t1.setText(""+a);
}
}
if(b.equals(""))
{
a=Double.valueOf(t1.getText()).doubleValue();
t1.setText(""+a);
}
}
if(!t1.getText().equals("错误!请按“CE”清屏后继续计算"))
{
b="*";
d=1;
}
if(t1.getText().length()>40)
{
t1.setText("错误!数字过长,请按“C”从新输入");
a=0.0;
b="";
d=0;
y=1;
}
}
if(e.getSource()==but[9]) //"%"
{
if(y==1)
{
t1.setText("0");
y=0;
}
double f=Double.valueOf(t1.getText()).doubleValue();
t1.setText(""+(f/100));
if(t1.getText().length()>40)
{
t1.setText("错误!数字过长,请按“C”从新输入");
a=0.0;
b="";
d=0;
y=1;
}
}
if(e.getSource()==but[10]) //"1"
{
if(x==1)
{
t1.setText("1");
x=0;
}
if(t1.getText().equals("0.0")||t1.getText().equals("0")||d==1||y==1)
{
t1.setText("1");
y=0;
}
else
t1.setText(t1.getText()+"1");
d=0;
if(t1.getText().length()>40)
{
t1.setText("错误!数字过长,请按“C”从新输入");
a=0.0;
b="";
d=0;
y=1;
}
}
if(e.getSource()==but[11]) //"2"
{
if(x==1)
{
t1.setText("2");
x=0;
}
if(t1.getText().equals("0.0")||t1.getText().equals("0")||d==1||y==1)
{
t1.setText("2");
y=0;
}
else
t1.setText(t1.getText()+"2");
d=0;
if(t1.getText().length()>40)
{
t1.setText("错误!数字过长,请按“C”从新输入");
a=0.0;
b="";
d=0;
y=1;
}
}
if(e.getSource()==but[12]) //"3"
{
if(x==1)
{
t1.setText("3");
x=0;
}
if(t1.getText().equals("0.0")||t1.getText().equals("0")||d==1||y==1)
{
t1.setText("3");
y=0;
}
else
t1.setText(t1.getText()+"3");
d=0;
if(t1.getText().length()>40)
{
t1.setText("错误!数字过长,请按“C”从新输入");
a=0.0;
b="";
d=0;
y=1;
}
}
if(e.getSource()==but[13]) //"-"
{
if(y==1)
{
t1.setText("0");
y=0;
}
if(d==0)
{
if(b.equals("+"))
{
a=a+Double.valueOf(t1.getText()).doubleValue();
t1.setText(""+a);
}
if(b.equals("-"))
{
a=a-Double.valueOf(t1.getText()).doubleValue();
t1.setText(""+a);
}
if(b.equals("*"))
{
a=a*Double.valueOf(t1.getText()).doubleValue();
t1.setText(""+a);
}
if(b.equals("/"))
{
double f=Double.valueOf(t1.getText()).doubleValue();
if(f==0)
{
t1.setText("错误!请按“CE”清屏后继续计算");
y=1;
}
else
{
a=a/Double.valueOf(t1.getText()).doubleValue();
t1.setText(""+a);
}
}
if(b.equals(""))
{
a=Double.valueOf(t1.getText()).doubleValue();
t1.setText(""+a);
}
}
if(!t1.getText().equals("错误!请按“CE”清屏后继续计算"))
{
b="-";
d=1;
}
if(t1.getText().length()>40)
{
t1.setText("错误!数字过长,请按“C”从新输入");
a=0.0;
b="";
d=0;
y=1;
}
}
if(e.getSource()==but[14]) //"1/x"
{
if(y==1)
{
t1.setText("0");
y=0;
}
else
{
double f=Double.valueOf(t1.getText()).doubleValue();
t1.setText(""+(1/f));
if(t1.getText().length()>40)
{
t1.setText("错误!数字过长,请按“C”从新输入");
a=0.0;
b="";
d=0;
y=1;
}
}
}
if(e.getSource()==but[15]) //"0"
{
if(x==1)
{
t1.setText("0");
x=0;
}
if(t1.getText().equals("0.0")||t1.getText().equals("0")||d==1||y==1)
{
t1.setText("0");
y=0;
}
else
t1.setText(t1.getText()+"0");
d=0;
if(t1.getText().length()>40)
{
t1.setText("错误!数字过长,请按“C”从新输入");
a=0.0;
b="";
d=0;
y=1;
}
}
if(e.getSource()==but[16]) //"-/+"
{
if(y==1)
{
t1.setText("0");
y=0;
}
double f=Double.valueOf(t1.getText()).doubleValue();
t1.setText(""+(-f));
if(t1.getText().length()>40)
{
t1.setText("错误!数字过长,请按“C”从新输入");
a=0.0;
b="";
d=0;
y=1;
}
}
if(e.getSource()==but[17]) //"."
{
if(y==1)
{
t1.setText("0");
y=0;
}
if(t1.getText().indexOf(".")==-1)//判断是否已经有"."
{
t1.setText(t1.getText()+".");
}
if(t1.getText().length()>40)
{
t1.setText("错误!数字过长,请按“C”从新输入");
a=0.0;
b="";
d=0;
y=1;
}
}
if(e.getSource()==but[18]) //"+"
{
if(y==1)
{
t1.setText("0");
y=0;
}
if(d==0)
{
if(b.equals("+"))
{
a=a+Double.valueOf(t1.getText()).doubleValue();
t1.setText(""+a);
}
if(b.equals("-"))
{
a=a-Double.valueOf(t1.getText()).doubleValue();
t1.setText(""+a);
}
if(b.equals("*"))
{
a=a*Double.valueOf(t1.getText()).doubleValue();
t1.setText(""+a);
}
if(b.equals("/"))
{
double f=Double.valueOf(t1.getText()).doubleValue();
if(f==0)
{
t1.setText("错误!请按“CE”清屏后继续计算");
y=1;
}
else
{
a=a/Double.valueOf(t1.getText()).doubleValue();
t1.setText(""+a);
}
}
if(b.equals(""))
{
a=Double.valueOf(t1.getText()).doubleValue();
t1.setText(""+a);
}
}
if(!t1.getText().equals("错误!请按“CE”清屏后继续计算"))
{
b="+";
d=1;
}
if(t1.getText().length()>40)
{
t1.setText("错误!数字过长,请按“C”从新输入");
a=0.0;
b="";
d=0;
y=1;
}
}
if(e.getSource()==but[19]) //"="
{
if(y==1)
{
t1.setText("0");
y=0;
}
if(b.equals("+"))
{
a=a+Double.valueOf(t1.getText()).doubleValue();
t1.setText(""+a);
}
if(b.equals("-"))
{
a=a-Double.valueOf(t1.getText()).doubleValue();
t1.setText(""+a);
}
if(b.equals("*"))
{
a=a*Double.valueOf(t1.getText()).doubleValue();
t1.setText(""+a);
}
if(b.equals("/"))
{
double f=Double.valueOf(t1.getText()).doubleValue();
if(f==0)
{
t1.setText("错误!请按“CE”清屏后继续计算");
y=1;
}
else
{
a=a/Double.valueOf(t1.getText()).doubleValue();
t1.setText(""+a);
}
}
if(b.equals(""))
{
a=Double.valueOf(t1.getText()).doubleValue();
t1.setText(""+a);
}
if(!t1.getText().equals("错误!请按“CE”清屏后继续计算"))
{
b="";
d=1;
x=1;
}
if(t1.getText().length()>40)
{
t1.setText("错误!数字过长,请按“C”从新输入");
a=0.0;
b="";
d=0;
y=1;
}
}
if(e.getSource()==bt11) //"Backspace"
{
if(y==1)
{
t1.setText("0");
y=0;
}
else
{
String s1=t1.getText();
if(s1.length()==1)//如果将显示的数字全部退掉 则显示"0"
{
t1.setText("0");
}
else
{
t1.setText(s1.substring(0,(s1.length()-1)));//退格
}
}
}
if(e.getSource()==bt12) //"CE"
{
t1.setText("0");//清屏
}
if(e.getSource()==bt13) //"C" 初始化
{
t1.setText("0");
a=0.0;
b="";
d=0;
}
if(e.getSource()==bt1) //""
{
}
if(e.getSource()==bt2) //"MC"
{
}
if(e.getSource()==bt3) //"MR"
{
}
if(e.getSource()==bt4) //"MS"
{
}
if(e.getSource()==bt5) //"M+"
{
}
}
}
public static void main(String args[])
{
Jsq a=new Jsq();
a.setSize(300,250);
a.setVisible(true);
}
}
package net.pzw;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Test extends JFrame{
private static final long serialVersionUID = 1L;
JFrame frame=new JFrame();
JPanel panel=new JPanel();
JTextField field1=new JTextField(5);
JTextField field2=new JTextField(5);
JTextField field3=new JTextField(5);
JButton button1=new JButton("+");
JButton button2=new JButton("-");
JButton button3=new JButton("*");
JButton button4=new JButton("/");
public Test() {
panel.add(field1);
panel.add(field2);
panel.add(field3);
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(button4);
frame.add(panel);
frame.setSize(450,80);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
button1.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
int x=Integer.parseInt(field1.getText())+Integer.parseInt(field2.getText());
field3.setText(Integer.toString(x));
}
});
button2.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
int x=Integer.parseInt(field1.getText())-Integer.parseInt(field2.getText());
field3.setText(Integer.toString(x));
}
});
button3.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
int x=Integer.parseInt(field1.getText())*Integer.parseInt(field2.getText());
field3.setText(Integer.toString(x));
}
});
button4.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
int x=Integer.parseInt(field1.getText())/Integer.parseInt(field2.getText());
field3.setText(Integer.toString(x));
}
});
}
public static void main(String[] args) {
new Test();
}
}
30分就想让别人帮你写程序???虽然很简单,但是没人有那闲心。
甘诚卡罗: import javax.swing.JOptionPane; public class Test { static int answer, x, y, score = 0; static String generate() { x = (int) (Math.random() * 100); y = (int) (Math.random() * 100); answer = x + y; return "请问" + x + "+" + y + "=?"; } public static void ...
吉木乃县19849397173: 请简要说明Java基于SWING的图形用户界面设计中的事件机制及处理事件的步骤 谢谢了,简要 - ?
甘诚卡罗: 在编程语言中的事件就是当某组件的状态发生改变的时候通知其它对象发生了这件事,我们在这里只讨论图形界面的组件,有可能是按钮、文本框、菜单、多选等等. 对于通知其它对象的方法一般有两种模式: 1)推模式:状态改变的对象通知...
吉木乃县19849397173: java的swing组件的使用 - ?
甘诚卡罗: Swing是用于图形用户界面开发的一个全面解决方案,包括250多个类,在这里不能一一列出.本书仅使用Swing用户界面. 1.标 签 2.按 钮 3.文本域 4.文本区 5.列表框 6.组合框 7.复选框 8.单选按钮 9.滚动条 10.选项卡 11.对话框 12.菜 单 1.标 签 ...
吉木乃县19849397173: 求一个java程序 基于Swing的图形用户界面设计 - ?
甘诚卡罗: import java.awt.Font;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;i...
吉木乃县19849397173: java 中swing如何绘制图形 ? - ?
甘诚卡罗: 这是不可能你的,你只能算出矩形四个点的坐标,然后再递归的一个一个画.
吉木乃县19849397173: 用java编写一个图形化界面小程序,swing窗体定义4个标签对象,分别是:用户名、输入料号、扫描条码等. - ?
甘诚卡罗: 用java编写一个图形化界面小程序,swing窗体定义4个标签对象,分别是:用户名、输入料号数量、扫描条码等.另外定义两个按钮对象(确定、取消) 上面这些你肯定会的 至于 用户点击确定按钮功能之后把以上数据(用户名、料号数量、条码、还有用户操作日期)统计在excel表中 如果本地测试的话,就新建一个excel,可以搜索相关java操作excel的代码,有jxl和poi,首先查找设定的excel名字,如果没有创建一个;还可以保存到数据库,提供实时导出excel
吉木乃县19849397173: 用java画图swing 画出各种几何图形 - ?
甘诚卡罗: 多数图形都在java.awt.geom包里实现了,都是实现Shape接口,可以使用Graphics2D的draw(Shape s)方法绘制.
吉木乃县19849397173: 用java图形界面swing做一个简单的收银系统,怎么实现类似append的效果 - ?
甘诚卡罗: 首先应该采用原型模式 把每一行的对象 定一个原型,每次增加行的之后 复制对象 然后把这些个商品信息表格 放到一个panel里面,这个panel可以采用网格布局,每次都往panel里面添加,然后再次刷新panel 这样就直接在panel的父视图产生了你说的那种效果
吉木乃县19849397173: 用java Swing图形界面怎样添加背景图片而不覆盖文字??? - ?
甘诚卡罗: 首先继承JPanel类,然后重写paintComponent方法 给你个例子,这是重写的方法 public void paintComponent(Graphics gs) { Graphics2D g = (Graphics2D) gs; super.paintComponent(g); //画背景图片 g.drawImage(gameBG.getImage(), 0, 0, 800, 560, this); } 记住,要先画图片,再画文字,否则会被覆盖
吉木乃县19849397173: 求高手用java SWING实现下图结构 - ?
甘诚卡罗: 这是服务器端:import java.awt.BorderLayout; import java.awt.Container; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.net.Socket; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import ...