java吧 关注:1,180,967贴子:12,576,876
  • 0回复贴,共1

求助呀,java实验课作业不会做

只看楼主收藏回复

package tex1;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionEvent;
public class App4 extends JFrame implements ActionListener {
JPanel contentPane;
JTextField textField;
private JButton button;
private JButton button_1;
private JButton button_2;
private JButton button_3;
private JButton button_4;
private JButton button_5;
private JButton button_6;
private JButton button_7;
private JButton button_8;
private JButton button_9;
private JButton button_10;
private JButton button_11;
private JButton button_12;
private JButton button_13;
private JButton button_14;
private JButton button_15;
int num1;
int result;
boolean isOperPressed = false;
// 当i=0时说明是我们第一次输入,字符串text不会累加
int i = 0;
// 存放text的内容
String text = "";
// 存放点击按钮+、-、*、/之前的数值
double defbutton = 0;
// +、-、*、/的代号分别为1,2,3,4
int symbol = 0;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
App4 frame = new App4();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
App4() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textField = new JTextField("0");
textField.setBounds(0, 5, 432, 42);
textField.setHorizontalAlignment(SwingConstants.RIGHT);
contentPane.add(textField);
textField.setColumns(10);
JButton button = new JButton("1");
button.setBounds(14, 153, 83, 33);
contentPane.add(button);
JButton button_1 = new JButton("2");
button_1.setBounds(112, 153, 83, 33);
contentPane.add(button_1);
JButton button_2 = new JButton("3");
button_2.setBounds(208, 153, 83, 33);
contentPane.add(button_2);
JButton button_3 = new JButton("4");
button_3.setBounds(14, 106, 83, 33);
contentPane.add(button_3);
JButton button_4 = new JButton("5");
button_4.setBounds(112, 107, 83, 33);
contentPane.add(button_4);
JButton button_5 = new JButton("6");
button_5.setBounds(208, 106, 83, 33);
contentPane.add(button_5);
JButton button_6 = new JButton("7");
button_6.setBounds(14, 60, 83, 33);
contentPane.add(button_6);
JButton button_7 = new JButton("8");
button_7.setBounds(112, 61, 83, 33);
contentPane.add(button_7);
JButton button_8 = new JButton("9");
button_8.setBounds(208, 60, 83, 33);
contentPane.add(button_8);
JButton button_9 = new JButton("+");
button_9.setBounds(311, 60, 83, 33);
contentPane.add(button_9);
JButton button_10 = new JButton("-");
button_10.setBounds(311, 106, 83, 33);
contentPane.add(button_10);
JButton button_11 = new JButton("*");
button_11.setBounds(311, 153, 83, 33);
contentPane.add(button_11);
JButton button_12 = new JButton("/");
button_12.setBounds(311, 207, 83, 33);
contentPane.add(button_12);
JButton button_13 = new JButton("C");
button_13.setBounds(111, 207, 83, 33);
contentPane.add(button_13);
JButton button_14 = new JButton("=");
button_14.setBounds(208, 207, 83, 33);
contentPane.add(button_14);
JButton button_15 = new JButton("0");
button_15.setBounds(14, 207, 83, 33);
contentPane.add(button_15);
button.addActionListener(this); // 对2按钮添加监听事件
button_1.addActionListener(this); // 对3按钮添加监听事件
button_2.addActionListener(this); // 对4按钮添加监听事件
button_3.addActionListener(this);// 对5按钮添加监听事件
button_4.addActionListener(this);// 对6按钮添加监听事件
button_5.addActionListener(this);// 对7按钮添加监听事件
button_6.addActionListener( this); // 对8按钮添加监听事件
button_7.addActionListener( this); // 对9按钮添加监听事件
button_8.addActionListener(this); // 对0按钮添加监听事件
button_9.addActionListener( this);// 对置零按钮添加监听事件
button_10.addActionListener( this); // 对+按钮添加监听事件
button_11.addActionListener( this);// 对=按钮添加监听事件
button_12.addActionListener( this);// 对-按钮添加监听事件
button_13.addActionListener( this); // 对*按钮添加监听事件
button_14.addActionListener( this);// 对/按钮添加监听事件
button_15.addActionListener( this);// 对/按钮添加监听事件
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button) {
if (isOperPressed || i == 0) {
textField.setText("1");
isOperPressed = false;
i = 1;
} else {
text = textField.getText();
textField.setText(text + "1");
}
} else if (e.getSource() == button_1) {
if (isOperPressed || i == 0) {
textField.setText("2");
isOperPressed = false;
i = 1;
} else {
text =textField.getText();
textField.setText(text + "2");
}
} else if (e.getSource() == button_2) {
if (isOperPressed || i == 0) {
textField.setText("3");
isOperPressed= false;
i = 1;
} else {
text = textField.getText();
textField.setText(text + "3");
}
} else if (e.getSource() == button_3) {
if (isOperPressed|| i == 0) {
textField.setText("4");
isOperPressed= false;
i = 1;
} else {
text = textField.getText();
textField.setText(text + "4");
}
} else if (e.getSource() == button_4) {
if (isOperPressed || i == 0) {
textField.setText("5");
isOperPressed = false;
i = 1;
} else {
text = textField.getText();
textField.setText(text + "5");
}
} else if (e.getSource() == button_5) {
if (isOperPressed || i == 0) {
textField.setText("6");
isOperPressed= false;
i = 1;
} else {
text = textField.getText();
textField.setText(text + "6");
}
} else if (e.getSource() == button_6) {
if (isOperPressed || i == 0) {
textField.setText("7");
isOperPressed = false;
i = 1;
} else {
text = textField.getText();
textField.setText(text + "7");
}
} else if (e.getSource() == button_7) {
if (isOperPressed || i == 0) {
textField.setText("8");
isOperPressed = false;
i = 1;
} else {
text = textField.getText();
textField.setText(text + "8");
}
} else if (e.getSource() ==button_8) {
if (isOperPressed || i == 0) {
textField.setText("9");
isOperPressed = false;
i = 1;
} else {
text = textField.getText();
textField.setText(text + "9");
}
}
/*
* 对于0这个按钮有一定的说法,在我的程序里不会出现如00000这样的情况,我加了判断条件就是
* 如果text中的数值=0就要判断在这个数值中是否有.存在?如果有那么就在原来数值基础之上添 加0;否则保持原来的数值不变
*/
else if (e.getSource() == button_15) { // result.text.getText()是得到text里内容的意思
if (isOperPressed || i == 0) {
textField.setText("0");
isOperPressed = false;
i = 1;
} else {
text = textField.getText();
if (Float.parseFloat(text) > 0 || Float.parseFloat(text) < 0) { // Float.parseFloat(text)就是类型转换了,下面都是一样
textField.setText(text + "0");
} else {
if (text.trim().indexOf(".") == -1) {
textField.setText(text);
} else {
textField.setText(text + "0");
}
}
}
} else if (e.getSource() ==button_13) {
textField.setText("0");
i = 0;
isOperPressed = true;
// text = "";
defbutton = 0;
}
/*
* 本程序不会让一个数值中出现2个以上的小数点.具体做法是:判断是否已经存在.存在就不添加, 不存在就添加.
*/
else if (e.getSource() == button_9) {
isOperPressed = true;
i = 0;
defbutton = Double.parseDouble(textField.getText());
symbol = 1;
} // 获得点击-之前的数值
else if (e.getSource() == button_10) {
isOperPressed= true;
i = 0;
defbutton = Double.parseDouble(textField.getText());
symbol = 2;
} // 获得点击*之前的数值
else if (e.getSource() == button_11) {
isOperPressed = true;
i = 0;
defbutton = Double.parseDouble(textField.getText());
System.out.println(defbutton);
symbol = 3;
} // 获得点击/之前的数值
else if (e.getSource() == button_12) {
isOperPressed= true;
i = 0;
defbutton = Double.parseDouble(textField.getText());
symbol = 4;
} else if (e.getSource() == button_14) {
switch (symbol) {
case 1: { // 计算加法
double ad = defbutton
+ Double.parseDouble(textField.getText());
textField.setText(ad + "");
i = 0;
text = "";
break;
}
case 2: { // 计算减法
double ad = defbutton
- Double.parseDouble(textField.getText());
textField.setText(String.valueOf(ad));
i = 0;
text = "";
break;
}
case 3: { // 计算乘法
double ad = defbutton
* Double.parseDouble(textField.getText());
textField.setText(ad + "");
i = 0;
text = "";
break;
}
case 4: { // 计算除法
double ad = defbutton
/ Double.parseDouble(textField.getText());
textField.setText(ad + "");
i = 0;
text = "";
break;
}
}
System.out.println(isOperPressed);
}
System.out.println(textField.getText());
}
}
运行后点击按键没有反应,简直哭死


IP属地:四川1楼2017-04-22 13:16回复