java初学者,最近写了两个界面,想在一个界面中通过按钮跳入另一个界面,找了一些方法都是把两个界面直接写在了一个类中,最终发现了一个解决的方法。直接上代码了,第一个界面使用的分层网格,第二个界面使用的盒式布局容器Box。

窗口图片

点击注册,进入下一个界面

 

如果想学习其中的添加背景图片,点进这里?

https://blog.csdn.net/he13733625052/article/details/90177674

下面是代码:

 

//main函数
package cn.text;

import cn.ui.UIinterface;
import cn.ui.UIzhuce;

public class Text {
	public static void main(String[] args) {
		UIinterface ui = new UIinterface();
	}

}
//第一个界面
package cn.ui;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
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.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class UIinterface extends JFrame{
	JLayeredPane pane = new JLayeredPane();
	//使用的
	JLabel label;
	JLabel label1;
	JPanel panel1 = new JPanel();
	JPanel panel2 = new JPanel();
	JPanel panel3 = new JPanel();
	JPanel panel4 = new JPanel();
	JTextField field1 = new JTextField("学号");
	JTextField field2 = new JTextField("密码");
	JButton Land = new JButton("登  陆");
	JButton register = new JButton("注  册");
	ImageIcon image;
	
	public UIinterface() {
		image = new ImageIcon("img/2.png");
		//设置组件透明
		field1.setOpaque(false);        
		field2.setOpaque(false);        
		Land.setOpaque(false);        
		register.setOpaque(false);        
		panel1.setOpaque(false);        
		panel2.setOpaque(false);       
		panel3.setOpaque(false);
		panel4.setOpaque(false);
	    
		label = new JLabel(image);
		panel1.setBounds(0, 0,image.getIconWidth(), image.getIconHeight());
		panel1.add(label);
		label1 = new JLabel("图书管理系统");
		
		label1.setFont(new Font("宋体",Font.LAYOUT_NO_LIMIT_CONTEXT,70));
		panel4.add(label1);
		panel4.setBounds(70, 0, 500, 300);;
		panel4.setLayout(new GridLayout(1,1));
		
		field1.setFont(new Font("黑体",Font.BOLD,40));
		field1.setForeground(Color.BLACK);
		field2.setFont(new Font("黑体",Font.BOLD,40));
		field2.setForeground(Color.BLACK);
		panel2.setLayout(new GridLayout(2,1));		
		panel2.setBounds(110, 300, 350, 100);						
		panel2.add(field1);		
		panel2.add(field2);	
		
		panel3.setBounds(100,500,350,50);	
		Land.setFont(new Font("黑体",Font.BOLD,30));
		Land.setForeground(Color.BLACK);
		register.setFont(new Font("黑体",Font.BOLD,30));
		register.setForeground(Color.BLACK);
		panel3.add(Land);		
		panel3.add(register);				
		Land.setSize(50,50);		
		register.setSize(50,50);
		
		pane.add(panel1,JLayeredPane.DEFAULT_LAYER);//带有标签的面板放在最底层		
		pane.add(panel2,JLayeredPane.MODAL_LAYER);		
		pane.add(panel3,JLayeredPane.PALETTE_LAYER);
		pane.add(panel4,JLayeredPane.DRAG_LAYER);
		
		register.addActionListener(new ActionListener() {
			//给需要跳转的按钮加上监听。
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub\
				closeThis();
				new UIzhuce();
				//进入到新界面 UIzhuec
			}

			private void closeThis() {
				// TODO Auto-generated method stub
				
			}
		
		});
			
		
		
		this.setTitle("图书管理系统");		
		this.setBounds(500,30,image.getIconWidth(), image.getIconHeight());		
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);		
		this.setLayeredPane(pane);		
		this.setVisible(true);	
		} 
		
		
		
		
		
		
	}
			
//第二个界面
package cn.ui;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Label;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class UIzhuce extends JFrame {
	
	Box boxH;      //行式盒
	Box boxone,boxtwo;    //列式盒
	JTextField text = new JTextField(15);
	JTextField text1 = new JTextField(15);
	JTextField text2 = new JTextField(15);
	JTextField text3 = new JTextField(15);
	JTextField text4 = new JTextField(15);
	JButton butt = new JButton("确定");
	JButton butt1 = new JButton("取消");
	Label bel1 = new Label("学号");
	Label bel2 = new Label("姓名");
	Label bel3 = new Label("班级");
	Label bel4 = new Label("密码");
	Label bel5 = new Label("确认密码");
	void init() {
		this.setLayout(new FlowLayout());//布局类
		
		boxH = Box.createHorizontalBox(); //行
		boxone = Box.createVerticalBox(); //列
		boxtwo = Box.createVerticalBox(); //列

		Dimension dim = new Dimension(50, 50);//这是个调整文本框合适大小的方法
		text.setPreferredSize(dim);
		text1.setPreferredSize(dim);
		text2.setPreferredSize(dim);
		text3.setPreferredSize(dim);
		text4.setPreferredSize(dim);
		text.setFont(new Font("宋体",Font.BOLD,30));
		text1.setFont(new Font("宋体",Font.BOLD,30));
		text2.setFont(new Font("宋体",Font.BOLD,30));
		text3.setFont(new Font("宋体",Font.BOLD,30));
		text4.setFont(new Font("宋体",Font.BOLD,30));
		
		
		bel1.setFont(new Font("宋体",Font.BOLD,30));
		bel2.setFont(new Font("宋体",Font.BOLD,30));
		bel3.setFont(new Font("宋体",Font.BOLD,30));
		bel4.setFont(new Font("宋体",Font.BOLD,30));
		bel5.setFont(new Font("宋体",Font.BOLD,30));
		
		butt.setFont(new Font("宋体",Font.BOLD,30));
		butt1.setFont(new Font("宋体",Font.BOLD,30));
		
		boxone.add(bel1);
		
		boxone.add(bel2);
		
		boxone.add(bel3);
		
		boxone.add(bel4);
		boxone.add(bel5);
		
		boxone.add(butt);
		
		boxtwo.add(text);
	
		boxtwo.add(text1);
		
		boxtwo.add(text2);
		
		boxtwo.add(text3);
		
		
		boxtwo.add(text4);
		
		boxtwo.add(butt1);
		boxH.add(boxone);
		boxH.add(Box.createHorizontalStrut(10));//调整同行标签和文本框的距离。
		boxH.add(boxtwo);
		this.add(boxH);
	}

	
	public UIzhuce() {
		this.init();
		this.setTitle("注册表");
		
		this.setBounds(100, 100,500,500);
		this.setVisible(true);
		
	}
	


}
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