Java实验05_GUI
实验05(01)计算员工工资
1. (0分)
设计Manager类、GUI类及事件处理
题目描述
(1)设计一个Manager类并编写代
码, Manager类的属性有姓名,
工号,基本工资,小时工资(元/小时)。自定义方法:至少包括计算月工资的方法:calSalary()。
(2)编写一个GUI类,输入manager的姓名、工号,基本工资,月工作时间(小时),创建对象,调用calSalary()方法计算出该manager的月工资,并显示在用户界面上。
*/
package test01_manager;
import java. awt. GridLayout;
import java. awt. Toolkit;
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. JPanel;
import javax. swing. JTextField;
public class ManageDemo {
public static void main ( String[ ] args) {
new ManagerGUI ( ) ;
}
}
class Manager {
private String name;
private String ID;
private double hourNum;
private double basicPrice;
private double hourPrice;
public Manager ( String name, String ID, double hourNum, double basicPrice, double hourPrice) {
this . name = name;
this . ID = ID;
this . hourNum = hourNum;
this . basicPrice = basicPrice;
this . hourPrice = hourPrice;
}
public double calSalary ( ) {
return basicPrice + hourPrice * hourNum;
}
}
class ManagerGUI extends JFrame implements ActionListener {
JLabel nameLabel, IDLabel, hourNumLabel, basicPriceLabel, hourPriceLabel, priceLabel;
JTextField nameTextField, IDTextField, hourNumTextField, basicPriceTextField, hourPriceTextField, priceTextField;
JPanel namePanel, IDPanel, hourNumPanel, basicPricePanel, hourPricePanel, pricePanel;
JButton calSalaryButton;
public ManagerGUI ( ) {
nameLabel = new JLabel ( "姓名" ) ;
IDLabel = new JLabel ( "工号" ) ;
hourNumLabel = new JLabel ( "小时数" ) ;
basicPriceLabel = new JLabel ( "基本工资" ) ;
hourPriceLabel = new JLabel ( "小时工资" ) ;
priceLabel = new JLabel ( "总工资" ) ;
nameTextField = new JTextField ( 10 ) ;
IDTextField = new JTextField ( 10 ) ;
hourNumTextField = new JTextField ( 10 ) ;
basicPriceTextField = new JTextField ( 10 ) ;
hourPriceTextField = new JTextField ( 10 ) ;
priceTextField = new JTextField ( 10 ) ;
namePanel = new JPanel ( ) ;
IDPanel = new JPanel ( ) ;
hourNumPanel = new JPanel ( ) ;
basicPricePanel = new JPanel ( ) ;
hourPricePanel = new JPanel ( ) ;
pricePanel = new JPanel ( ) ;
calSalaryButton = new JButton ( "计算" ) ;
calSalaryButton. addActionListener ( this ) ;
namePanel. add ( nameLabel) ;
namePanel. add ( nameTextField) ;
IDPanel. add ( IDLabel) ;
IDPanel. add ( IDTextField) ;
hourNumPanel. add ( hourNumLabel) ;
hourNumPanel. add ( hourNumTextField) ;
basicPricePanel. add ( basicPriceLabel) ;
basicPricePanel. add ( basicPriceTextField) ;
hourPricePanel. add ( hourPriceLabel) ;
hourPricePanel. add ( hourPriceTextField) ;
pricePanel. add ( priceLabel) ;
pricePanel. add ( priceTextField) ;
this . setLayout ( new GridLayout ( 7 , 1 , 5 , 5 ) ) ;
this . add ( namePanel) ;
this . add ( IDPanel) ;
this . add ( hourNumPanel) ;
this . add ( basicPricePanel) ;
this . add ( hourPricePanel) ;
this . add ( calSalaryButton) ;
this . add ( pricePanel) ;
this . setTitle ( "Manager" ) ;
this . setSize ( 300 , 250 ) ;
this . setDefaultCloseOperation ( JFrame. EXIT_ON_CLOSE) ;
int width = Toolkit. getDefaultToolkit ( ) . getScreenSize ( ) . width;
int height = Toolkit. getDefaultToolkit ( ) . getScreenSize ( ) . height;
this . setLocation ( width / 2 - 200 , height / 2 - 150 ) ;
this . setVisible ( true ) ;
}
@Override
public void actionPerformed ( ActionEvent e) {
if ( e. getSource ( ) == calSalaryButton) {
String name = nameTextField. getText ( ) ;
String ID = IDTextField. getText ( ) ;
double hourNum = Double. parseDouble ( hourNumTextField. getText ( ) ) ;
double basicPrice = Double. parseDouble ( basicPriceTextField. getText ( ) ) ;
double hourPrice = Double. parseDouble ( hourPriceTextField. getText ( ) ) ;
Manager manager = new Manager ( name, ID, hourNum, basicPrice, hourPrice) ;
double price = manager. calSalary ( ) ;
priceTextField. setText ( String. valueOf ( price) ) ;
}
}
}
实验05(02)简单计算器
一.界面显示
二.代码
2.1.CalculatorDemo.java
package test16_Calculator;
import java. awt. event. *;
import javax. swing. *;
import java. awt. *;
public class CalculatorDemo {
public static void main ( String[ ] args) {
Calculator calculator = new Calculator ( ) ;
}
}
class Calculator extends JFrame implements ActionListener {
int buttonSize = 20 ;
private boolean isEqual;
String[ ] buttonNameArray = {
"7" , "8" , "9" , "/" , "4" , "5" , "6" , "*" , "1" , "2" , "3" , "-" , "0" , "." , "C" , "+" , "圆的面积" ,
"test" , "test" , "=" } ;
Operation op;
String operator = "" ;
private JButton[ ] buttonArray = new JButton [ buttonSize] ;
private JTextField displayField;
private JPanel panel;
public Calculator ( ) {
isEqual = true ;
for ( int i = 0 ; i < buttonArray. length; i++ ) {
buttonArray[ i] = new JButton ( buttonNameArray[ i] ) ;
}
displayField = new JTextField ( ) ;
panel = new JPanel ( ) ;
Font font = new Font ( "宋体" , Font. BOLD, 16 ) ;
Font font1 = new Font ( "宋体" , Font. BOLD, 20 ) ;
for ( int i = 0 ; i < buttonArray. length; i++ ) {
buttonArray[ i] . setFont ( font) ;
}
displayField. setFont ( font1) ;
displayField. setHorizontalAlignment ( JTextField. RIGHT) ;
displayField. setText ( "0" ) ;
displayField. setForeground ( Color. BLACK) ;
displayField. setBorder ( BorderFactory. createCompoundBorder ( BorderFactory. createLineBorder ( Color. black) ,
BorderFactory. createEmptyBorder ( 5 , 5 , 5 , 5 ) ) ) ;
panel. setBorder ( BorderFactory. createCompoundBorder ( BorderFactory. createLineBorder ( Color. black) ,
BorderFactory. createEmptyBorder ( 10 , 10 , 10 , 10 ) ) ) ;
panel. setLayout ( new GridLayout ( 5 , 4 , 10 , 10 ) ) ;
this . add ( displayField, BorderLayout. NORTH) ;
for ( int i = 0 ; i < buttonArray. length; i++ ) {
panel. add ( buttonArray[ i] ) ;
}
this . add ( panel, BorderLayout. CENTER) ;
for ( int i = 0 ; i < buttonArray. length; i++ ) {
buttonArray[ i] . addActionListener ( this ) ;
}
this . setTitle ( "计算器" ) ;
this . setSize ( 500 , 350 ) ;
this . setDefaultCloseOperation ( JFrame. EXIT_ON_CLOSE) ;
int width = Toolkit. getDefaultToolkit ( ) . getScreenSize ( ) . width;
int height = Toolkit. getDefaultToolkit ( ) . getScreenSize ( ) . height;
this . setLocation ( width / 2 - 200 , height / 2 - 150 ) ;
this . setVisible ( true ) ;
}
private int judgeOperation ( String s) {
int operatorNum = 0 ;
if ( "+-*/" . indexOf ( s) != - 1 ) {
operatorNum = 2 ;
} else {
if ( "圆的面积" . indexOf ( s) != - 1 ) {
operatorNum = 1 ;
} else {
operatorNum = 0 ;
}
}
return operatorNum;
}
@Override
public void actionPerformed ( ActionEvent e) {
String s = e. getActionCommand ( ) ;
if ( "1234567890.=" . indexOf ( s) == - 1 ) {
operator = s;
}
if ( "C" . indexOf ( s) == - 1 ) {
operator = "" ;
}
if ( judgeOperation ( s) == 0 && ! s. equals ( "=" ) ) {
if ( isEqual) {
displayField. setText ( s) ;
} else {
displayField. setText ( displayField. getText ( ) + s) ;
}
isEqual = false ;
} else {
if ( judgeOperation ( s) != 0 ) {
op = OperationFactory. createOperate ( s) ;
op. setNum1 ( Double. parseDouble ( displayField. getText ( ) ) ) ;
displayField. setText ( "" ) ;
} else {
if ( judgeOperation ( operator) == 2 && s. equals ( "=" ) ) {
op. setNum2 ( Double. parseDouble ( displayField. getText ( ) ) ) ;
if ( op. getNum2 ( ) == 0 && operator. equals ( "/" ) ) {
JOptionPane. showMessageDialog ( null, "除数不能为0" , "提示" , 3 ) ;
displayField. setText ( "" ) ;
} else {
double result = op. getResult ( ) ;
displayField. setText ( Double. toString ( result) ) ;
isEqual = true ;
}
} else if ( s. equals ( "=" ) && judgeOperation ( operator) == 1 ) {
double result = op. getResult ( ) ;
displayField. setText ( Double. toString ( result) ) ;
isEqual = true ;
}
}
}
}
}
2.2.OperationFactory.java
package test16_Calculator;
public class OperationFactory {
public static Operation createOperate ( String operate) {
Operation op = null;
switch ( operate) {
case "+" : {
op = new OperationAdd ( ) ;
break ;
}
case "-" : {
op = new OperationSub ( ) ;
break ;
}
case "*" : {
op = new OperationMul ( ) ;
break ;
}
case "/" : {
op = new OperationDiv ( ) ;
break ;
}
case "圆的面积" : {
op = new OperationCircle ( ) ;
break ;
}
default :
break ;
}
return op;
}
}
2.3.Operation.java
package test16_Calculator;
public class Operation {
private double num1;
private double num2;
public double getResult ( ) {
return 0 ;
}
public double getNum1 ( ) {
return num1;
}
public void setNum1 ( double num1) {
this . num1 = num1;
}
public double getNum2 ( ) {
return num2;
}
public void setNum2 ( double num2) {
this . num2 = num2;
}
}
class OperationAdd extends Operation {
public double getResult ( ) {
return getNum1 ( ) + getNum2 ( ) ;
}
}
class OperationSub extends Operation {
public double getResult ( ) {
return getNum1 ( ) - getNum2 ( ) ;
}
}
class OperationMul extends Operation {
public double getResult ( ) {
return getNum1 ( ) * getNum2 ( ) ;
}
}
class OperationDiv extends Operation {
public double getResult ( ) {
return getNum1 ( ) / getNum2 ( ) ;
}
}
class OperationCircle extends Operation {
public double getResult ( ) {
return getNum1 ( ) * getNum1 ( ) * Math. PI;
}
}
实验05(03)登录触发
一.界面
二.代码
package test08_Login;
import java. awt. Color;
import java. awt. Font;
import java. awt. GridLayout;
import java. awt. Toolkit;
import java. awt. event. ActionEvent;
import java. awt. event. ActionListener;
import javax. swing. ImageIcon;
import javax. swing. JButton;
import javax. swing. JFrame;
import javax. swing. JLabel;
import javax. swing. JOptionPane;
import javax. swing. JPanel;
import javax. swing. JPasswordField;
import javax. swing. JTextField;
public class Main {
public static void main ( String[ ] args) {
LoginGUI loginGUI = new LoginGUI ( ) ;
}
}
class LoginGUI extends JFrame implements ActionListener {
private static final long serialVersionUID = 1 L;
JLabel jlabel1, jlabel2;
JTextField usernameText;
JPasswordField passwordText;
JButton loginButton, cancelButton;
JPanel jpanel1, jpanel2, jpanel3;
public LoginGUI ( ) {
jpanel1 = new JPanel ( ) ;
jpanel2 = new JPanel ( ) ;
jpanel3 = new JPanel ( ) ;
jlabel1 = new JLabel ( "用户名" ) ;
jlabel2 = new JLabel ( "密 码" ) ;
usernameText = new JTextField ( 10 ) ;
passwordText = new JPasswordField ( 10 ) ;
loginButton = new JButton ( "登录" ) ;
cancelButton = new JButton ( "取消" ) ;
Font font = new Font ( "楷体" , Font. BOLD, 18 ) ;
jlabel1. setFont ( font) ;
jlabel2. setFont ( font) ;
loginButton. setFont ( font) ;
cancelButton. setFont ( font) ;
Color color = new Color ( 0 , 0 , 255 ) ;
jlabel1. setForeground ( color) ;
jlabel2. setForeground ( color) ;
loginButton. setForeground ( color) ;
cancelButton. setForeground ( color) ;
this . setLayout ( new GridLayout ( 3 , 1 , 5 , 5 ) ) ;
jpanel1. add ( jlabel1) ;
jpanel1. add ( usernameText) ;
jpanel2. add ( jlabel2) ;
jpanel2. add ( passwordText) ;
jpanel3. add ( loginButton) ;
jpanel3. add ( cancelButton) ;
this . add ( jpanel1) ;
this . add ( jpanel2) ;
this . add ( jpanel3) ;
loginButton. addActionListener ( this ) ;
cancelButton. addActionListener ( this ) ;
ImageIcon icon = new ImageIcon ( "./src/test08_Login/qq.png" ) ;
this . setIconImage ( icon. getImage ( ) ) ;
this . setTitle ( "用户登录" ) ;
this . setSize ( 300 , 180 ) ;
this . setDefaultCloseOperation ( JFrame. EXIT_ON_CLOSE) ;
int width = Toolkit. getDefaultToolkit ( ) . getScreenSize ( ) . width;
int height = Toolkit. getDefaultToolkit ( ) . getScreenSize ( ) . height;
this . setLocation ( width / 2 - 200 , height / 2 - 150 ) ;
this . setVisible ( true ) ;
}
@Override
public void actionPerformed ( ActionEvent e) {
if ( e. getSource ( ) == loginButton) {
if ( checkPassword ( ) == true ) {
JOptionPane. showMessageDialog ( null, "用户名和密码正确,欢迎登录!" , "提示" , 1 ) ;
} else {
JOptionPane. showMessageDialog ( null, "用户名或密码错误,请重新登录!" , "提示" , 2 ) ;
}
} else {
if ( e. getActionCommand ( ) == "取消" ) {
clearText ( ) ;
}
}
}
public boolean checkPassword ( ) {
String userName = "dxxy" ;
String password = "admin" ;
String inputUserName = usernameText. getText ( ) . trim ( ) ;
String inputPassword = String. valueOf ( passwordText. getPassword ( ) ) . trim ( ) ;
if ( userName. equals ( inputUserName) && password. equals ( inputPassword) ) {
return true ;
} else {
return false ;
}
}
public void clearText ( ) {
usernameText. setText ( "" ) ;
passwordText. setText ( "" ) ;
}
}