用java
设计一个图形用户界面。界面中包括三个标签、三个文本框和一个按钮。三个标 签分别是【商品1数量】、【商品2数量】、【总量】。按钮的标题为【计算总量】。要求在文本框中输入商品1数量、商品2数量,单击计算总量按钮后在文本框中显示总量。
用swing画窗体,加上几个组件。以及事件处理。 如有帮助,请采纳。
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.JOptionPane;
import javax.swing.JTextField;
public class SummationForm extends JFrame implements ActionListener {
private static final long serialVersionUID = -3059616600875760053L;
JLabel labMathematics, labEnglish, labTotal;
JTextField textMathematics, textEnglish, textTotal;
static JButton butSummation;
public SummationForm() {
labMathematics = new JLabel("商品数量1");
labEnglish = new JLabel("商品数量2");
labTotal = new JLabel("总量");
textMathematics = new JTextField("", 10);
textEnglish = new JTextField("", 10);
textTotal = new JTextField("", 10);
butSummation = new JButton("计算总量");
labMathematics.setBounds(30, 30, 120, 30);
textMathematics.setBounds(180, 30, 120, 30);
labEnglish.setBounds(30, 80, 120, 30);
textEnglish.setBounds(180, 80, 120, 30);
labTotal.setBounds(30, 130, 120, 30);
textTotal.setBounds(180, 130, 120, 30);
butSummation.setBounds(160, 180, 120, 30);
add(labMathematics);
add(textMathematics);
add(labEnglish);
add(textEnglish);
add(labTotal);
add(textTotal);
add(butSummation);
setLayout(null);
setBounds(200, 100, 400, 300);
// 窗口在屏幕中间显示
setLocationRelativeTo(null);
setTitle("求总量");
setResizable(false);
setVisible(true);
}
public static void main(String[] args) {
SummationForm summationForm = new SummationForm();
butSummation.addActionListener(summationForm);
summationForm.add(butSummation);
}
public void actionPerformed(ActionEvent e) {
JButton jb = (JButton) e.getSource();
if (jb == butSummation) {
String Mathematics = textMathematics.getText();
String English = textEnglish.getText();
try {
int a = Integer.parseInt(Mathematics);
int b = Integer.parseInt(English);
int t = a + b;
textTotal.setText(String.valueOf(t));
} catch (Exception e2) {
JOptionPane.showMessageDialog(this, "输入的格式错误!", "提示对话框", JOptionPane.DEFAULT_OPTION);
}
}
}
}
import java.awt.Button;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class Calculator extends JFrame implements ActionListener {
//创建按钮、文本框
Button b1, b2, b3, b4;
JTextField text, text1, res;
public Calculator() {
super("一个简单的加法器");
// 设置布局(设置没有布局,才能自己定位)
setLayout(null);
///操作数1标签
Label uname = new Label("操作数1:");
// 加入窗口中
add(uname);
// 设置组件位置 (组件的x坐标,组件的y坐标,组件的长,组件的高)
uname.setBounds(20, 50, 40, 20);
// 操作数1文本框
text = new JTextField();
add(text);
text.setBounds(65, 50, 200, 20);
// 操作数2标签
Label pwd = new Label("操作数2:");
add(pwd);
pwd.setBounds(20, 80, 40, 20);
// 操作数2文本框
text1 = new JTextField();
add(text1);
text1.setBounds(65, 80, 200, 20);
// 结果文字标签
Label rest = new Label("计算结果:");
add(rest);
rest.setBounds(20, 110, 40, 20);
// 结果文本框
res = new JTextField();
add(res);
res.setBounds(65, 110, 200, 20);
// 按钮1
b1 = new Button("加+");
add(b1);
b1.setBounds(300, 80, 80, 30);
b1.addActionListener(this);
//设置窗口大小
setSize(450, 250);
//设置窗口是否可见,设为false的话看不到窗体
setVisible(true);
//设置窗体居中
setLocationRelativeTo(null);
//点击关闭按钮解除程序
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//main方法,new Calculator对象执行程序
public static void main(String[] args) {
new Calculator();
}
//执行加减乘除方法
public void actionPerformed(ActionEvent e) {
//获取两个操作数文本框内容,转成double格式参与准备运算
double t1 = Double.parseDouble( text.getText() );
double t2 = Double.parseDouble( text1.getText() );
res.setText((t1+t2)+"");
}
}
把文字换成你图片里的直接运行就可以了