Experiment 5 Java graphical interface

 

table of Contents

1. The purpose of the experiment

2. Experimental code

1. Write an application program with a window titled "Login", which can realize the input of user name and password.

2. Write an application that has a window titled "Registered Student Information". Please design the layout and components of the window by yourself to realize the registration function of student information.

3. Design the chat window as shown in Figure 5-4

4. (Optional) Write a simple calculator program, the interface is shown in Figure 5-5, to realize simple addition, subtraction, multiplication, division and other functions. Up to 12 numbers are displayed.

One word per text


 

1. The purpose of the experiment

1. Grasp the basic concepts of JavaFX stage, scene, node, panel, control, etc.

2. Master the window structure of JavaFX ;

3. Master the layout panel classes commonly used in JavaFX , such as stack panel StackPane class, flow panel FlowPane class, border panel BorderPane class, GridPane grid panel class, Hbox single row panel class, Vbox single column panel class, etc.;

4. Master the commonly used controls, such as Button , TextField , Label , PasswordField, etc.;

5. Master the JavaFx event handling mechanism;

2. Experimental code

1. Write an application program with a window titled "Login", which can realize the input of user name and password.

( 1 ) If the user name and password are entered correctly, click the "Login" button to pop up the "User Login Successful" message box;

( 2 ) If the user name and password are entered incorrectly, click the "Login" button to pop up the "User login failed" message box.

import java.util.ArrayList;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextArea;
import javafx.scene.layout.GridPane;
import javafx.scene.control.ScrollPane;
public class App15_7 extends Application
{
  private TextField tf=new TextField();
  private PasswordField pf=new PasswordField();
  private TextArea ta=new TextArea("未登录");
  @Override
  public void start(Stage Stage)
  {
    GridPane rootGP=new GridPane();
    final Label lab1=new Label("用户名:");
    final Label lab2=new Label("密  码:");
    tf.setPromptText("输入用户名");
    pf.setPromptText("输入密码");
    rootGP.add(lab1,0,0);
    rootGP.add(tf,1,0);
    rootGP.add(lab2,0,1);
    rootGP.add(pf,1,1);
    final ScrollPane scro=new ScrollPane(ta);
    ta.setPrefColumnCount(15);
    ta.setEditable(false);
    pf.setOnAction(e->
      {
    	ArrayList databases = new ArrayList();
    	user user1 = new user("tim","123");
    	databases.add(user1);
    	user user2 = new user("mary","121");
    	databases.add(user2);
    	user user3 = new user("bill","321");
    	databases.add(user3);
    	user user4 = new user("carl","110");
    	databases.add(user4);
    	for(int i=0;i<=databases.size()-1;i++) {
    		if(tf.getText().equals(((user) databases.get(i)).getname()) && pf.getText().equals(((user) databases.get(i)).getpsw()))
            {
              ta.setEditable(true);
              ta.setWrapText(true);
              ta.setStyle("-fx-text-fill:red");
              ta.setText("恭喜你!!\n登录成功");
            }
          }
    	}
        
    );
    rootGP.add(scro,0,3,4,3);
    Scene scene=new Scene(rootGP,190,120);
    Stage.setTitle("登录");
    Stage.setScene(scene);
    Stage.show();
  }
  
  public static void main(String[] args) {
	  launch(args);
  }
}

2. Write an application that has a window titled "Registered Student Information". Please design the layout and components of the window by yourself to realize the registration function of student information.

( 1 ) Student information includes: student ID, name, date of birth, etc.;

( 2 ) Click the registration button, if you have not registered, the "Student Information Registration Successful" dialog box will pop up, and the student will be added to the List collection;

( 3 ) Click the register button, if the student has already registered, the "student information already exists, registration failed" dialog box will pop up;

( 4 ) Click the Cancel button to end this registration and close the window.

( 5 ) Adopting object-oriented programming ideas

package Javatest;

