Bootstrap

图形界面1)

Java学习

day27

布局管理

目录

  1. 对话框
  2. 面板
  3. 布局
  4. 单/多选按钮
  5. 列表

包资源管理器
1、对话框
运行代码如下;

package Demo01;

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

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
//对话框
public class DialogDemo01 extends JDialog{
	public DialogDemo01(JFrame jframe) {
		super(jframe,false);
		
		this.setBounds(100,100, 300, 300);
		Container container1 = this.getContentPane();
		container1.setLayout(null);
		
		JButton button1 = new JButton("我要5班");
		button1.setBounds(30,30,200,100);
		
		container1.add(button1);
	}
	
	public static void main(String[] args) {
		JFrame j = new JFrame();
		j.setVisible(true);
		j.setBounds(100, 100, 700, 500);
		j.setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		Container container = j.getContentPane();
		container.setLayout(null);
		JButton button = new JButton("我要登录");
		button.setBounds(30,30,200,100);
		button.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				DialogDemo01 s = new DialogDemo01(j);
				s.setVisible(true);
				
				System.out.println("我被点击了");
			}
		});
		container.add(button);
	}

}
运行结果:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210607131845852.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzU1Njg5NTEy,size_16,color_FFFFFF,t_70)

2、面板
(1)无滚动条的面板

package Demo02;

import java.awt.Container;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
//无滚动条的面板
//面板作为容器不能单独存在,必须放在其他容器里面(JFrame,JDialog)
public class JPaneDemo extends JFrame {
	public JPaneDemo() {
		Container container1 = this.getContentPane();
		container1.setLayout(new GridLayout(2,1,10,10));
		
		JPanel panel = new JPanel(new GridLayout(1,3));//面板
		panel.add(new JButton("1"));
		panel.add(new JButton("2"));
		panel.add(new JButton("3"));
		
		container1.add(panel);
		
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setVisible(true);
		this.setBounds(100, 100, 500, 500);
	}
	
	public static void main(String[] args) {
		new JPaneDemo();
	}
}

运行结果:
无滚到条

(2)有滚到条的面板

package Demo02;

import java.awt.Container;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
//有滚动条的面板
public class JScrollDemo extends JFrame {
	public JScrollDemo() {
		Container container1 = this.getContentPane();
		//文本域
		JTextArea textarea = new JTextArea(20,50);
		textarea.setText("大数据5班");
		
		//scroll面板
		JScrollPane scorllpane = new JScrollPane(textarea);
		//JPanel panel = new JPanel();
		//scrollpane.add(textarea);
		container1.add(scorllpane);
	
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setVisible(true);
		this.setBounds(100, 100, 500, 500);
	}
	
	public static void main(String[] args) {
		new JScrollDemo();
	}
}

运行结果:
有滚动条

3、布局
(1)流式布局

package Demo03;

import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
//流式布局
public class TestFlowLayout {
	public static void main(String[] args) {
		JFrame j = new JFrame();
		j.setVisible(true);
		j.setBounds(100, 100, 700, 500);
		j.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		//流式布局
		//j.setLayout(new FlowLayout());不传参数,默认居中
		//j.setLayout(new FlowLayout(FlowLayout.RIGHT));//靠右
		j.setLayout(new FlowLayout(FlowLayout.LEFT));//靠左
		
		JButton button1 = new JButton("button1");
		JButton button2 = new JButton("button2");
		JButton button3 = new JButton("button3");
		JButton button4 = new JButton("button4");
		JButton button5 = new JButton("button5");
		JButton button6 = new JButton("button6");
		
		j.add(button1);
		j.add(button2);
		j.add(button3);
		j.add(button4);
		j.add(button5);
		j.add(button6);
	}
}

运行结果:
流式布局

(2)方位布局

package Demo03;
import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
//边界(方位)布局(有特定方位)
public class TestBorderLayout {
	public static void main(String[] args) {
		JFrame j = new JFrame();
		j.setVisible(true);
		j.setBounds(100, 100, 700, 500);
		j.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		JButton east = new JButton("east");
		JButton west = new JButton("west");
		JButton south = new JButton("south");
		JButton north = new JButton("north");
		JButton center = new JButton("center");
		
		j.add(east,BorderLayout.EAST);
		j.add(west,BorderLayout.WEST);
		j.add(south,BorderLayout.SOUTH);
		j.add(north,BorderLayout.NORTH);
		j.add(center,BorderLayout.CENTER);
	}
}

