Java implements a simple panel in the southeast and northwest

Table of contents

I. Introduction

Two, the code part

1. Code

3. Program running results (the panel pops up) 

4. Codes of knowledge points involved


I. Introduction

1. This code was written by me when I was in school. There are some places that have not been perfectly implemented. Please forgive me and give me more advice!

2. This pop-up window interface can be input according to simple requirements, and display whether it is correct or not. The code setting of this article is to implement a simple panel in the code. At the same time, custom settings can be realized;

3. The system can only run on the console (eclipse and other versions), and needs to be matched with the jdk environment;

4. Here is a special note, if the complete code package name to be pasted is inconsistent with mine, it is specified inconsistently, please change it manually;

Two, the code part

1. Code

package com.edu.p1;

import javax.swing.JFrame;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
public class Test504 extends JFrame {
	public Test504(String title){
		super(title);//窗口的标题
		this.setSize(400,300);
		this.setLocation(800,200);
		JButton jb1,jb2,jb3,jb4,jb5;
		JPanel jp;
		jb1=new JButton("东");
		jb2=new JButton("西");
		jb3=new JButton("南");
		jb4=new JButton("北");
		jb5=new JButton("中");
		jp=new JPanel();
		jp.setLayout(new BorderLayout());//为面板jp设置布局管理器为方位式
		jp.add(jb1,BorderLayout.EAST);//将按钮jb1添加到面板的“东"
		jp.add(jb2,BorderLayout.WEST);
		jp.add(jb3,BorderLayout.SOUTH);
		jp.add(jb4,BorderLayout.NORTH);
		jp.add(jb5,BorderLayout.CENTER);
		this.add(jp);
		this.setVisible(true);
		
	}
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Test504 jp=new Test504("东西南北中");//实例化本类对象
	}

}

3. Program running results (the panel pops up) 

1. Display the result 

 

4. Codes of knowledge points involved

1. Set the layout manager for the panel jp as orientation

Defined value.setLayout(new BorderLayout());

2. Add button jb1 to the "East" of the panel

jp.add(jb1,BorderLayout.EAST);

3. Set the display position of the window on the screen 

this.setLocation(x,y);

4. Set the size of the window

this.setSize(x,y);

Guess you like

Origin blog.csdn.net/weixin_59042297/article/details/129779406