Java图形截面编程(第四次实验课)

package temphomework;
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.JPanel;
import javax.swing.JTextField;
public class SimpleCalc extends JFrame {
public static void main(String[] args) {
new SimpleCalc();
}
public SimpleCalc() {
setSize(500, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
init();
setVisible(true);
}
protected void init() {
setTitle("加法计算器 作者:软件Q197 杨红福");
JPanel p = new JPanel();
add(p);
JTextField num1 = new JTextField(10);
p.add(num1);
p.add(new JLabel("+"));
JTextField num2 = new JTextField(10);
p.add(num2);
p.add(new JLabel("="));
JTextField result = new JTextField(10);
result.setEditable(false);
p.add(result);
JButton calc = new JButton("计算");
calc.addActionListener(new CalcListener(num1, num2, result));
p.add(calc);
p.setBackground(Color.gray);
}
class CalcListener implements ActionListener {
private JTextField num1, num2, result;
CalcListener(JTextField num1, JTextField num2, JTextField result) {
this.num1 = num1;
this.num2 = num2;
this.result = result;
}
@Override
public void actionPerformed(ActionEvent e) {
try {
double r = Double.parseDouble(num1.getText()) + Double.parseDouble(num2.getText());
result.setText(String.valueOf(r));
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Error: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
}


package temphomework;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
class Login extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
JPanel panel;
JLabel label,label2,label3;
JComboBox list;
JButton loginButton,exitButton;
JTextField jTextField;
JPasswordField passwordField;
public Login() {
this.setTitle("杨红福 软件Q197");
this.setSize(250,220);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
panel.setLayout(new FlowLayout());
label3=new JLabel("用户类型");
label = new JLabel("用户名");
label2 = new JLabel("密码 ");
JLabel null_c=new JLabel(" ");
loginButton = new JButton("登录");
loginButton.addActionListener(this);
exitButton = new JButton("退出");
exitButton.addActionListener(this);
jTextField = new JTextField(17);
passwordField = new JPasswordField(17);
list=new JComboBox();
list.addItem("学生用户");
list.addItem("教师用户");
panel.add(label3);
panel.add(list);
panel.add(null_c);
panel.add(label);
panel.add(jTextField);
panel.add(label2);
panel.add(passwordField);
panel.add(loginButton);
panel.add(exitButton);
this.add(panel);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource()==loginButton) {
if (jTextField.getText().contains("s") && passwordField.getText().contains("s")) {
JOptionPane.showMessageDialog(null,"登录成功!" );
}else if(jTextField.getText().contains(" ")){
JOptionPane.showMessageDialog(null, "用户名不能为空!");
}else {
JOptionPane.showMessageDialog(null, "用户名或密码错误!");
}
if (e.getSource()==exitButton) {
System.exit(0);
}
}
}
}
class UserLoginApp {
public static void main(String[] args) {
new Login();
}
}