运行结果:
方位布局

(3)网格布局

package Demo03;

import java.awt.FlowLayout;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
//网格布局
public class TestGrid {
	public static void main(String[] args) {
		JFrame j = new JFrame();
		j.setVisible(true);
		j.setBounds(100, 100, 700, 500);
		j.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		j.setLayout(new GridLayout(3,2));
		
		JButton button1 = new JButton("button1");
		//button1.setSize(50, 50);
		JButton button2 = new JButton("button2");
		JButton button3 = new JButton("button3");
		JButton button4 = new JButton("button4");
		JButton button5 = new JButton("button5");
		//JButton button6 = new JButton("button6");
		
		j.add(button1);
		j.add(button2);
		j.add(button3);
		j.add(button4);
		j.add(button5);
		//j.add(button6);
		//j.pack();
	}
}

运行结果:
网格布局

4、单/多选按钮
(1)单选按钮

package Demo04;

import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
//单选按钮
public class RadioButton extends JFrame {
	public RadioButton() {
		Container container1 = this.getContentPane();
		//单选框
		JRadioButton radiobutton1 = new JRadioButton("单选1");
		JRadioButton radiobutton2 = new JRadioButton("JRadioButton2");
		JRadioButton radiobutton3 = new JRadioButton("JRadioButton3");
		JRadioButton radiobutton4 = new JRadioButton("JRadioButton4");
		
		ButtonGroup group = new ButtonGroup();
		group.add(radiobutton1);
		group.add(radiobutton2);
		group.add(radiobutton3);
		container1.add(radiobutton1,BorderLayout.CENTER);
		container1.add(radiobutton2,BorderLayout.NORTH);
		container1.add(radiobutton3,BorderLayout.SOUTH);
		container1.add(radiobutton4,BorderLayout.EAST);
	
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setVisible(true);
		this.setBounds(100, 100, 500, 500);
	}
	
	public static void main(String[] args) {
		new RadioButton();
	}
	
}

运行结果:
单选框
(2)多选按钮

package Demo04;

import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
//多选按钮
public class CheckBox extends JFrame {
	public CheckBox() {
		
		Container container1 = this.getContentPane();
		//多选框
		JCheckBox checkbox1= new JCheckBox("自动登录");
		JCheckBox checkbox2= new JCheckBox("记住密码");
		JCheckBox checkbox3= new JCheckBox("找回密码");
		
		container1.add(checkbox1,BorderLayout.CENTER);
		container1.add(checkbox2,BorderLayout.WEST);
		container1.add(checkbox3,BorderLayout.EAST);
	
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setVisible(true);
		this.setBounds(100, 100, 500, 500);
	}
	
	public static void main(String[] args) {
		new CheckBox();
	}
	
}

运行结果:
多选框

5、列表
(1)下拉列表

package Demo05;

import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.ButtonGroup;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
//下拉列表
public class TextComBoxDemo01 extends JFrame {
	public TextComBoxDemo01() {
		Container container1 = this.getContentPane();
		
		JComboBox status = new JComboBox();
		status.addItem("正在学习");
		status.addItem("正在睡觉");
		status.addItem("正在看电视");
		container1.add(status);
		
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setVisible(true);
		this.setBounds(100, 100, 500, 500);
	}
	
	public static void main(String[] args) {
		new TextComBoxDemo01();
	}
	
}

运行结果:
下拉列表

(2)无下拉选择的列表

package Demo05;

import java.awt.Container;
import java.util.Vector;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;

public class TextComBoxDemo02 extends JFrame {
	public TextComBoxDemo02() {
		Container container1 = this.getContentPane();
		
		Vector<Object> contents = new Vector();
		JList jlist = new JList(contents);
		
		contents.add("qq");
		contents.add("微信");
		contents.add("交友");
		contents.add("聊天");
		
		container1.add(jlist);
		
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setVisible(true);
		this.setBounds(100, 100, 500, 500);
	}
	
	public static void main(String[] args) {
		new TextComBoxDemo02();
	}
	
}

运行结果:
列表

tip:2020080605006

;