1.设计如下界面,并实现相应功能。
package wxy;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class wxy1 {
private JFrame frame;
private JLabel firstLabel,secondLabel,resultLabel;
private JTextField firstTextField,secondTextField,resultTextField;
private JButton countButton,clearButton,exitButton;
public void launchFrame(){
frame=new JFrame("加法题");
this.firstLabel=new JLabel("操作数一");
this.secondLabel=new JLabel("操作数二");
this.resultLabel=new JLabel("计算结果");
this.firstTextField=new JTextField(12);
this.secondTextField=new JTextField(12);
this.resultTextField=new JTextField(12);
this.countButton=new JButton("计算");
this.clearButton=new JButton("清除");
this.exitButton=new JButton("退出");
JPanel firstPanel=new JPanel();
JPanel secondPanel=new JPanel();
JPanel resultPanel=new JPanel();
JPanel buttonPanel=new JPanel();
firstPanel.add(this.firstLabel);
firstPanel.add(this.firstTextField);
secondPanel.add(this.secondLabel);
secondPanel.add(this.secondTextField);
resultPanel.add(this.resultLabel);
resultPanel.add(this.resultTextField);
buttonPanel.add(this.countButton);
buttonPanel.add(this.clearButton);
buttonPanel.add(this.exitButton);
frame.setLayout(new GridLayout(4,1));
frame.add(firstPanel);
frame.add(secondPanel);
frame.add(resultPanel);
frame.add(buttonPanel);
frame.setSize(300,220);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
wangxingyuan1.Monitor1 monitor1=new wangxingyuan1.Monitor1();
this.countButton.addActionListener(monitor1);
wangxingyuan1.Monitor2 monitor2=new wangxingyuan1.Monitor2();
this.clearButton.addActionListener(monitor2);
wangxingyuan1.Monitor3 monitor3=new wangxingyuan1.Monitor3();
this.exitButton.addActionListener(monitor3);
}
class Monitor1 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
double num1= Double.parseDouble(firstTextField.getText());
double num2= Double.parseDouble(secondTextField.getText());
double num3=num1+num2;
resultTextField.setText(""+num3);
}
}
class Monitor2 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
firstTextField.setText("");
secondTextField.setText("");
resultTextField.setText("");
}
}
class Monitor3 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
public static void main(String[] args) {
wxy1 WXY1=new wxy1();
WXY1.launchFrame();
}
}
2.设计如下界面,并实现相应功能。对于输入的用户名和密码,如果正确则给出“登录成功”的提示信息,否则给出“用户名或密码错误”的提示信息。设用户名为自己的姓名,密码为自己学号。
package wxy;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class wxy2 {
private JFrame frame;
private JLabel userLable,passwordLabel;
private JLabel titleLabel;
private JTextField userTextField;
private JPasswordField passwordField;
private JButton logonButton,registerButton;
public void launchFrame()
{
frame=new JFrame("登录窗口");
this.userLable=new JLabel("用户名");
this.passwordLabel=new JLabel("密 码");
this.titleLabel=new JLabel("职工信息管理系统");
this.userTextField=new JTextField(12);
this.passwordField=new JPasswordField(12);
this.logonButton=new JButton("登录");
this.registerButton=new JButton("注册");
JPanel userPanel=new JPanel();
JPanel passwordPanel=new JPanel();
JPanel titlePanel=new JPanel();
JPanel buttonPanel=new JPanel();
titlePanel.add(this.titleLabel);
userPanel.add(this.userLable);
userPanel.add(this.userTextField);
passwordPanel.add(this.passwordLabel);
passwordPanel.add(this.passwordField);
buttonPanel.add(this.logonButton);
buttonPanel.add(this.registerButton);
frame.setLayout(new GridLayout(4,1));
frame.add(titlePanel);
frame.add(userPanel);
frame.add(passwordPanel);
frame.add(buttonPanel);
frame.setSize(300,220);
frame.setVisible(true);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Monitor monitor=new Monitor();
this.logonButton.addActionListener(monitor);
}
class Monitor implements ActionListener
{
@Override
public void actionPerformed(ActionEvent arg0){
//TODO Auto-generated method stub
String user=userTextField.getText();
char[] mima= passwordField.getPassword();
String password=new String(mima);
if(user.equals(小王")&&password.equals("2011050211"))
{
JOptionPane.showMessageDialog(null,"登录成功!");
}
else
{
JOptionPane.showMessageDialog(null,"用户名或密码输入错误,请重新输入");
}
}
}
}
class Test01
{
public static void main(String[] args)
{
wxy2 Wxy2=new wxy2();
wxy2.launchFrame();
}
}