GUI编程
简介
GUI: Swing AWT
-
界面不美观
-
需要jar环境
为什么我们要学习
-
可以写出自己心中想要的一些小工具
-
工作时候,也可能维护到swing界面,概率贼小
-
了解MVC架构,了解监听!
AWT
AWT介绍
-
包含了很多类和接口!
-
元素:窗口、按钮、文本框
组件和容器
frame
public class TestFrame { public static void main(String[] args) { //Frame,JDK Frame frame = new Frame("我的第一个Java图形界面窗口"); //需要设置可见性 w h frame.setVisible(true); //设置窗口大小 frame.setSize(400,400); //设置背景颜色 Color frame.setBackground(new Color(185, 222, 184)); //弹出的初始位置 frame.setLocation(200,200); //设置大小固定 frame.setResizable(false); } }
-
-
问题:发现窗口关闭不掉,停止Java程序
package bitter01; import java.awt.*; public class TestFrame02 { public static void main(String[] args) { //展示多个窗口 new MyFrame myFrame = new MyFrame(100, 100, 200, 200, Color.blue); MyFrame myFrame1 = new MyFrame(300, 100, 200, 200, Color.yellow); MyFrame myFrame2 = new MyFrame(100, 300, 200, 200, Color.pink); MyFrame myFrame3 = new MyFrame(300, 300, 200, 200, Color.green); } } class MyFrame extends Frame{ static int id = 0;//可能存在多个窗口,我们需要一个计数器 public MyFrame(int x,int y,int w,int h,Color color){ super("MyFrame+"+(++id)); setBackground(color); setBounds(x,y,w,h); setVisible(true); } }
面板panel
package bitter01; import java.awt.*; //panel 可以看成是一个空间,但是不能单独存在 public class TestPanel { public static void main(String[] args) { Frame frame = new Frame(); //布局的概念 Panel panel = new Panel(); //设置布局 frame.setLayout(null); //坐标 frame.setBounds(300,300,500,500); frame.setBackground(new Color(169, 70, 176)); //panel也可以设置坐标,相对于panel panel.setBounds(50,50,400,400); panel.setBackground(new Color(118, 86, 243)); //frame.add(panel) frame.add(panel); frame.setVisible(true); } }
package bitter01; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; //panel 可以看成是一个空间,但是不能单独存在 public class TestPanel { public static void main(String[] args) { Frame frame = new Frame(); //布局的概念 Panel panel = new Panel(); //设置布局 frame.setLayout(null); //坐标 frame.setBounds(300,300,500,500); frame.setBackground(new Color(72, 239, 102)); //panel也可以设置坐标,相对于panel panel.setBounds(50,50,400,400); panel.setBackground(new Color(118, 86, 243)); //frame.add(panel) frame.add(panel); frame.setVisible(true); //监听事件,监听窗口关闭事件 Sysetm.exit(0); //适配器模式: frame.addWindowListener(new WindowAdapter() { //窗口点击关闭的时候需要要做的事情 @Override public void windowClosing(WindowEvent e) { //结束程序 System.exit(0); } }); } } /* * frame.addWindowListener(new WindowListener() { @Override public void windowOpened(WindowEvent e) { } @Override public void windowClosing(WindowEvent e) { } @Override public void windowClosed(WindowEvent e) { } @Override public void windowIconified(WindowEvent e) { } @Override public void windowDeiconified(WindowEvent e) { } @Override public void windowActivated(WindowEvent e) { } @Override public void windowDeactivated(WindowEvent e) { } });*/
解决了关闭的问题
布局管理器
-
流式布局
-
东西南北中
-
表格布局
-
流式布局
package bitter01; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class TestFlowLayout { public static void main(String[] args) { Frame frame = new Frame(); //组件 --按钮 Button button1 = new Button("button1"); Button button2 = new Button("button2"); Button button3 = new Button("button3"); //设置为流式布局 //frame.setLayout(new FlowLayout()); //frame.setLayout(new FlowLayout(FlowLayout.LEFT)); frame.setLayout(new FlowLayout(FlowLayout.RIGHT)); frame.setSize(200,200); //把按钮添加上去 frame.add(button1); frame.add(button2); frame.add(button3); frame.setVisible(true); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } }
-
东西南北中
package bitter01; import sun.swing.text.html.FrameEditorPaneTag; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class TestBorderLayout { public static void main(String[] args) { 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.setSize(200,200); frame.setVisible(true); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } }
-
表格布局 Grid
package bitter01; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class TestGrid { public static void main(String[] args) { Frame frame = new Frame(); 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.pack();//java函数 自动布局 frame.setVisible(true); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } }