Java图形化界面GUI:初接触
简介
图形用户界面(Graphical User Interface,简称 GUI,又称图形用户接口)是指采用图形方式显示的计算机操作用户界面。
JavaGUI核心技术: Swing,AWT;
JavaGUI缺点: 界面不美观,需要jre环境;
为什么要学习?
可以写出自己心中想要的小工具
工作可能需要维护Swing界面,概率极小
了解MVC架构,了解监听
AWT
包含了很多类和接口 GUI的
元素:窗口,按钮,文本框
包:java.awt
————————————————
- Frame
- Panel面板
- 布局管理器
package com.zdz.gui; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class Application { public static void main(String[] args) { //textFrame(); //testPanel(); //testFlowLayout(); //testBorderLayout(); //testGridLayout(); demo(); } public static void textFrame(){ Frame frame = new Frame("我的第一个Java图形化界面"); //需要设置可见性 frame.setVisible(true); //设置窗口大小 frame.setSize(500,500); //设置背景色 frame.setBackground( new Color(221, 233, 255)); //弹出的初始位置 frame.setLocation(200,200); //设置大小固定 frame.setResizable(false); MyFrame myFrame = new MyFrame(100, 100, 200, 200, Color.blue); MyFrame myFrame1 = new MyFrame(300, 100, 200, 200, Color.blue); MyFrame myFrame2 = new MyFrame(100, 300, 200, 200, Color.blue); MyFrame myFrame3 = new MyFrame(300, 300, 200, 200, Color.blue); } public static void testPanel(){ Frame frame = new Frame("Panel演示"); Panel panel = new Panel(); //设置布局 frame.setLayout(null); //坐标 frame.setBounds(300,300,500,500); frame.setBackground(new Color(187, 255, 76)); //Panel设置坐标,相对于frame panel.setBounds(50,50,400,400); panel.setBackground(new Color(203, 172, 255)); frame.add(panel); frame.setVisible(true); //监听事件,监听窗口关闭事件 //适配器模式: frame.addWindowListener(new WindowAdapter() { //窗口关闭时要做的事情 @Override public void windowClosing(WindowEvent e) { //结束程序 System.exit(0); } }); } //流式布局 public static void testFlowLayout(){ Frame frame = new Frame(); //组件-按钮 Button button1 = new Button("button1"); Button button2 = new Button("button2"); Button button3 = new Button("button3"); Button button4 = new Button("button4"); //frame.setLayout(new FlowLayout());//流式布局 frame.setLayout(new FlowLayout(FlowLayout.LEFT)); frame.setSize(400,400); frame.add(button1); frame.add(button2); frame.add(button3); frame.add(button4); frame.setVisible(true); } //东南西北中 public static void testBorderLayout(){ Frame frame = new Frame("TestBorderLayout"); Button east = new Button("East"); Button west = new Button("West"); Button south = new Button("South"); Button north = new Button("North"); Button center = new Button("Center"); frame.add(east,BorderLayout.EAST); frame.add(west,BorderLayout.WEST); frame.add(south,BorderLayout.SOUTH); frame.add(north,BorderLayout.NORTH); frame.add(center,BorderLayout.CENTER); frame.setVisible(true); frame.setSize(400,400); } //表格布局 public static void testGridLayout(){ Frame frame = new Frame("TestGirdLayout"); Button btn1 = new Button("btn1"); Button btn2 = new Button("btn2"); Button btn3 = new Button("btn3"); Button btn4 = new Button("btn4"); Button btn5 = new Button("btn5"); Button btn6 = new Button("btn6"); frame.setLayout(new GridLayout(3,2)); frame.add(btn1); frame.add(btn2); frame.add(btn3); frame.add(btn4); frame.add(btn5); frame.add(btn6); frame.setVisible(true); frame.pack(); //frame.setSize(400,400); } public static void demo(){ Frame frame = new Frame(); frame.setSize(500,350); frame.setLocation(500,500); frame.setBackground(Color.blue); frame.setVisible(true); frame.setLayout(new GridLayout(2,1)); //4个面板 Panel panel1 = new Panel(new BorderLayout()); Panel panel2 = new Panel(new GridLayout(2,1)); Panel panel3 = new Panel(new BorderLayout()); Panel panel4 = new Panel(new GridLayout(2,1)); //上面4个 panel1.add(new Button("East-1"),BorderLayout.EAST); panel1.add(new Button("West-1"),BorderLayout.WEST); panel2.add(new Button("p2-btn-1")); panel2.add(new Button("p2-btn-2")); panel1.add(panel2,BorderLayout.CENTER); //下面4个 panel3.add(new Button("East-2"),BorderLayout.EAST); panel3.add(new Button("West-2"),BorderLayout.WEST); for (int i = 1; i <= 4; i++) { panel4.add(new Button("btn"+i)); } panel3.add(panel4,BorderLayout.CENTER); frame.add(panel1); frame.add(panel3); //监听 frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } } class MyFrame extends Frame{ static int id=0;//可能存在多个窗口,我们需要一个计数器 public MyFrame(int x,int y,int w,int h,Color color){ super("MyFrame"+(++id)); setVisible(true); setSize(w,h); setBackground(color); setLocation(x,y); setResizable(false); } }
版权声明:本文为CSDN博主「张生说」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/zhangduang1/article/details/114897778