大一java程序设计问题!!!
1.输入两个整数,分别进行加、减、乘、除等算术运算,界面如下图所示。要求:1)采用Swing组件设计图形用户界面,采用3个文本行JTextField分别表示两个操作数和运...
1.输入两个整数,分别进行加、减、乘、除等算术运算,界面如下图所示。要求:1) 采用Swing组件设计图形用户界面,采用3个文本行JTextField分别表示两个操作数和运算结果,其中表示运算结果的文本行只能显示,不能输入;采用组合框JComboBox提供多种算数运算符的选择方式,组合框只能在下拉列表中选择,不能编辑。2) 文本行要响应单击事件,组合框要响应选择事件。两个事件处理方法执行由组合框指定的算术运算。3) 进行异常处理。当文本行中的字符串不能转换成整数时,弹出对话框显示指定信息,提示用户重新输入。
兄弟们不好意思,没财富值了。。。。不能悬赏了 展开
兄弟们不好意思,没财富值了。。。。不能悬赏了 展开
2个回答
展开全部
import java.awt.BorderLayout;
public class demo extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JLabel lblNewLabel;
private JTextField textField_2;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
demo frame = new demo();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public demo() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 75);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textField = new JTextField();
textField.setBounds(10, 10, 91, 21);
contentPane.add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setBounds(176, 10, 91, 21);
contentPane.add(textField_1);
textField_1.setColumns(10);
textField_2 = new JTextField();
textField_2.setBounds(335, 10, 76, 21);
contentPane.add(textField_2);
textField_2.setColumns(10);
JComboBox comboBox = new JComboBox();
comboBox.setModel(new DefaultComboBoxModel(new String[] {"+","-","*","/"}));
comboBox.setBounds(124, 10, 32, 21);
contentPane.add(comboBox);
comboBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
int stateChange = e.getStateChange();
if(stateChange == 1){
try {
int text1 = Integer.parseInt(textField.getText());
int text2 = Integer.parseInt(textField_1.getText());
int text3=0;
if("+".equals(e.getItem())) text3 = text1 + text2;
if("-".equals(e.getItem())) text3 = text1 - text2;
if("*".equals(e.getItem())) text3 = text1 * text2;
if("/".equals(e.getItem())) text3 = text1 / text2;
textField_2.setText(text3+"");
} catch (Exception e2) {
JOptionPane.showMessageDialog(null, "错误!!"+e2.getMessage());
}
}
}
});
lblNewLabel = new JLabel("=");
lblNewLabel.setBounds(284, 13, 24, 15);
contentPane.add(lblNewLabel);
}
}
追问
为啥我运行不了
追答
报什么错误
展开全部
这个是我碰出来的。
package cxxx;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class Rnnn extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JLabel lblNewLabel;
private JTextField textField_2;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Rnnn frame = new Rnnn();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public Rnnn() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 75);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textField = new JTextField();
textField.setBounds(10, 10, 91, 21);
contentPane.add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setBounds(176, 10, 91, 21);
contentPane.add(textField_1);
textField_1.setColumns(10);
textField_2 = new JTextField();
textField_2.setBounds(335, 10, 76, 21);
contentPane.add(textField_2);
textField_2.setColumns(10);
JComboBox comboBox = new JComboBox();
comboBox.setModel(new DefaultComboBoxModel(new String[] {"+","-","*","/"}));
comboBox.setBounds(124, 10, 32, 21);
contentPane.add(comboBox);
comboBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
int stateChange = e.getStateChange();
if(stateChange == 1){
try {
int text1 = Integer.parseInt(textField.getText());
int text2 = Integer.parseInt(textField_1.getText());
int text3=0;
if("+".equals(e.getItem())) text3 = text1 + text2;
if("-".equals(e.getItem())) text3 = text1 - text2;
if("*".equals(e.getItem())) text3 = text1 * text2;
if("/".equals(e.getItem())) text3 = text1 / text2;
textField_2.setText(text3+"");
} catch (Exception e2) {
JOptionPane.showMessageDialog(null, "错误!!"+e2.getMessage());
}
}
}
});
lblNewLabel = new JLabel("=");
lblNewLabel.setBounds(284, 13, 24, 15);
contentPane.add(lblNewLabel);
}
}
package cxxx;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class Rnnn extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JLabel lblNewLabel;
private JTextField textField_2;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Rnnn frame = new Rnnn();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public Rnnn() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 75);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textField = new JTextField();
textField.setBounds(10, 10, 91, 21);
contentPane.add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setBounds(176, 10, 91, 21);
contentPane.add(textField_1);
textField_1.setColumns(10);
textField_2 = new JTextField();
textField_2.setBounds(335, 10, 76, 21);
contentPane.add(textField_2);
textField_2.setColumns(10);
JComboBox comboBox = new JComboBox();
comboBox.setModel(new DefaultComboBoxModel(new String[] {"+","-","*","/"}));
comboBox.setBounds(124, 10, 32, 21);
contentPane.add(comboBox);
comboBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
int stateChange = e.getStateChange();
if(stateChange == 1){
try {
int text1 = Integer.parseInt(textField.getText());
int text2 = Integer.parseInt(textField_1.getText());
int text3=0;
if("+".equals(e.getItem())) text3 = text1 + text2;
if("-".equals(e.getItem())) text3 = text1 - text2;
if("*".equals(e.getItem())) text3 = text1 * text2;
if("/".equals(e.getItem())) text3 = text1 / text2;
textField_2.setText(text3+"");
} catch (Exception e2) {
JOptionPane.showMessageDialog(null, "错误!!"+e2.getMessage());
}
}
}
});
lblNewLabel = new JLabel("=");
lblNewLabel.setBounds(284, 13, 24, 15);
contentPane.add(lblNewLabel);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询