GUI编程
1. 简介
GUI的核心技术:Swing AWT
2. AWT
2.1 Awt 介绍
- awt 指抽象的窗口工具,包含了很多类和接口,用于GUI编程:图形用户界面编程
- 元素:窗口,按钮,文本框
- java.awt
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-njBOlpiZ-1645521210902)(C:\Users\Lr\AppData\Roaming\Typora\typora-user-images\image-20220126114737811.png)]
2.2 组件和容器
1.Frame(窗口)
import javax.swing.*;
import java.awt.*;//GUI的第一个界面
public class TestFrame {public static void main(String[] args) {//FrameFrame frame= new Frame("我得第一个Java图像界面窗口");//需要设置可见性frame.setVisible(true);//设置窗口大小frame.setSize(400,400);//设置背景颜色 Colorframe.setBackground(new Color(153, 114, 35));//弹出的初始位置frame.setLocation(200,200);//设置大小固定frame.setResizable(false);}
}
[外链图片转存
失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UdbnPHHP-1645521210903)(C:\Users\Lr\AppData\Roaming\Typora\typora-user-images\image-20220126141152305.png)]
问题:窗口无法关闭,只能停止Java程序
封装:
import java.awt.*;public class TestFrame2 {public static void main(String[] args){//展示多个弹窗 newMyFrame myFrame1 = new MyFrame(100,100,200,200,Color.orange);MyFrame myFrame2 = new MyFrame(300,100,200,200,Color.gray);MyFrame myFrame3 = new MyFrame(100,300,200,200,Color.green);MyFrame myFrame4 = new MyFrame(300,300,200,200,Color.pink);}
}
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);}
}
[外链图片转
存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vg6Gcchp-1645521210904)(C:\Users\Lr\AppData\Roaming\Typora\typora-user-images\image-20220126144912544.png)]
2.面板Panel
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;//Panel,可以看成是一个空间,但是不能单独存在 需要放在Frame上
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(196, 94, 97));//panel设置坐标,相对于framepanel.setBounds(50,50,249,249);panel.setBackground(new Color(255, 175, 175));//frame.add(panel)frame.add(panel);frame.setVisible(true);//监听事件,监听窗口关闭事件System.exit(0);//适配器模式:frame.addWindowListener(new WindowAdapter() {//窗口点击关闭的时候要做的事情@Overridepublic void windowClosing(WindowEvent e) {//结束程序System.exit(0);}});}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-myLSVuX6-1645521210905)(C:\Users\Lr\App
Data\Roaming\Typora\typora-user-images\image-20220126153458578.png)]
2.3 布局管理器
-
流式布局
-
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.setSize(200,200);//把按钮添加上去frame.add(button1);frame.add(button2);frame.add(button3);frame.setVisible(true);} }
-
东西南北中布局
-
import java.awt.*;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);} }
[外链
图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4T5KyPsp-1645521210906)(C:\Users\Lr\AppData\Roaming\Typora\typora-user-images\image-20220126190301807.png)]
-
表格布局
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);} }
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-T9aZbc25-1645521210906)(C:\Users\Lr\AppData\Ro
aming\Typora\typora-user-images\image-20220126191159287.png)]
-
练习
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nUE4AJ5a-1645521210907)(C:\Users\Lr\AppData\Roaming\Typora\typora-user-images\image-20220126212455254.png)]
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;public class TestAll {public static void main(String[] args){//总的窗口FrameFrame frame = new Frame();frame.setSize(400,300);frame.setLocation(300,400);frame.setBackground(Color.gray);frame.setVisible(true);//表格布局frame.setLayout(new GridLayout(2,1));//4个面板Panel p1 = new Panel(new BorderLayout());Panel p2 = new Panel(new GridLayout(2,1));Panel p3 = new Panel(new BorderLayout());Panel p4 = new Panel(new GridLayout(2,2));// 上部分p1.add(new Button("East-1"),BorderLayout.EAST);p1.add(new Button("West-1"),BorderLayout.WEST);p2.add(new Button("p2-btn-1"));p2.add(new Button("p2-btn-2"));p1.add(p2,BorderLayout.CENTER);//下部分p3.add(new Button("East-2"),BorderLayout.EAST);p3.add(new Button("West-2"),BorderLayout.WEST);//下部分的中间4个for (int i = 0; i < 4; i++) {p4.add(new Button("for-" + i));}p3.add(p4,BorderLayout.CENTER);frame.add(p1);frame.add(p3);//监听关闭窗口frame.addWindowListener(new WindowAdapter(){@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);}});}}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TYRiJXGl-16![在这里插入图片描述](https://img-blog.csdnimg.cn/ef53429900c745
bc92c9359bd5cbfbc0.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5Y-q5oOz5q-P5aSp5YGl5bq355qE5p2O5p-Q5Lq6,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center)
45521210908)(C:\Users\Lr\AppData\Roaming\Typora\typora-user-images\image-20220126220927648.png)]
2.4 事件监听
事件监听:当某个事情发生的时候,干什么?
package com.li.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,因此需要构造一个ActionListenerMyActionListener myActionListener = new MyActionListener();button.addActionListener(myActionListener);frame.add(button,BorderLayout.CENTER);frame.pack();windowClose(frame);//关闭窗口frame.setVisible(true);}//关闭窗体的事件private static void windowClose(Frame frame){frame.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);}});}
}
//事件监听
class MyActionListener implements ActionListener{@Overridepublic void actionPerformed(ActionEvent e) {System.out.println("test");}
}
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;public class TestActionEventTwo {public static void main(String[] args) {//两个按钮,实现同一个监听//开始 停止Frame frame = new Frame("开始-停止");Button button1 = new Button("start");Button button2= new Button("stop");//可以显示的定义触发会返回的命令,如果不显示定义,则会走默认的值//可以多个按钮只写一个监听类button2.setActionCommand("button2-stop");MyMonitor myMonitor = new MyMonitor();button1.addActionListener(myMonitor);button2.addActionListener(myMonitor);frame.add(button1,BorderLayout.NORTH);frame.add(button2,BorderLayout.SOUTH);frame.pack();//自适应frame.setVisible(true);}
}class MyMonitor implements ActionListener {@Overridepublic void actionPerformed(ActionEvent e) {//e.getActionCommand() 获得按钮的信息System.out.println("按钮被点击了:msg-> " + e.getActionCommand());}
}
2.5 输入框TextField监听
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;public class TestText01 {public static void main(String[] args) {//启动new MyFrame();}
}class MyFrame extends Frame {public MyFrame(){TextField textField = new TextField();add(textField);//监听这个文本框输入的文字MyActionListener2 myActionListener2 = new MyActionListener2();//按下enter 就会触发这个输入框的事件textField.addActionListener(myActionListener2);//设置替换编码textField.setEchoChar('*');setVisible(true);pack();}}
class MyActionListener2 implements ActionListener{@Overridepublic void actionPerformed(ActionEvent e) {TextField field = (TextField) e.getSource();//获得一些资源,返回的一个对象System.out.println(field.getText());//获得输入框的文本field.setText("");//按enter后文本框为空}}
2.6 简易计算器,组合+内部类
- 基础写法
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;//简易计算器
public class TestCalc {public static void main(String[] args) {new Calculator();}
}
//计算器类
class Calculator extends Frame {public Calculator() { //构造器//3个文本框TextField num1 = new TextField(10);TextField num2 = new TextField(10);TextField num3 = new TextField(20);//1个按钮Button button = new Button("=");button.addActionListener(new MyCalculatorListener(num1,num2,num3));//1个标签Label label = new Label("+");//布局setLayout(new FlowLayout());add(num1);add(label);add(num2);add(button);add(num3);pack();setVisible(true);}
}
//监听器类
class MyCalculatorListener implements ActionListener {//获取三个变量private TextField num1,num2,num3;public MyCalculatorListener(TextField num1,TextField num2,TextField num3) {this.num1 = num1;this.num2 = num2;this.num3= num3;}@Overridepublic void actionPerformed(ActionEvent e) {//1.获得加数和被加数int n1 = Integer.parseInt(num1.getText());//Integer 将String类型转换为int型int n2 = Integer.parseInt(num2.getText());//2.将这个值加法运算后,放到第三个框num3.setText(""+(n1+n2));//3.清除前两个框num1.setText("");num2.setText("");}
}
-
面向对象写法
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;//简易计算器 public class TestCalc {public static void main(String[] args) {new Calculator().loadFrame();} } //计算器类 class Calculator extends Frame {//属性TextField num1,num2,num3;//方法public void loadFrame(){//3个文本框num1 = new TextField(10);num2 = new TextField(10);num3 = new TextField(20);//1个按钮Button button = new Button("=");button.addActionListener(new MyCalculatorListener(this));//1个标签Label label = new Label("+");//布局setLayout(new FlowLayout());add(num1);add(label);add(num2);add(button);add(num3);pack();setVisible(true);}} //监听器类 class MyCalculatorListener implements ActionListener {//获取计算器这个对象,在一个类中组合另一个类;Calculator calculator = null;public MyCalculatorListener(Calculator calculator) {this.calculator = calculator;}@Overridepublic void actionPerformed(ActionEvent e) {//1.获得加数和被加数int n1 = Integer.parseInt(calculator.num1.getText());int n2 = Integer.parseInt(calculator.num2.getText());//2.将这个值加法运算后,放到第三个框calculator.num3.setText(""+(n1+n2));//3.清除前两个框calculator.num1.setText("");calculator.num2.setText("");} }
-
内部类:
更好的包装
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;//简易计算器 public class TestCalc {public static void main(String[] args) {new Calculator().loadFrame();} } //计算器类 class Calculator extends Frame {//属性TextField num1,num2,num3;//方法public void loadFrame(){//3个文本框num1 = new TextField(10);num2 = new TextField(10);num3 = new TextField(20);//1个按钮Button button = new Button("=");button.addActionListener(new MyCalculatorListener());//1个标签Label label = new Label("+");//布局setLayout(new FlowLayout());add(num1);add(label);add(num2);add(button);add(num3);pack();setVisible(true);}//监听器类private class MyCalculatorListener implements ActionListener {@Overridepublic void actionPerformed(ActionEvent e) {//1.获得加数和被加数int n1 = Integer.parseInt(num1.getText());int n2 = Integer.parseInt(num2.getText());//2.将这个值加法运算后,放到第三个框num3.setText(""+(n1+n2));//3.清除前两个框num1.setText("");num2.setText("");}} }
2.7 画笔
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);}//画笔@Overridepublic void paint(Graphics g) {// super.paint(g);//画笔需要有颜色 可以画画g.setColor(Color.red);g.drawOval(100,100,100,100);g.fillOval(100,100,100,100);//实心圆g.setColor(Color.GREEN);g.fillRect(150,200,200,200);//画笔用完,要还原到最初的颜色。}}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-syqmsNEZ-1645521210908)(C:\Users\Lr\AppData\Roaming\Typora\typora-user-images\image-20220129145236535.png)]
2.8 鼠标监听
目的:实现鼠标画画
首先明确需要的事件:
-
画板(画板和画笔绑定在一起)
-
存点的集合
-
画笔(读取2中存的点)
-
鼠标:监听事件(x,y)将点存在2中;(需要实现鼠标每点击一次就重画一次,调用repaint()方法。)
import java.awt.*; import java.awt.event.*; import java.util.ArrayList; import java.util.Iterator; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent;//鼠标监听事件 public class TestMouseListener {public static void main(String[] args) {new MyFrame("画图");} }//自己的类 class MyFrame extends Frame{//画画需要画笔,需要监听鼠标当前的位置,需要集合来存储这个点ArrayList points;//构造器public MyFrame(String title) {super(title);//super: 代表父类对象的应用,可以调用父类的title构造方法,给窗体添加titlesetBounds(200, 200, 400, 400);//存鼠标点击产生的点points=new ArrayList<>();setVisible(true);//鼠标监听器,正对这个窗口this.addMouseListener(new MyMouseListener());}//将points中点画出来@Overridepublic void paint(Graphics g) {//画画,监听鼠标事件Iterator iterator=points.iterator();//迭代器 (即循环)集合中的点while (iterator.hasNext()) { //取出List中的点Point point=(Point)iterator.next(); //指向下一个点g.setColor(Color.green);g.fillOval(point.x, point.y, 10, 10);}}//添加一个点到界面上面public void addPaint(Point point) {points.add(point);}//适配器模式private class MyMouseListener extends MouseAdapter {//鼠标按下,弹起,按住不放@Overridepublic void mousePressed(MouseEvent e) {MyFrame myFrame=(MyFrame)e.getSource();//点击的时候,界面上产生一个点myFrame.addPaint(new Point(e.getX(),e.getY()));//每次点击鼠标都需要重画一遍myFrame.repaint();}}}//画笔的点通过传入适配器,然后放到集合中,再用画笔画出
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-AivUIROg-1645521210909)(C:\Users\Lr\AppData\Roaming\Typora\typora-user-images\image-20220129203253429.png)]
2.9 窗口监听
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.blue);setBounds(100,100,200,200);setVisible(true);//addWindowListener(new MyWindowListener());this.addWindowListener(//this, 就是本身,然后本身加入这个方法,就是匿名内部类//匿名内部类,建议这么写new WindowAdapter() {@Overridepublic void windowOpened(WindowEvent e) {//super.windowOpened(e);System.out.println("windowOpened");}@Override//关闭窗口public void windowClosing(WindowEvent e) {//super.windowClosing(e);System.out.println("windowClosing");System.exit(0);}@Overridepublic void windowClosed(WindowEvent e) {//super.windowClosed(e);System.out.println("windowClosed");}@Override//激活窗口public void windowActivated(WindowEvent e) {//super.windowActivated(e);WindowFrame source = (WindowFrame) e.getSource();source.setTitle("被激活了");System.out.println("windowActivated");//激活}});}
/* 内部类class MyWindowListener extends WindowAdapter{@Overridepublic void windowClosing(WindowEvent e) {//super.windowClosing(e);//setVisible(false); //隐藏窗口,通过按钮,隐藏当前窗口System.exit(0);//正常退出}}*/}
2.10 键盘监听
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();//不需要去记这个数值,直接使用静态属性 VK_XXXSystem.out.println(keyCode);if (keyCode == KeyEvent.VK_UP){System.out.println("你按下了上键");}//根据按下不同操作,产生不同结果:}});}
}
3. Swing
3.1 窗口,面板
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kbOsAmgh-1645521210909)(C:\Users\Lr\AppData\Roaming\Typora\typora-user-images\image-20220211115254887.png)]
package com.li.lesson04;import javax.swing.*;
import java.awt.*;public class JFrameDemo {//int(); 初始化public void init(){JFrame jf = new JFrame("这是一个JFrame 窗口");jf.setVisible(true);jf.setBounds(100,100,200,200);jf.setBackground(Color.cyan);//设置文字 JlabelJLabel label = new JLabel("这里是一个小测试",SwingConstants.CENTER);jf.add(label);//关闭事件jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {//建立一个窗口new JFrameDemo().init();}
}
标签居中
package com.li.lesson04;import javax.swing.*;
import java.awt.*;public class JFrameDemo02 {public static void main(String[] args) {new MyJframe2().init();}
}//构造器
class MyJframe2 extends JFrame{public void init(){this.setVisible(true);this.setBounds(10,10,200,200);//设置文字JlabelJLabel label = new JLabel("这是一个测试",SwingConstants.CENTER);//让文本标签居中,设置水平对齐//label.setHorizontalAlignment(SwingConstants.CENTER);//关闭事件this.add(label);//获得一个容器,容器里的颜色才是真正的颜色Container container = this.getContentPane();container.setBackground(Color.pink);}
}
3.2 弹窗
JDialog,用来被弹出,默认就有关闭事件
package com.li.lesson04;import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;//主窗口
public class DialogDemo extends JFrame {public DialogDemo() {//构造器this.setVisible(true);//this.setSize(700,500);this.setBounds(100,100,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() { //监听器@Overridepublic void actionPerformed(ActionEvent e) {//弹窗new MyDialogDemo();//需要使用只需要new一个对象}});container.add(button);//容器里面加入按钮}public static void main(String[] args) {new DialogDemo();}}//弹窗的窗口
class MyDialogDemo extends JDialog{public MyDialogDemo() {this.setVisible(true);this.setBounds(100,100,500,500);//this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);// 里面自带了,所以这个是多余的Container container = this.getContentPane();container.setLayout(null);container.add(new Label("学习Java"));}
}
3.3 标签
label
new JLabel("xxx");
图标 ICON
package com.li.lesson04;import javax.swing.*;
import java.awt.*;//图标,需要实现类,Frame 继承
public class IconDemo extends JFrame implements Icon {private int width;private int height;public IconDemo(){}//无参构造public IconDemo(int width, int height){this.width = width;this.height = height;}public void init(){this.setBounds(100,100,700,500);IconDemo iconDemo = new IconDemo(15, 15);//图标放在标签上,也可以放在按钮上!JLabel label = new JLabel("icontest", iconDemo, SwingConstants.CENTER);Container container = getContentPane();container.add(label);this.setVisible(true);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new IconDemo().init();}public void paintIcon(Component c, Graphics g, int x, int y){g.fillOval(x,y,width,height);}public int getIconWidth(){return this.width;}public int getIconHeight(){return this.height;}
}
图片ICON
package com.li.lesson04;import javax.swing.*;
import java.awt.*;
import java.net.URL;public class ImageIconDemo extends JFrame {//先拥有一个构造器 再构造一个main方法public ImageIconDemo() {//获取图片的地址JLabel label = new JLabel("ImageIcon");//将图片放在标签里URL url = ImageIconDemo.class.getResource("壁纸01.jpg");ImageIcon imageIcon = new ImageIcon(url);label.setIcon(imageIcon);label.setHorizontalAlignment(SwingConstants.CENTER);//居中显示Container container = getContentPane();container.add(label);setVisible(true);setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);setBounds(100,100,200,200);}public static void main(String[] args) {new ImageIconDemo();}
}
3.4 面板
JPanel
package com.li.lesson05;import javax.swing.*;
import java.awt.*;public class JPanelDemo extends JFrame {public JPanelDemo(){Container container = this.getContentPane();container.setLayout(new GridLayout(2,1,10,10));//后面的参数的意思,间距JPanel panel1 = new JPanel(new GridLayout(1,3));JPanel panel2 = new JPanel(new GridLayout (1,2));panel1.add(new JButton("1"));panel1.add(new JButton("1"));panel1.add(new JButton("1"));panel2.add(new JButton("2"));panel2.add(new JButton("2"));container.add(panel1);container.add(panel2);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);setVisible(true);setBounds(100,100,500,500);}public static void main(String[] args) {new JPanelDemo();}
}
JScrollPanel
package com.li.lesson05;import javax.swing.*;
import java.awt.*;public class JScrollDemo extends JFrame {public JScrollDemo() {Container container = this.getContentPane();//文本域JTextArea textArea = new JTextArea(20, 50);textArea.setText("努力上进");//Scroll面板JScrollPane scrollPane = new JScrollPane(textArea);container.add(scrollPane);this.setVisible(true);this.setBounds(100,100,300,350);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new JScrollDemo();}
}
3.5 按钮
图片按钮
package com.li.lesson05;import javax.swing.*;
import java.awt.*;
import java.net.URL;public class JButtonDemo01 extends JFrame {public JButtonDemo01() {Container container = this.getContentPane();//将一个图片变为图标URL resource = JButtonDemo01.class.getResource("壁纸01.jpg");Icon icon = new ImageIcon(resource);//把这个图标放在按钮上JButton button = new JButton();button.setIcon(icon);button.setToolTipText("图片按钮");//addcontainer.add(button);this.setVisible(true);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);this.setBounds(100,100,500,300);}public static void main(String[] args) {new JButtonDemo01();}
}
-
单选按钮
package com.li.lesson05;import javax.swing.*; import java.awt.*; import java.net.URL;public class JButtonDemo02 extends JFrame {public JButtonDemo02(){Container container = this.getContentPane();//将一个图片变为图标URL resource = JButtonDemo01.class.getResource("壁纸01.jpg");Icon icon = new ImageIcon(resource);//单选框JRadioButton radioButton1 = new JRadioButton("JRadioButton01");JRadioButton radioButton2 = new JRadioButton("JRadioButton02");JRadioButton radioButton3 = new JRadioButton("JRadioButton03");//由于单选框只能选择一个,所以需要分组,一个组中只能选择一个ButtonGroup group = new ButtonGroup();group.add(radioButton1);group.add(radioButton2);group.add(radioButton3);//如果不加入这个,那就都可以选择了container.add(radioButton1,BorderLayout.CENTER);container.add(radioButton2,BorderLayout.NORTH);container.add(radioButton3,BorderLayout.SOUTH);this.setVisible(true);this.setBounds(100,100,500,300);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new JButtonDemo02();} }
-
多选按钮
package com.li.lesson05;import javax.swing.*; import java.awt.*; import java.net.URL;public class JButtonDemo03 extends JFrame {public JButtonDemo03(){Container container = this.getContentPane();//将一个图片变为图标URL resource = JButtonDemo01.class.getResource("壁纸01.jpg");Icon icon = new ImageIcon(resource);//多选框JCheckBox checkBox01 = new JCheckBox("checkBox01");JCheckBox checkBox02= new JCheckBox("checkBox02");container.add(checkBox01,BorderLayout.NORTH);container.add(checkBox02,BorderLayout.SOUTH);this.setVisible(true);this.setBounds(100,100,500,300);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new JButtonDemo03();} }
3.6 列表
-
下拉框
package com.li.lesson06;import javax.swing.*; import java.awt.*;public class TestComboboxDemo01 extends JFrame {public TestComboboxDemo01(){Container container = this.getContentPane();//下拉框JComboBox status = new JComboBox();status.addItem(null);status.addItem("正在上映");status.addItem("已下架");status.addItem("即将上映");container.add(status);this.setVisible(true);this.setBounds(100,100,500,350);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new TestComboboxDemo01();} }
-
列表框
package com.li.lesson06;import javax.swing.*;
import java.awt.*;
import java.util.Vector;public class TestComboboxDemo02 extends JFrame {public TestComboboxDemo02(){Container container = this.getContentPane();//生成列表的内容 稀疏数组,压缩数据//String[] contents = {"1","2","3"};//静态Vector<Object> contents = new Vector();//也可以放动态的变量//列表中需要放入内容JList jList = new JList(contents);contents.add("张三");contents.add("栗子");contents.add("杨桃");container.add(jList);this.setVisible(true);this.setSize(500,350);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new TestComboboxDemo02();}
}
- 应用的场景
- 选择地区,或者一些单个选项
- 列表,展示信息,一般是动态扩容
3.7 文本框
-
文本框
package com.li.lesson06;import javax.swing.*; import java.awt.*;public class TestTextDemo01 extends JFrame {public TestTextDemo01(){Container container = this.getContentPane();JTextField textField = new JTextField("hello");JTextField textField2 = new JTextField("world",20);container.add(textField,BorderLayout.NORTH);container.add(textField2,BorderLayout.SOUTH);this.setVisible(true);this.setSize(500,350);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new TestTextDemo01();}}
-
密码框
package com.li.lesson06; import javax.swing.*; import java.awt.*;public class TestTextDemo02 extends JFrame {public TestTextDemo02(){Container container = this.getContentPane();// 面板JPasswordField passwordField = new JPasswordField(); // ****passwordField.setEchoChar('*');container.add(passwordField);this.setVisible(true);this.setSize(500,350);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new TestTextDemo02();}}
-
文本域
package com.li.lesson06;import javax.swing.*;
import java.awt.*;public class TestTextDemo03 extends JFrame {public TestTextDemo03(){Container container = this.getContentPane();//文本域 配合面板使用JTextArea textArea = new JTextArea(20,50);textArea.setText("文本域的测试");//Scroll面板JScrollPane scrollPane = new JScrollPane(textArea);container.add(scrollPane);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);this.setVisible(true);this.setSize(400,350);}public static void main(String[] args) {new TestTextDemo03();}
}
-
C/S 客户端跟服务器
-
B/S 浏览器跟服务器
s) {
new TestTextDemo01();
}}
-
密码框
package com.li.lesson06; import javax.swing.*; import java.awt.*;public class TestTextDemo02 extends JFrame {public TestTextDemo02(){Container container = this.getContentPane();// 面板JPasswordField passwordField = new JPasswordField(); // ****passwordField.setEchoChar('*');container.add(passwordField);this.setVisible(true);this.setSize(500,350);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new TestTextDemo02();}}
-
文本域
package com.li.lesson06;import javax.swing.*;
import java.awt.*;public class TestTextDemo03 extends JFrame {public TestTextDemo03(){Container container = this.getContentPane();//文本域 配合面板使用JTextArea textArea = new JTextArea(20,50);textArea.setText("文本域的测试");//Scroll面板JScrollPane scrollPane = new JScrollPane(textArea);container.add(scrollPane);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);this.setVisible(true);this.setSize(400,350);}public static void main(String[] args) {new TestTextDemo03();}
}
- C/S 客户端跟服务器
- B/S 浏览器跟服务器