import java.util.ArrayList;
import java.util.Iterator;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
@SuppressWarnings("unused")
public class Lab5 extends Application{
	final Label lab1=new Label("姓名:");
	final Label lab2=new Label("学号:");
	final Label lab3=new Label("出生日期:");
	final TextField tf1=new TextField();
	final TextField tf2=new TextField();
	final TextField tf3=new TextField();
	public Button bt1;
	public Button bt2;
	@Override
	public void start(Stage primaryStage ) throws Exception {
		GridPane rootGP=new GridPane();
		rootGP.setPadding(new Insets(15,15,15,15));
		rootGP.setHgap(5);
		rootGP.setVgap(5);
		tf1.setPromptText("请输入学生姓名");
		rootGP.add(lab1, 0, 0);
		rootGP.add(tf1, 1, 0);
		
		tf2.setPromptText("请输入学生号");
		rootGP.add(lab2, 0, 1);
		rootGP.add(tf2, 1, 1);
		tf3.setPromptText("请输入出生日期");
		rootGP.add(lab3, 0, 2);
		rootGP.add(tf3, 1, 2);
		
		Button bt1=new Button("注册");
		rootGP.add(bt1, 0, 3);
		Button bt2=new Button("取消");
		rootGP.add(bt2, 1, 3);
		
		Scene scene=new Scene(rootGP,400,120);
		primaryStage.setScene(scene);
		primaryStage.setTitle("登录");
		primaryStage.show();
		
		bt1.setOnAction(e->{
			Stage stage=new Stage();
			stage.setTitle("消息框");
			final TextArea ta=new TextArea("请输入内容....");
			ta.setText("成功界面");
			stage.setScene(new Scene (ta,180,180));
			stage.show();
			
			if ((tf1.getText().equals("周彦志")) && (tf2.getText().equals("2019443709"))
					&& tf3.getText().equals("2001-07-06"))
			{
			ta.setWrapText(true);
			ta.setStyle("-fx-text-fill:blue");
			ta.setText("注册成功!");
			ArrayList<String> list1=new ArrayList<String>();
			list1.add("杨洪    2019443747    2000年12月11日");
			/*遍历
			 
			      数组*/
			@SuppressWarnings("rawtypes")
			Iterator i =list1.iterator();
			while (i.hasNext()) {
				String str=(String)i.next();
				System.out.println(str);
			}
			}else {
				ta.setWrapText(true);//文本区可换行
				ta.setStyle("-fx-text-fill:red");//将文本区中的文字设置为红色
				ta.setText("该学生信息已存在,注册失败!");
			}});
		
		
		
		
}
	
}

3. Design the chat window as shown in Figure 5-4

Figure 5-4 Chat window interface

Click the "Send" button to send the information in the text area to the chat area, and clear the content;

When the window starts, load the historical chat history;

Click the close button to save the chat history to a file and close the window.

package homework5;
import java.awt.Button;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Q4 extends Frame {
	TextArea ta;
	TextField tf;
	public Q4() {//用构造函数初始化
	ta = new TextArea("");
	ta.setBounds(50, 50, 350, 200);
	tf = new TextField("");
	tf.setBounds(50, 300, 350, 50);
	Button b1 = new Button("发送");
	b1.setBounds(400, 300, 50, 50);
	Button b2 = new Button("关闭");
	b2.setBounds(460, 300, 50, 50);
	add(ta);
	add(tf);
	add(b1);
	add(b2);
	
	b1.addActionListener(new ActionListener() {
	@Override
	public void actionPerformed(ActionEvent e) {
	// TODO Auto-generated method stub
	ta.append(tf.getText()+"\n");
	tf.setText("");
	}
	});
	jie er = new jie();
	b2.addActionListener(er);
	addWindowListener(new WindowAdapter() {
	public void windowClosing(WindowEvent ee) {
	System.exit(0);
	}
	});
	}
	public static void main(String[] args) {
	
	Q4 f = new Q4();
	f.setLayout(null);
	f.setTitle("消息窗口");
	f.setSize(600, 600);
	f.setVisible(true);
	}
	class jie implements ActionListener {
	public void actionPerformed(ActionEvent e) {
	System.exit(0);//不是1
	}
	}
}
}

4. (Optional) Write a simple calculator program, the interface is shown in Figure 5-5, to realize simple addition, subtraction, multiplication, division and other functions. Up to 12 numbers are displayed.

Figure 5-5 Simple calculator

