package app;
import javax.swing.*;
import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
public class Main {
public static void main(String[] args){
//FirstWindow firstWindow = new FirstWindow("213");
//Window window = new window();
//WindowFlow windowFlow = new WindowFlow("123");
//WindowBord windowBord = new WindowBord();
//testBord testBord = new testBord("123");
//WindowCard windowCard = new WindowCard("122");
//WindowBox windowBox = new WindowBox("23");
//WindowGrid windowGrid = new WindowGrid("56");
//WindowNull windowNull = new WindowNull("34");
}
public static class Window {
public Window(){
JFrame frame1 = new JFrame("1");
JFrame frame2 = new JFrame("2");
//设置窗口位置及大小 setSize(width,height)默认坐标(0,0)
frame1.setBounds(500,512,256,128);
frame2.setBounds(1000,512,256,128);
//设置窗口是否可见
frame1.setVisible(true);
//单击右上角关闭图标后隐藏窗口并释放窗体占有的其他资源
frame1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//设置窗口是否可见
frame2.setVisible(true);
//单击右上角关闭图标后结束窗体所在应用程序
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public static class FirstWindow extends JFrame{
public FirstWindow(String s){
//建立一个菜单栏
JMenuBar menubar = new JMenuBar();
//将菜单条添加到窗口的菜单条区域(只能添加一个菜单条)
setJMenuBar(menubar);
//建立指定标题菜单
JMenu menu = new JMenu("file");
//把菜单添加至菜单栏
menubar.add(menu);
//设置窗口标题
setTitle(s);
//设置窗口位置及大小
setBounds(500,500,360,270);
//设置窗口是否显示
setVisible(true);
//构造有标题和图标的菜单项
JMenuItem item1 = new JMenuItem("open", new ImageIcon("src/image/罗小黑0.gif"));
JMenuItem item2 = new JMenuItem("save", new ImageIcon("src/image/罗小黑1.gif"));
//为菜单项item1设置快捷键O
item1.setAccelerator((KeyStroke.getKeyStroke("O")));
为菜单项item2设置快捷键ctrl + s
item2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));
//向菜单项添加由参数item1指定的菜单选项对象
menu.add(item1);
//菜单项之间添加分隔线
menu.addSeparator();
//向菜单项添加由参数item2指定的菜单选项对象
menu.add(item2);
//向菜单项添加指定的选项
menu.add("help");
//验证
validate();
//关闭之后程序隐藏窗口并释放窗体占有的其他资源
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
public static class WindowFlow extends JFrame{
JButton b[];
public WindowFlow(String s){
//设置窗口标题
setTitle(s);
b = new JButton[20];
//创建一个居中对齐的布局对象
FlowLayout flow =new FlowLayout();
//设置对齐方式(左对齐)
flow.setAlignment(FlowLayout.LEFT);
//设置水平间隙
flow.setHgap(2);
//设置垂直间隙
flow.setVgap(8);
//使用这个布局对象
setLayout(flow);
//根据b[]中数字创建按钮
for (int i = 0;i < b.length; i++){
b[i] = new JButton("" + i);
//将按钮添加至窗口中
add(b[i]);
//如果是b[]最后一位数字,则将按钮的大小改变
if (i == b.length-1) {
b[i].setPreferredSize(new Dimension(80, 40));
}
//自检
validate();
//建立窗口位置及大小
setBounds(100,100,200,160);
//设置窗口是否可见
setVisible(true);
//关闭窗口时隐藏窗口并释放所占用资源
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
}
public static class WindowBord {
//最多添加5个组件,额外的需要嵌套或使用其他布局
public WindowBord(){
//设置窗口
JFrame win = new JFrame("12");
//设置窗口位置及大小
win.setBounds(100,100,300,300);
//设置窗口是否可见
win.setVisible(true);
//设置4个按钮
JButton bSouth = new JButton("南"),
bNorth = new JButton("北"),
bEast = new JButton("东"),
bWest = new JButton("西");
//设置中心区域文字
JTextArea bCenter = new JTextArea("中心");
//添加组件
win.add(bNorth, BorderLayout.NORTH);
win.add(bSouth, BorderLayout.SOUTH);
win.add(bEast, BorderLayout.EAST);
win.add(bWest, BorderLayout.WEST);
win.add(bCenter, BorderLayout.CENTER);
//自检
win.validate();
//关闭后隐藏窗口并释放所占用资源
win.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
//写来验证的,可以无视
public static class testBord extends JFrame{
public testBord(String s){
setTitle(s);
setBounds(100,100,500,600);
setVisible(true);
JButton bSouth = new JButton("南"),
bNorth = new JButton("北"),
bEast = new JButton("东"),
bWest = new JButton("西");
//设置中心区域文字
JTextArea bCenter = new JTextArea("中心");
//添加组件
add(bNorth, BorderLayout.NORTH);
add(bSouth, BorderLayout.SOUTH);
add(bEast, BorderLayout.EAST);
add(bWest, BorderLayout.WEST);
add(bCenter, BorderLayout.CENTER);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public static class WindowCard extends JFrame{
JTabbedPane p;
Icon icon[];
String imageName[] = {"src/image/biu0.gif", "src/image/biu1.gif", "src/image/biu2.gif", "src/image/biu3.gif","src/image/biu4.gif"};
public WindowCard(String s){
//设置窗口名称
setTitle(s);
//设置窗口位置及大小
setBounds(100,100,500,600);
//设置窗口是否可见
setVisible(true);
//设置列表icon的元素数量
icon = new Icon[imageName.length];
//设置图标
for (int i = 0;i < icon.length; i++){
icon[i] = new ImageIcon(imageName[i]);
}
//设置选项卡窗格位置
p = new JTabbedPane(JTabbedPane.LEFT);
//添加按钮并设置标题及图标
for (int i = 0; i < icon.length; i++){
int m = i + 1;
p.add("观看第" + m + "个图片", new JButton(icon[i]));
}
//自检
p.validate();
//设置p在窗口中位置(参考Border Layout布局)
add(p,BorderLayout.CENTER);
//自检
validate();
//关闭窗口后结束应用程序
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public static class WindowGrid extends JFrame {
GridLayout grid;
JButton b[];
public WindowGrid(String s) {
setTitle(s);
//设置三行三列的布局
grid = new GridLayout(3,3);
b = new JButton[9];
setBounds(100, 100, 500, 600);
setVisible(true);
//设置并添加9个按钮
for (int i = 0; i < b.length; i++) {
b[i] = new JButton("" + i);
//将按钮添加至窗口中
add(b[i]);
}
//启用布局
setLayout(grid);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
validate();
}
}
public static class WindowNull extends JFrame{
JButton a;
public WindowNull(String s ){
//设置标题
setTitle(s);
//设置窗口是否可见
setVisible(true);
//设置窗口位置及大小
setBounds(30, 40, 500, 560);
//设置布局方式为null
setLayout(null);
//添加按钮
a = new JButton("2");
add(a);
//设置按钮位置及大小
a.setBounds(20,30,250,280);
//关闭窗口后结束窗口所在应用程序
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
}
java布局设计FlowLayout、BorderLayout、CardLayout、GridLayout、BoxLayout及null布局
最新推荐文章于 2023-05-07 12:23:26 发布