JAVA创建用户界面的简单应用例子 5
创建一个窗口(1)窗口有“退出”菜单,选择后可退出程序(2)窗口内有“提示”按钮,单击后显示自己的班级、学号、姓名(3)“退出”按钮,单击后可退出程序(4)在窗口内实现一...
创建一个窗口
(1)窗口有“退出”菜单,选择后可退出程序
(2) 窗口内有“提示”按钮,单击后显示自己的班级、学号、姓名
(3) “退出”按钮,单击后可退出程序
(4)在窗口内实现一个计算器,可进行简单的加、减、乘、除操作
能把程序发给我xiaodeqiang2010@qq.com吗?谢谢!急用! 展开
(1)窗口有“退出”菜单,选择后可退出程序
(2) 窗口内有“提示”按钮,单击后显示自己的班级、学号、姓名
(3) “退出”按钮,单击后可退出程序
(4)在窗口内实现一个计算器,可进行简单的加、减、乘、除操作
能把程序发给我xiaodeqiang2010@qq.com吗?谢谢!急用! 展开
2个回答
展开全部
已发。
import java.awt.*;
import java.awt.event.*;
import javax.management.loading.PrivateClassLoader;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import com.sun.org.apache.bcel.internal.generic.NEW;
class Plus extends JFrame{
private JTextField t1=new JTextField(11);
private JTextField t2=new JTextField(11);
private JTextField snoField=new JTextField(10);
private JTextField nameField=new JTextField(10);
private JComboBox comboBox=new JComboBox();//Label l=new Label("+");
private JButton button=new JButton("=");
private JButton button2=new JButton("提示");
private JTextField t3=new JTextField(11);
public Plus(){
setLayout(new BorderLayout());
JPanel jPanel1=new JPanel();
jPanel1.add(new JLabel("学号:"));
jPanel1.add(snoField);
jPanel1.add(new JLabel("姓名:"));
jPanel1.add(nameField);
jPanel1.add(button2);
button2.addActionListener(new MyAL());
add(jPanel1,BorderLayout.NORTH);
JPanel jPanel=new JPanel();
comboBox.addItem("+");
comboBox.addItem("-");
comboBox.addItem("*");
comboBox.addItem("/");
button.addActionListener(new MyAL());
addWindowListener(new MyWL());
jPanel.add(t1);
jPanel.add(comboBox);
jPanel.add(t2);
jPanel.add(button);
jPanel.add(t3);
jPanel.setVisible(true);
add(jPanel,BorderLayout.SOUTH);
setVisible(true);
setBounds(400,200,500, 150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private class MyWL extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
private class MyAL implements ActionListener{
public void actionPerformed(ActionEvent e){
String string=e.getActionCommand();
if("提示".equals(string)){
snoField.setText("你的学号");
nameField.setText("你的姓名");
}else{
String op=(String)comboBox.getSelectedItem();
try{
String temp1=t1.getText();
String temp2=t2.getText();
double d1 = Double.parseDouble(temp1);
double d2 = Double.parseDouble(temp2);
String s="";
if("+".equals(op))
s=String.valueOf(d1+d2);
if("-".equals(op))
s=String.valueOf(d1-d2);
if("*".equals(op))
s=String.valueOf(d1*d2);
if("/".equals(op))
s=String.valueOf(d1/d2);
t3.setText(s);
}
catch(NumberFormatException f){
t3.setText("输入有误");
}
}
}
}
}
public class PlusTest {
public static void main(String[] args) {
new Plus();
}
}
有什么疑问可hi我
退出按钮已加上
import java.awt.*;
import java.awt.event.*;
import javax.management.loading.PrivateClassLoader;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import com.sun.org.apache.bcel.internal.generic.NEW;
class Plus extends JFrame{
private JTextField t1=new JTextField(11);
private JTextField t2=new JTextField(11);
private JTextField snoField=new JTextField(10);
private JTextField nameField=new JTextField(10);
private JComboBox comboBox=new JComboBox();//Label l=new Label("+");
private JButton button=new JButton("=");
private JButton button2=new JButton("提示");
private JTextField t3=new JTextField(11);
public Plus(){
setLayout(new BorderLayout());
JPanel jPanel1=new JPanel();
jPanel1.add(new JLabel("学号:"));
jPanel1.add(snoField);
jPanel1.add(new JLabel("姓名:"));
jPanel1.add(nameField);
jPanel1.add(button2);
button2.addActionListener(new MyAL());
add(jPanel1,BorderLayout.NORTH);
JPanel jPanel=new JPanel();
comboBox.addItem("+");
comboBox.addItem("-");
comboBox.addItem("*");
comboBox.addItem("/");
button.addActionListener(new MyAL());
addWindowListener(new MyWL());
jPanel.add(t1);
jPanel.add(comboBox);
jPanel.add(t2);
jPanel.add(button);
jPanel.add(t3);
jPanel.setVisible(true);
add(jPanel,BorderLayout.SOUTH);
setVisible(true);
setBounds(400,200,500, 150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private class MyWL extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
private class MyAL implements ActionListener{
public void actionPerformed(ActionEvent e){
String string=e.getActionCommand();
if("提示".equals(string)){
snoField.setText("你的学号");
nameField.setText("你的姓名");
}else{
String op=(String)comboBox.getSelectedItem();
try{
String temp1=t1.getText();
String temp2=t2.getText();
double d1 = Double.parseDouble(temp1);
double d2 = Double.parseDouble(temp2);
String s="";
if("+".equals(op))
s=String.valueOf(d1+d2);
if("-".equals(op))
s=String.valueOf(d1-d2);
if("*".equals(op))
s=String.valueOf(d1*d2);
if("/".equals(op))
s=String.valueOf(d1/d2);
t3.setText(s);
}
catch(NumberFormatException f){
t3.setText("输入有误");
}
}
}
}
}
public class PlusTest {
public static void main(String[] args) {
new Plus();
}
}
有什么疑问可hi我
退出按钮已加上
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询