package demo05;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Calculator implements ActionListener{
	GridLayout layout;
	JButton button1,button2,button3,button4,button5,button6,button7,button8,button9,button10,button11,button12,button13,button14,button15,button16;
	JSplitPane jsp;
	JFrame frame;
//	JTextArea area;
	JPanel pane1,pane2;
	JTextField jtf;
	public Calculator(){
		frame=new JFrame("计算器");
		frame.setSize(new Dimension(400,600));
		layout=new GridLayout(4,4);
		pane1=new JPanel(layout);
		jtf=new JTextField();
		jtf.setBounds(0, 0, 400, 200);
		button1=new JButton("7");
		button2=new JButton("8");
		button3=new JButton("9");
		button4=new JButton("/");
		button5=new JButton("4");
		button6=new JButton("5");
		button7=new JButton("6");
		button8=new JButton("*");
		button9=new JButton("1");
		button10=new JButton("2");
		button11=new JButton("3");
		button12=new JButton("+");
		button13=new JButton(".");
		button14=new JButton("0");
		button15=new JButton("=");
		button16=new JButton("-");
		pane1.add(button1);
		pane1.add(button2);
		pane1.add(button3);
		pane1.add(button4);
		pane1.add(button5);
		pane1.add(button6);
		pane1.add(button7);
		pane1.add(button8);
		pane1.add(button9);
		pane1.add(button10);
		pane1.add(button11);
		pane1.add(button12);
		pane1.add(button13);
		pane1.add(button14);
		pane1.add(button15);
		pane1.add(button16);
		jsp=new JSplitPane(JSplitPane.VERTICAL_SPLIT,jtf,pane1);
		frame.add(jsp);
		button1.addActionListener(this);
		button2.addActionListener(this);
		button3.addActionListener(this);
		button4.addActionListener(this);
		button5.addActionListener(this);
		button6.addActionListener(this);
		button7.addActionListener(this);
		button8.addActionListener(this);
		button9.addActionListener(this);
		button10.addActionListener(this);
		button11.addActionListener(this);
		button12.addActionListener(this);
		button13.addActionListener(this);
		button14.addActionListener(this);
		button15.addActionListener(this);
		button16.addActionListener(this);
		jtf.addActionListener(this);
		frame.setVisible(true);
	}
	@Override
	public void actionPerformed(ActionEvent event) {
		Object obj=event.getSource();
		if(obj instanceof JButton){
			JButton button=(JButton)obj;
			if(button14==button){
			if(jtf.getText().equals("0")){
				jtf.requestFocus();
			}
			else{
				String str=jtf.getText();
				jtf.setText(str+"0");
			}
			}
			if(button9==button){
			if(jtf.getText().equals("0")){
				jtf.setText("");
				jtf.setText("1");
				jtf.requestFocus();
			}
			else{
				String str=jtf.getText();
				jtf.setText(str+"1");
			}
			}
			if(button10==button){
			if(jtf.getText().equals("0")){
				jtf.setText("");
				jtf.setText("2");
				jtf.requestFocus();
			}
			else{
				String str=jtf.getText();
				jtf.setText(str+"2");
			}
			}
			if(button11==button){
			if(jtf.getText().equals("0")){
				jtf.setText("");
				jtf.setText("3");
				jtf.requestFocus();
			}
			else{
				String str=jtf.getText();
				jtf.setText(str+"3");
			}
			}
			if(button5==button){
			if(jtf.getText().equals("0")){
				jtf.setText("");
				jtf.setText("4");
				jtf.requestFocus();
			}
			else{
				String str=jtf.getText();
				jtf.setText(str+"4");
			}
			}
			if(button6==button){
			if(jtf.getText().equals("0")){
				jtf.setText("");
				jtf.setText("5");
				jtf.requestFocus();
			}
			else{
				String str=jtf.getText();
				jtf.setText(str+"5");
			}
			}
			if(button7==button){
			if(jtf.getText().equals("0")){
				jtf.setText("");
				jtf.setText("6");
				jtf.requestFocus();
			}
			else{
				String str=jtf.getText();
				jtf.setText(str+"6");
			}
			}
			if(button1==button){
			if(jtf.getText().equals("0")){
				jtf.setText("");
				jtf.setText("7");
				jtf.requestFocus();
			}
			else{
				String str=jtf.getText();
				jtf.setText(str+"7");
			}
			}
			if(button2==button){
			if(jtf.getText().equals("0")){
				jtf.setText("");
				jtf.setText("8");
				jtf.requestFocus();
			}
			else{
				String str=jtf.getText();
				jtf.setText(str+"8");
			}
			}
			if(button3==button){
			if(jtf.getText().equals("0")){
				jtf.setText("");
				jtf.setText("9");
				jtf.requestFocus();
			}
			else{
				String str=jtf.getText();
				jtf.setText(str+"9");
			}
			}
			if(button13==button){
				if(jtf.getText().equals("0")){
				jtf.setText("");
				jtf.setText("");
				jtf.requestFocus();
			}
			else{
				String str=jtf.getText();
				jtf.setText(str+"");
			}
			}
			if(button12==button){
			if(jtf.getText().equals("0")){
				jtf.setText("");
				jtf.setText("+");
				jtf.requestFocus();
			}
			else{
				String str=jtf.getText();
				jtf.setText(str+"+");
			}
			}
			if(button16==button){
			if(jtf.getText().equals("0")){
				jtf.setText("");
				jtf.setText("-");
				jtf.requestFocus();
			}
			else{
				String str=jtf.getText();
				jtf.setText(str+"-");
			}
			}
			if(button8==button){
			if(jtf.getText().equals("0")){
				jtf.setText("");
				jtf.setText("*");
				jtf.requestFocus();
			}
			else{
				String str=jtf.getText();
				jtf.setText(str+"*");
			}
			}
			if(button4==button){
			if(jtf.getText().equals("0")){
				jtf.setText("");
				jtf.setText("/");
				jtf.requestFocus();
			}
			else{
				String str=jtf.getText();
				jtf.setText(str+"/");
			}
			}
			if(button13==button){
			if(jtf.getText().equals("0")){
				jtf.setText("");
				jtf.setText(".");
				jtf.requestFocus();
			}
			else{
				String str=jtf.getText();
				jtf.setText(str+".");
			}
			}
			//等号实现加减乘除功能
			if(button15==button){
				if(jtf.getText().indexOf("+")!=-1){
					String str=jtf.getText();
					int i=str.indexOf("+");
					String s=str.substring(0, i);
					String s1=str.substring(i+1,str.length());
					Double d1=Double.parseDouble(s);
					Double d2=Double.parseDouble(s1);
					double result=d1+d2;
					String str1=String.valueOf(result);
					jtf.setText(str1);
}

			
			//减法运算
				else
					if(jtf.getText().indexOf("-")!=-1){
				
				String str=jtf.getText();
				int i=str.indexOf("-");
				String s=str.substring(0, i);
				String s1=str.substring(i+1,str.length());
				Double d1=Double.parseDouble(s.trim());
				Double d2=Double.parseDouble(s1);
				double result=d1-d2;
				String str1=String.valueOf(result);
				jtf.setText(str1);
			}
			//乘法运算
					else if(jtf.getText().indexOf("*")!=-1){
				String str=jtf.getText();
				int i=str.indexOf("*");
				String s=str.substring(0, i);
				String s1=str.substring(i+1,str.length());
				Double d1=Double.parseDouble(s.trim());
				Double d2=Double.parseDouble(s1);
				double result=d1*d2;
				String str1=String.valueOf(result);
				jtf.setText(str1);
			}
					else if(jtf.getText().indexOf("/")!=-1){
						String str=jtf.getText();
						int i=str.indexOf("/");
						String s=str.substring(0, i);
						String s1=str.substring(i+1,str.length());
						Double d1=Double.parseDouble(s.trim());
						Double d2=Double.parseDouble(s1);
						double result=d1/d2;
						String str1=String.valueOf(result);
						jtf.setText(str1);
			}
			else{
				jtf.setText("请选择要输入的运算符");
			}
			}
			}
		
		}
		
	public static void main(String[] args){
		new Calculator();
	}
}

One word per text

Happiness is always given to yourself, so what is the so-called sense of security?

The lucky color in this article is orange! Hope you are happy every day!

Guess you like

Origin blog.csdn.net/weixin_47723732/article/details/112948620