一、简介:
1、Gui的核心技术:Swing、AWT
二、AWT
1、AWT介绍:包含了很多的类和接口,GUI:图形用户编程
2、元素:窗口、按钮、文本框
3、引用的java包:java.awt.*
4、组件和容器
第一个Frame界面(代码以及实现)
package com.zy.lesson01; import java.awt.*; //gui第一个界面 public class TestFrame { public static void main(String[] args) { //frame Frame f = new Frame("我的第一个java图形界面窗口"); //设置窗口可见性 f.setVisible(true); //设置窗口大小 f.setSize(400,400); //设置背景颜色 color f.setBackground(new Color(83,130,1)); //设置窗口弹出的初始值 f.setLocation(200,200); //设置窗口大小固定 f.setResizable(false); } }
注意:发现窗口关闭不掉,需要停止程序运行,才能把窗口给完全关掉。
尝试实现多个窗口弹窗(代码以及实现)(回顾封装)
package com.zy.lesson01; import java.awt.*; public class TestFrame1 { public static void main(String[] args) { //展示多个窗口 Frame MyFrame1 = new MyFrame(100, 100, 200, 200, Color.black); Frame MyFrame2= new MyFrame(300, 100, 200, 200, Color.yellow); Frame MyFrame3 = new MyFrame(100, 300, 200, 200, Color.orange); Frame MyFrame4= new MyFrame(300, 300, 200, 200, Color.magenta); } }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); } }
5、面板 Panel(解决了运行窗口的关闭事件)
package com.zy.lesson01; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; //Panel 可以看作是一个空间,但不能单独存在 public class TestPanel { public static void main(String[] args) { Frame frame = new Frame("panel面板的使用"); Panel panel = new Panel();//存在布局 //设置默认布局 frame.setLayout(null); frame.setBounds(300, 300,500,500); frame.setBackground(new Color(40,161,35)); //panel 设置坐标,相当于frame panel.setBounds(50,50,400,400); panel.setBackground(new Color(193,15,60)); //frame.add(panel)==>看panel的源码,知道为什么panel可以添加到frame中 frame.add(panel); frame.setVisible(true); //监听事件,监听窗口关闭事件, //设配器模式 frame.addWindowListener(new WindowAdapter() { //窗口点击关闭的时候需要做的事情 @Override public void windowClosing(WindowEvent e) { //结束程序 System.exit(0); } }); } }
6、布局管理器
流式布局:
package com.zy.lesson01; import java.awt.*; 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); } }
东西南北中:
package com.zy.lesson01; import java.awt.*; public class TestLayout { public static void main(String[] args) { Frame frame = new Frame("BorderLayout"); Button e= new Button("east"); Button w= new Button("west"); Button s= new Button("south"); Button n= new Button("north"); Button c= new Button("center"); frame.add(e,BorderLayout.EAST); frame.add(w,BorderLayout.WEST); frame.add(s,BorderLayout.SOUTH); frame.add(n,BorderLayout.NORTH); frame.add(c,BorderLayout.CENTER); frame.setSize(200,200); frame.setVisible(true); } }
表格布局 Grid:
package com.zy.lesson01; import java.awt.*; public class TestGridLayout { public static void main(String[] args) { Frame frame = new Frame("TestGridLayout "); 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); } }
练习:
代码如下:
package com.zy.lesson01; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class Test { public static void main(String[] args) { //总frame Frame frame = new Frame(); frame.setSize(400,300); frame.setLocation(300,400); frame.setBackground(Color.magenta); frame.setVisible(true); frame.setLayout(new GridLayout(2,1)); //四个面板 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,2)); panel1.add(new Button("east-1"),BorderLayout.EAST); panel1.add(new Button("wast-1"),BorderLayout.WEST); panel2.add(new Button("p2-btn-1")); panel2.add(new Button("p2-btn-2")); panel1.add(panel2,BorderLayout.CENTER); panel3.add(new Button("east-2"),BorderLayout.EAST); panel3.add(new Button("wast-2"),BorderLayout.WEST); for (int i = 0; i < 4; i++) { panel4.add(new Button("for"+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); } }); } }
7、事件监听
package com.zy.lesson02; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class TestActionEvent { public static void main(String[] args) { //点击按钮,触发事件 Frame frame = new Frame("监听事件"); Button button = new Button(); ///因为,addActionListener()需要一个ActionListener,所以我们需要构造一个ActionListener MyActionListener myActionListener = new MyActionListener(); button.addActionListener(myActionListener); frame.add(button, BorderLayout.CENTER); frame.pack(); windonwClose(frame);//关闭窗口 frame.setVisible(true); } //关闭窗体的事件 private static void windonwClose(Frame frame){ frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); }}); }} //事件监听 class MyActionListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { System.out.println("按钮被点击了"); }}
练习:多个按钮只使用一个监听事件。
8、输入框TextFieid 监听
package com.zy.lesson02; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class TestTestListener { public static void main(String[] args) { new Myframe(); }} class Myframe extends Frame{ public Myframe(){ TextField textField = new TextField(); add(textField); //监听这个文本框输入的文字 MyActionLinstener myActionLinstener = new MyActionLinstener(); //按下enter,就会触发这个输入框的事件 textField.addActionListener(myActionLinstener); //设置编码 textField.setEchoChar('*'); setVisible(true); pack(); addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); }}); }} class MyActionLinstener implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { TextField textField=(TextField) e.getSource();//获取一些资源,返回一个对象 System.out.println(textField.getText());//获取输入框的文字 textField.setText("");//null } }
9、画笔 Paint
package com.zy.lesson03; import java.awt.*; public class TestPaint { public static void main(String[] args) { new mypaint().loadFrame(); } } class mypaint extends Frame{ public void loadFrame(){ setBounds(200,200,600,500); setVisible(true); } @Override //画笔 public void paint(Graphics g) { g.setColor(Color.BLUE); g.drawOval(100,100,100,100); g.fillOval(100,100,100,100);//实心圆 g.setColor(Color.magenta); g.fillRect(150,200,200,200);//矩形 } }
10、鼠标监听
目的:想要实现鼠标画点
package com.zy.lesson03; import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.ArrayList; import java.util.Iterator; public class mouseLinstered { public static void main(String[] args) { new MyFrame("画图"); }} class MyFrame extends Frame{ //画画需要画笔,需要监听鼠标当前的位置,需要集合存储这个点 ArrayList points; public MyFrame(String title){ super(title); setBounds(200,200,400,300); //存鼠标的点 points=new ArrayList(); setVisible(true); //鼠标监听器,针对这个窗口 this.addMouseListener(new MyMouseListener() ); } @Override public void paint(Graphics g) { //画画,监听鼠标的事件 Iterator iterator = points.iterator(); while (iterator.hasNext()){ Point point = (Point)iterator.next(); g.setColor(Color.magenta); g.fillOval(point.x,point.y,10,10); } } //添加一个点到界面上 public void addPaint(Point point){ points.add(point); } //适配器模式 private class MyMouseListener extends MouseAdapter{ //鼠标按下,弹起,按住不放 @Override public void mousePressed(MouseEvent e) { MyFrame frame = (MyFrame) e.getSource(); //点击的时候,就会在界面产生一个点 //这个点就是鼠标的点 frame.addPaint(new Point(e.getX(),e.getY())); //每次点击鼠标都需要重新画一遍 frame.repaint();//刷新 }}}
11、窗口监听
package com.zy.lesson03; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class TestWindow { public static void main(String[] args) { new WindowFrame(); } } class WindowFrame extends Frame{ public WindowFrame(){ setBackground(Color.CYAN); setBounds(100,100,200,200); setVisible(true); // addWindowListener(new myWindowListener()); this.addWindowListener(new WindowAdapter() { //匿名内部类,推荐使用这种方式 //窗口监听事件====>最常用的二个方法 @Override public void windowClosing(WindowEvent e) { System.out.println("您点击了x"); //关闭窗口 System.exit(0); } @Override public void windowActivated(WindowEvent e) { WindowFrame source = (WindowFrame) e.getSource(); source.setTitle("窗口被激活了"); System.out.println("windowActivated"); //窗口激活 } }); } /* class myWindowListener extends WindowAdapter{ @Override public void windowClosing(WindowEvent e) { setVisible(false);//隐藏窗口,通过按钮,隐藏当前窗口 //System.exit(0);//正常退出 } }*/ }
12、键盘监听
package com.zy.lesson03; import java.awt.*; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; public class TestKeyListener { public static void main(String[] args) { new KeyFrame(); } } class KeyFrame extends Frame{ public KeyFrame(){ setBounds(1,2,300,400); setVisible(true); this.addKeyListener(new KeyAdapter() { //键盘按下 @Override public void keyPressed(KeyEvent e) { //获得键盘下的键是哪一个,当前的码 int keyCode = e.getKeyCode(); if (keyCode==KeyEvent.VK_UP){ System.out.println("您按下了上键"); } //根据按下不同的键盘,产生不同的结果 } }); } }
三、Swing
1、窗口、面板
package com.zy.lesson04; import javax.swing.*; import java.awt.*; public class Testjframe1 { public static void main(String[] args) { new MyJFrame().init(); } } class MyJFrame extends JFrame{ public void init(){ this.setTitle("第一个JFrame界面"); this.setBounds(20,30,200,200); this.setVisible(true); //设置文字 Jlabel JLabel label = new JLabel("label文本框"); this.add(label); //文字居中,设置水平对齐 label.setHorizontalAlignment(SwingConstants.CENTER); //获得一个容器 Container contentPane = this.getContentPane(); contentPane.setBackground(Color.orange);; } }
2、弹窗:
JDialog :用来被弹出,默认就有关闭事件,就可以不用写关闭事件
package com.zy.lesson04; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; //主窗口 public class TestDialog extends JFrame { public TestDialog(){ this.setVisible(true); this.setSize(700,500); // this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //JFrame 放东西,容器 Container container=this.getContentPane(); //绝对布局 container.setLayout(null); //按钮 JButton button = new JButton("点击弹出一个对话框");//创建 button.setBounds(30,30,200,50); //点击这个按钮的时候,弹出一个弹窗 button.addActionListener(new ActionListener() {//设置监听器 @Override public void actionPerformed(ActionEvent e) { //弹窗 new MyDialogFrame(); } }); container.add(button); } public static void main(String[] args) { new TestDialog(); }} //弹窗的窗口 class MyDialogFrame extends JDialog{ public MyDialogFrame() { this.setVisible(true); this.setBounds(100,100,500,500); // this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); Container contentPane = this.getContentPane(); contentPane.setLayout(null); contentPane.add(new Label("这是一个弹窗")); } }
3、标签 label 、图标 icon
自己画的图标
package com.zy.lesson04; import javax.swing.*; import java.awt.*; //标签 //图标 Icon 它是一个接口,需要实现类,有frame继承 public class TestLaicon extends JFrame implements Icon { private int width; private int height; public TestLaicon(){ } public TestLaicon(int width,int height){ this.width=width; this.height=height; } public void init(){ TestLaicon laicon = new TestLaicon(15, 15); //图标方法标签上,也可以放在按钮上 JLabel jLabel = new JLabel("iconTest",laicon,SwingConstants.CENTER); Container pane = getContentPane(); pane.add(jLabel); this.setVisible(true); this.setBounds(100,200,400,200); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new TestLaicon().init(); } @Override public void paintIcon(Component c, Graphics g, int x, int y) { g.fillOval(x,y,width,height); } @Override public int getIconWidth() { return this.width=width; } @Override public int getIconHeight() { return this.height=height; } }
加载图片
package com.zy.lesson04; import javax.swing.*; import java.awt.*; import java.net.URL; public class ImageIcondemo extends JFrame { public ImageIcondemo(){ //获取图片的地址 JLabel label = new JLabel("imageicon"); URL url = ImageIcondemo.class.getResource("QQ.png"); ImageIcon imageIcon = new ImageIcon(url); label.setIcon(imageIcon); label.setHorizontalTextPosition(SwingConstants.CENTER); Container pane = getContentPane(); pane.add(label); setBounds(100,200,400,200); setVisible(true); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new ImageIcondemo(); } }
4、面板
Jpanel
package com.zy.lesson05; import javax.swing.*; import java.awt.*; public class TestPanel extends JFrame { public TestPanel(){ Container pane = getContentPane(); pane.setLayout(new GridLayout(2,1,10,10));//后面参数的意思,就是间距 JPanel jPanel = new JPanel(new GridLayout(1,3)); jPanel.add(new JButton("1")); jPanel.add(new JButton("2")); jPanel.add(new JButton("3")); pane.add(jPanel); this.setVisible(true); this.setSize(500,500); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new TestPanel(); } }
JScrollPanel 滚动条:
package com.zy.lesson05; import javax.swing.*; import java.awt.*; public class TestScrolldemo extends JFrame { public TestScrolldemo(){ Container contentPane = this.getContentPane(); //文本域 JTextArea jTextArea = new JTextArea(20,50); jTextArea.setText("哈哈哈哈哈"); //Scroll面板 JScrollPane jScrollPane = new JScrollPane(jTextArea); contentPane.add(jScrollPane); this.setVisible(true); this.setBounds(100,300,150,150); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new TestScrolldemo(); } }
5、按钮
图片按钮、单选框、多选框
package com.zy.lesson05; import javax.swing.*; import java.awt.*; import java.net.URL; //图片按钮 public class JButtonDemo extends JFrame { public JButtonDemo(){ Container pane = this.getContentPane(); //将一个图片变成一个图标 URL resource = JButtonDemo.class.getResource("QQ.png"); ImageIcon icon = new ImageIcon(resource); //把这个图标放在按钮上 JButton jButton = new JButton(); jButton.setIcon(icon); jButton.setToolTipText("这是一个图片按钮"); pane.add(jButton); this.setVisible(true); this.setSize(500,300); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new JButtonDemo(); } }
单选框按钮
package com.zy.lesson05; import javax.swing.*; import java.awt.*; public class JButtonDemo1 extends JFrame { public JButtonDemo1(){ Container pane = this.getContentPane(); //单选框 JRadioButton button1 = new JRadioButton("button1"); JRadioButton button2 = new JRadioButton("button2"); JRadioButton button3 = new JRadioButton("button3"); //由于单选框只能选一个,因此要对它们进行分组 ButtonGroup group = new ButtonGroup(); group.add(button1); group.add(button2); group.add(button3); pane.add(button1,BorderLayout.CENTER); pane.add(button2,BorderLayout.NORTH); pane.add(button3,BorderLayout.SOUTH); this.setVisible(true); this.setSize(500,300); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new JButtonDemo1(); } }
多选框按钮
package com.zy.lesson05; import javax.swing.*; import java.awt.*; public class JButtonDemo2 extends JFrame{ public JButtonDemo2(){ Container pane = this.getContentPane(); //多选框 JCheckBox button1 = new JCheckBox("button1"); JCheckBox button2 = new JCheckBox("button2"); JCheckBox button3 = new JCheckBox("button3"); pane.add(button1,BorderLayout.CENTER); pane.add(button2,BorderLayout.NORTH); pane.add(button3,BorderLayout.SOUTH); this.setVisible(true); this.setSize(500,300); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new JButtonDemo2(); } }
6、列表(应用场景 ,或者一些某一个选项,列表==》用于展示信息,一般是动态扩容)
下拉框
package com.zy.lesson06; import javax.swing.*; import java.awt.*; public class TestComboxDemo1 extends JFrame { public TestComboxDemo1(){ Container container = getContentPane(); JComboBox jComboBox = new JComboBox(); jComboBox.addItem(null); jComboBox.addItem("正在热映"); jComboBox.addItem("已下架"); jComboBox.addItem("即将上映"); container.add(jComboBox); this.setVisible(true); this.setSize(300,200); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new TestComboxDemo1(); } }
列表框
package com.zy.lesson06; import javax.swing.*; import java.awt.*; import java.util.Vector; public class TestTableDemo2 extends JFrame { public TestTableDemo2(){ Container container = getContentPane(); //生成列表的内容 //String[] contents={"1","2","3"};//静态 Vector vector = new Vector(); //动态 //列表需要放内容 // JList list = new JList(cntentso); JList list = new JList(vector); //container.add(list); vector.add("zhangsan"); vector.add("lishi"); vector.add("wanger"); container.add(list); this.setVisible(true); this.setSize(300,200); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new TestTableDemo2(); } }
7、文本框、密码框、文本域(与面板一起使用)
package com.zy.lesson06; import javax.swing.*; import java.awt.*; public class Testtextdemo1 extends JFrame { public Testtextdemo1(){ Container container = getContentPane(); //文本框 /* JTextField jTextField = new JTextField("hello"); JTextField jTextField1 = new JTextField("world",20); container.add(jTextField,BorderLayout.NORTH); container.add(jTextField1,BorderLayout.SOUTH);*/ /* 密码框 JPasswordField jPasswordField = new JPasswordField(); jPasswordField.setEchoChar('*'); container.add(jPasswordField); */ //文本域 JTextArea jTextArea = new JTextArea(20,50); jTextArea.setText("哈哈哈哈哈"); //Scroll面板 JScrollPane jScrollPane = new JScrollPane(jTextArea); container.add(jScrollPane); this.setVisible(true); this.setSize(300,200); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new Testtextdemo1(); }}
全部代码详情:https://gitee.com/zhaoyuanq994/project