600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > GUI编程(图形用户界面编程)

GUI编程(图形用户界面编程)

时间:2022-07-26 03:18:34

相关推荐

GUI编程(图形用户界面编程)

文章目录

GUI编程(图形用户界面编程!)1. 简介2. AWT(Abstract Windows tools)2.1 AWT简介2.1 组件和容器1. 第一个Frame窗口2. 如何弹出多个窗口?3. 面板Panel 2.2. 布局管理器1. 流式布局2. 东西南北中3. 表格布局4. 小测试5. 小结 2.3. 事件监听2.4. 输入框 TextField2.5. 简易计算器2.6. 画笔2.7.鼠标监听,模拟画图工具2.8.窗口监听2.9.键盘监听 3. Swing3.1 窗口,面板3.2 弹窗3.3 标签3.4 面板3.5 按钮3.6 列表3.7 文本框 4.总结

GUI编程(图形用户界面编程!)

观看【狂神说Java】GUI编程入门到游戏实战视频学习笔记

图形用户界面(Graphical User Interface,简称 GUI,又称图形用户接口)是指采用图形方式显示的计算机操作用户界面。

命令来完成例行任务的字符界面相比,图形用户界面有许多优点。图形用户界面由窗口、下拉菜单、对话框及其相应的控制机制构成,在各种新式应用程序中都是标准化的,即相同的操作总是以同样的方式来完成,在图形用户界面,用户看到和操作的都是图形对象,应用的是计算机图形学的技术。

图形用户界面是一种人与计算机通讯执行其它一些日常任务。与通过键盘输入文本或字符命令来完成例行任务的字符界面相比,图形用户界面有许多优点。图形用户界面由窗口、下拉菜单、对话框及其相应的控制机制构成,在各种新式应用程序中都是标准化的,即相同的操作总是以同样的方式来完成,在图形用户界面,用户看到和操作的都是图形对象,应用的是计算机图形学的技术。

实现功能不能缺少组件,主要包括以下的一些组件:

窗口弹窗面板文本框列表框按钮图片监听事件鼠标键盘事件…

1. 简介

GUI编程的核心技术:Swing AWT

页面不美观

需要 jre环境 ---------->不流行的原因

学习的目的:

可以写出自己心中的一些小工具;工作可能会遇到维护Swing界面了解MVC架构还有了解监听

2. AWT(Abstract Windows tools)

2.1 AWT简介

包含了很多类和接口!

元素:窗口,按钮,文本框

java.awt 包

2.1 组件和容器

1. 第一个Frame窗口

代码:

package com.liang.lesson1;import java.awt.*;//学习java的第一个界面//CUI的第一个对象public class TestFrame {public static void main(String[] args) {//界面也是一个对象————>FrameFrame frame = new Frame("我的第一个java图形界面窗口");//需要设置可见性frame.setVisible(true);//设置窗口大小frame.setSize(400,400);//设置窗口背景颜色frame.setBackground(new Color(205, 45, 114));//弹出的最初位置frame.setLocation(200,200);//设置大小不变frame.setResizable(false);}}

2. 如何弹出多个窗口?

将代码封装!

package com.liang.lesson1;import java.awt.*;public class TestFrame2 {public static void main(String[] args) {//展示多个窗口MyFrame myFrame1 = new MyFrame(100,100,300,300,Color.blue);MyFrame myFrame2 = new MyFrame(400,100,300,300,Color.red);MyFrame myFrame3 = new MyFrame(100,400,300,300,Color.green);MyFrame myFrame4 = new MyFrame(400,400,300,300,Color.yellow);}}class MyFrame extends Frame{static int id = 0;//可能存在多个窗口,我们需要有个计数器public MyFrame(int x,int y,int w, int h , Color color){super("MyFrame" + (++id));setBounds(x,y,w,h);//弹出的出位置和弹窗的大小setBackground(color);setVisible(true);}}

3. 面板Panel

解决了窗口关闭问题:------>监听事件

package com.liang.lesson1;import java.awt.*;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;//测试面板Panelpublic class TestPenal {public static void main(String[] args) {Frame frame = new Frame("我喜欢你!");//布局的概念Panel panel = new Panel();//设置布局frame.setLayout(null);//设置弹窗的位置和大小(设在中间)frame.setBounds(500,100,500,500);//设置窗口颜色frame.setBackground(new Color(35, 195, 182));//设置面板大小,相对于framepanel.setBounds(100,100,300,300);//设置面板颜色panel.setBackground(new Color(17, 83, 183));//添加面板frame.add(panel);//设置窗口可见frame.setVisible(true);//监听事件,监听窗口关闭事件//适配器模式frame.addWindowListener(new WindowAdapter() {//点击关闭需要做的事情@Overridepublic void windowClosing(WindowEvent e) {//关闭窗口System.exit(0);}});}}

2.2. 布局管理器

1. 流式布局

package com.liang.lesson1;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.setBounds(500,100,500,500);//设置窗口颜色frame.setBackground(new Color(35, 195, 182));//添加按钮frame.add(button1);frame.add(button2);frame.add(button3);//监听frame.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);}});frame.setVisible(true);}}

2. 东西南北中

package com.liang.lesson1;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("东西南北中");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.setBounds(500,100,500,500);//设置窗口颜色frame.setBackground(new Color(35, 80, 195));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.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);}});}}

3. 表格布局

package com.liang.lesson1;import java.awt.*;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;public class TestGridLayout {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.setBounds(500,100,500,500);//设置表格--->三行两列frame.setLayout(new GridLayout(2,3));frame.add(btn1);frame.add(btn2);frame.add(btn3);frame.add(btn4);frame.add(btn5);frame.add(btn6);frame.setVisible(true);//监听frame.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);}});}}

4. 小测试

​ 用程序设计出以下的菜单模块:

方法:

package com.liang.lesson1;import javax.swing.border.Border;import java.awt.*;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;public class Demon {public static void main(String[] args) {//弹出最大的窗口Frame frame = new Frame("小测试!");//设置弹窗的位置和大小(设在中间)frame.setBounds(500,100,600,600);//设置颜色frame.setBackground(new Color(19, 233, 225));//设置可见frame.setVisible(true);//表格布局两行一列frame.setLayout(new GridLayout(2,1));//设置4两个面板Panel panel1 = new Panel(new BorderLayout());Panel panel2 = new Panel(new BorderLayout());Panel panel3 = new Panel(new GridLayout(2,1));Panel panel4 = new Panel(new GridLayout(2,2));//填充按钮//上左边的按键panel1.add(new Button("button1"),BorderLayout.WEST);//上右边的按键panel1.add(new Button("button2"),BorderLayout.EAST);//中间的按钮panel3.add(new Button("button5"));panel3.add(new Button("button6"));//上中间的面板,把面板3装到1上panel1.add(panel3,BorderLayout.CENTER);//下左边的按键panel2.add(new Button("button3"),BorderLayout.WEST);//下右边的按键panel2.add(new Button("button4"),BorderLayout.EAST);//下中间的面板的按键// panel4.add(new Button("button7"));// panel4.add(new Button("button8"));// panel4.add(new Button("button9"));// panel4.add(new Button("button10"));//上面四行为了方便可以通过循环来添加for (int i = 7;i <= 10;i ++){panel4.add(new Button("button" + i));}//下中间的面板,把面板4装到2上panel2.add(panel4,BorderLayout.CENTER);//把面板1和2装到窗口上frame.add(panel1);frame.add(panel2);//监听frame.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);}});}}

5. 小结

Frame是一个顶级窗口

Panel不能单独显示,需要有一个容器来承载才能显示出来

布局管理器

流式东西南北中表格 大小,位置,颜色,可见,监听!

2.3. 事件监听

当某个事情发生时候,应该干什么?

package com.liang.lesson2;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 TestAction {public static void main(String[] args) {//按下按钮,触发一些事件Frame frame = new Frame("事件监听!");Button button = new Button("I am a button !");frame.setBounds(500,100,300,300);frame.setBackground(new Color(205, 239, 56));button.setBounds(600,50,100,100);button.setBackground(new Color(0x12C894));frame.setVisible(true);//以便更改参数MyListener mylistener = new MyListener();button.addActionListener(mylistener);frame.add(button,BorderLayout.CENTER);frame.pack();windowClose(frame);}//将关闭窗口抽象为静态方法public static void windowClose(Frame frame){frame.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);}});}}class MyListener implements ActionListener{@Overridepublic void actionPerformed(ActionEvent e) {System.out.println("干饭人干饭魂,干饭人也想睡觉!");}}

还有多个按钮共享一个监听事件!(省略)

2.4. 输入框 TextField

ckage com.liang.lesson2;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 TestInput {public static void main(String[] args) {//只管启动MyFrame myframe = new MyFrame();windowClose(myframe);}/***将关闭窗口抽象为静态方法*/public static void windowClose(Frame frame){frame.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);}});}}class MyFrame extends Frame {public MyFrame() {setBounds(300,100,500,500);setBackground(new Color(52, 203, 185));TextField textfield = new TextField();//添加文本框add(textfield,BorderLayout.SOUTH);//文本框监听MyListener1 myListener1 = new MyListener1();textfield.addActionListener(myListener1);//隐藏字符编码textfield.setEchoChar('*');setVisible(true);}}class MyListener1 implements ActionListener {@Overridepublic void actionPerformed(ActionEvent e) {//获取一些资源TextField textField = (TextField) e.getSource();System.out.println(textField.getText());textField.setText("");//回车后清空}}

2.5. 简易计算器

面向过程写法:

package com.liang.lesson2;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;/*** @author 86178* 简易计算器*/public class TestCal {public static void main(String[] args) {Calc calc = new Calc();windowClose(calc);}/***将关闭窗口抽象为静态方法*/public static void windowClose(Frame frame){frame.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);}});}}//计算器类class Calc extends Frame{public Calc(){//1. 三个文本框TextField file = new TextField(10);//字符数TextField file1 = new TextField(10);TextField file2 = new TextField(20);//2.一个按键Button button = new Button("=");button.addActionListener(new Listener(file,file1,file2));//3.一个标签Label label = new Label("+");//布局setBounds(500,100,300,300);setLayout(new FlowLayout());add(file);add(label);add(file1);add(button);add(file2);pack();setVisible(true);}}//监听器类class Listener implements ActionListener{//获取三个变量private TextField file,file1,file2;//来个构造器public Listener(TextField file,TextField file1,TextField file2){this.file = file;this.file1 = file1;this.file2 = file2;}@Overridepublic void actionPerformed(ActionEvent e) {//1. 获取加数和被加数int i = Integer.parseInt(file.getText());int i1 = Integer.parseInt(file1.getText());//2. 给button监听file2.setText("" + (i + i1));file.setText("");file1.setText("");}}

优化:

package com.liang.lesson2;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;/*** @author 86178* 简易计算器*/public class TestCal {public static void main(String[] args) {Calculator calc = new Calculator();calc.LoadFrame();windowClose(calc);}/***将关闭窗口抽象为静态方法*/public static void windowClose(Frame frame){frame.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);}});}}//计算器类class Calculator extends Frame{//属性TextField file1,file2,file3;//方法public void LoadFrame(){file1 = new TextField(10);file2 = new TextField(10);file3 = new TextField(20);Button button = new Button("=");Label label = new Label("+");button.addActionListener(new Listener(this));//布局setBounds(500,100,300,300);setLayout(new FlowLayout());add(file1);add(label);add(file2);add(button);add(file3);pack();setVisible(true);}}//监听器类class Listener implements ActionListener {//获取对象Calculator calculator = null;//来个构造器public Listener(Calculator calculator) {this.calculator = calculator;}@Overridepublic void actionPerformed(ActionEvent e) {//1. 获取加数和被加数int i1 = Integer.parseInt(calculator.file1.getText());int i2 = Integer.parseInt(calculator.file2.getText());//2.相加calculator.file3.setText("" + (i1 + i2));//3.清除calculator.file1.setText("");calculator.file2.setText("");}}

内部类:更好地包装

package com.liang.lesson2;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;/*** @author 86178* 简易计算器*/public class TestCal {public static void main(String[] args) {Calculator calc = new Calculator();calc.LoadFrame();windowClose(calc);}/***将关闭窗口抽象为静态方法*/public static void windowClose(Frame frame){frame.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);}});}}//计算器类class Calculator extends Frame{//属性TextField file1,file2,file3;//方法public void LoadFrame(){file1 = new TextField(10);file2 = new TextField(10);file3 = new TextField(20);Button button = new Button("=");Label label = new Label("+");button.addActionListener(new Listener());//布局setBounds(500,100,300,300);setLayout(new FlowLayout());add(file1);add(label);add(file2);add(button);add(file3);pack();setVisible(true);}//监听器类private class Listener implements ActionListener {@Overridepublic void actionPerformed(ActionEvent e) {//1. 获取加数和被加数int i1 = Integer.parseInt(file1.getText());int i2 = Integer.parseInt(file2.getText());file1.setText("");file2.setText("");file3.setText("" + (i1 + i2));}}}

2.6. 画笔

package com.liang.lesson3;import java.awt.*;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;public class TestPaint {public static void main(String[] args) {MyPaint paint = new MyPaint();paint.loadFrame();MyPaint.windowClose(paint);}}class MyPaint extends Frame{public void loadFrame(){setBounds(500,100,500,500);setVisible(true);}//画笔@Overridepublic void paint(Graphics g) {g.setColor(Color.green);g.drawOval(100,100,200,200);g.setColor(Color.red);g.fillOval(100,100,100,100);g.setColor(Color.yellow);g.fillRoundRect(100,200,100,100,50,50);}/***将关闭窗口抽象为静态方法*/public static void windowClose(Frame frame){frame.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);}});}}

2.7.鼠标监听,模拟画图工具

package com.liang.lesson3;import java.awt.*;import java.awt.event.*;import java.util.ArrayList;import java.util.Iterator;/*** 测试鼠标监听*/public class TestMouse {public static void main(String[] args) {MyMouse myMouse = new MyMouse("鼠标监听!");//关闭窗口MyMouse.windowClose(myMouse);}}//创建一个画板class MyMouse extends Frame{//画画需要画笔,需要监听鼠标,需要有一个集合来存放这个点ArrayList points;public MyMouse(String title){super(title);setBounds(500,100,500,500);points = new ArrayList<>();setVisible(true);//鼠标监听this.addMouseListener(new MyMouseListener());}//把点画出来的方法@Overridepublic void paint(Graphics g) {//画画,监听鼠标事件Iterator iterator = points.iterator();while(iterator.hasNext()){Point point = (Point)iterator.next();g.setColor(Color.blue);g.fillOval(point.x,point.y,10,10);}}//把点添加到界面上public void addPaint(Point point){points.add(point);}//适配器模式private class MyMouseListener extends MouseAdapter{@Overridepublic void mouseClicked(MouseEvent e) {//获取一个点//这个就是点击的时候会在界面产生一个点。MyMouse myMouse = (MyMouse) e.getSource();myMouse.addPaint( new Point(e.getX(),e.getY()));myMouse.repaint();//刷新一下}}/***将关闭窗口抽象为静态方法*/public static void windowClose(Frame frame){frame.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);}});}}

2.8.窗口监听

package com.liang.lesson3;import java.awt.*;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;public class TestWindows {public static void main(String[] args) {new Windows();}}class Windows extends Frame{public Windows(){setBounds(500,100,500,500);setBackground(Color.blue);setVisible(true);this.addWindowListener(new WindowAdapter() {@Overridepublic void windowActivated(WindowEvent e) {Windows win = (Windows) e.getSource();win.setTitle("真有你的!");System.out.println("我回来了");}@Overridepublic void windowClosing(WindowEvent e) {System.out.println("我要关闭了");System.exit(0);}});}}

2.9.键盘监听

package com.liang.lesson3;import java.awt.*;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;public class TestKid {public static void main(String[] args) {new MyKid();}}class MyKid extends Frame{public MyKid(){setBounds(500,100,300,300);setBackground(Color.green);setVisible(true);this.addKeyListener(new KeyAdapter() {@Overridepublic void keyPressed(KeyEvent e) {//获得当前的键是哪一个int keycode = e.getKeyCode();//不需要记录数值,直接使用静态方法VK_XXSystem.out.println(keycode);if(keycode == KeyEvent.VK_0 ){System.out.println("靓仔你按下的是0");}}});this.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);}});}}

3. Swing

3.1 窗口,面板

package com.liang.lesson4;import javax.swing.*;import java.awt.*;/*** @author 86178*/public class JframeDemon {public static void main(String[] args) {new MyJframe().init();}}class MyJframe extends JFrame{public void init(){super.setTitle("我的第一个Swing窗口");this.setBounds(500,100,500,500);this.setVisible(true);JLabel label = new JLabel("欢迎来到我的世界!");this.add(label);//让文本居中label.setHorizontalAlignment(SwingConstants.CENTER);//获得一个容器Container container = this.getContentPane();container.setBackground(Color.yellow);//关闭事件this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}}

3.2 弹窗

kage com.liang.lesson4;import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;/*** 主弹窗* @author 86178*/public class DialogDemon extends JFrame {public DialogDemon(){super.setTitle("应该有弹窗!");this.setBounds(500,100,500,500);this.setVisible(true);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//东西放在容器里Container container = this.getContentPane();//绝对布局container.setLayout(null);container.setBackground(Color.CYAN);//按钮JButton Button = new JButton("点我有弹窗!");Button.setBounds(200,150,200,50);container.add(Button);//点击按钮的时候弹出一个弹窗Button.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {//弹窗new MyDialog();}});}public static void main(String[] args) {new DialogDemon();}}class MyDialog extends JDialog{public MyDialog() {super.setTitle("我是弹窗!");this.setVisible(true);//弹窗大小this.setBounds(500,300,200,200);Container container = this.getContentPane();Container container1 = new Container();container1.setLayout(null);container.setBackground(Color.magenta);JLabel lab = new JLabel("公告:我只是想发一条公告!");this.add(lab);}}

3.3 标签

label

package com.liang.lesson4;import javax.swing.*;import java.awt.*;/*** 图标需要实现类* @author 86178*/public class IconDemon extends JFrame implements Icon {private int width;private int height;public IconDemon(){}public IconDemon(int width,int height){this.width = width;this.height = height;}public void init(){IconDemon iconDemon = new IconDemon(15,15);this.setTitle("我是一个图标");//图标可以放在标签上,也可以放在按钮上JLabel label = new JLabel("incontest", iconDemon, SwingConstants.CENTER);Container contentPane = getContentPane();contentPane.setBackground(Color.CYAN);contentPane.add(label);this.setBounds(500,100,500,500);this.setVisible(true);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new IconDemon().init();}@Overridepublic void paintIcon(Component c, Graphics g, int x, int y) {g.fillOval(x,y,width,height);}@Overridepublic int getIconWidth() {return this.width;}@Overridepublic int getIconHeight() {return this.height;}}

图像:ICON

package com.liang.lesson4;import javax.swing.*;import java.awt.*;import .URL;/*** @author 86178*/public class ImageIconDemon extends JFrame {public ImageIconDemon(){super.setTitle("找我玩!");//获取图片的地址JLabel label = new JLabel("imageIcon");URL url = ImageIconDemon.class.getResource("找我玩.jpg");ImageIcon imageIcon = new ImageIcon(url);label.setIcon(imageIcon);label.setHorizontalAlignment(SwingConstants.CENTER);Container contentPane = getContentPane();contentPane.add(label);this.setVisible(true);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);this.setBounds(500,100,600,600);}public static void main(String[] args) {new ImageIconDemon();}}

3.4 面板

package com.liang.lesson5;import javax.swing.*;import java.awt.*;/*** @author 86178*/public class JpanelDemon extends JFrame {public JpanelDemon(){Container container = getContentPane();container.setLayout(new GridLayout(2,1,10,10));JPanel panel = new JPanel(new GridLayout(1,1,5,5));panel.add(new Button("one"));panel.add(new Button("two"));panel.add(new Button("three"));container.add(panel);this.setBounds(500,100,500,500);this.setVisible(true);setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new JpanelDemon();}}

下拉框

package com.liang.lesson5;import javax.swing.*;import java.awt.*;/*** @author 86178*/public class JScrollPanelDemon extends JFrame {public JScrollPanelDemon(){Container container = this.getContentPane();//文本域JTextArea jTextArea = new JTextArea(20,50);jTextArea.setText("欢迎和我一起学习!");//scorll面板// JScrollPane jscroll = new JScrollPane(jTextArea);// container.add(jscroll);container.add(new JScrollPane(jTextArea));setBounds(500,100,500,500);this.setVisible(true);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new JScrollPanelDemon();}}

3.5 按钮

图片按钮:

package com.liang.lesson5;import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import .URL;/*** @author 86178*/public class JbuttonDemon extends JFrame {public JbuttonDemon(){Container container = this.getContentPane();//把一个图像变成图标URL url = JbuttonDemon.class.getResource("a1.jpg");ImageIcon icon = new ImageIcon(url);//把图标放到按钮上JButton button = new JButton();button.setIcon(icon);button.setToolTipText("图片按钮");button.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {System.out.println("张小姐好棒!");}});container.add(button);this.setVisible(true);this.setBounds(500,100,500,500);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new JbuttonDemon();}}

单选框:

package com.liang.lesson5;import javax.swing.*;import java.awt.*;/*** @author 86178*/public class JbuttonDemon1 extends JFrame {public JbuttonDemon1() {super.setTitle("单选框");Container contentPane = this.getContentPane();JRadioButton jRadioButton1 = new JRadioButton("i");JRadioButton jRadioButton2 = new JRadioButton("love");JRadioButton jRadioButton3 = new JRadioButton("you");//把按键放在一组ButtonGroup group = new ButtonGroup();group.add(jRadioButton1);group.add(jRadioButton2);group.add(jRadioButton3);contentPane.add(jRadioButton1,BorderLayout.NORTH);contentPane.add(jRadioButton2,BorderLayout.CENTER);contentPane.add(jRadioButton3,BorderLayout.SOUTH);this.setVisible(true);this.setBounds(500,100,500,500);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new JbuttonDemon1();}}

多选框:

package com.liang.lesson5;import javax.swing.*;import java.awt.*;/*** @author 86178*/public class JbuttonDemon2 extends JFrame {public JbuttonDemon2() {this.setTitle("多选框");Container contentPane = this.getContentPane();//多选框Checkbox checkbox1 = new Checkbox("Stay");Checkbox checkbox2 = new Checkbox("with");Checkbox checkbox3 = new Checkbox("me");contentPane.setLayout(new FlowLayout(FlowLayout.LEFT));contentPane.setLayout(new FlowLayout(FlowLayout.CENTER));contentPane.setLayout(new FlowLayout(FlowLayout.RIGHT));contentPane.add(checkbox1);contentPane.add(checkbox2);contentPane.add(checkbox3);pack();this.setVisible(true);this.setBounds(500,100,500,500);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new JbuttonDemon2();}}

3.6 列表

下拉框

package com.liang.lesson5;import javafx.boBox;import javax.swing.*;import java.awt.*;/*** @author 86178* 测试下拉框*/public class TestComboxDemon1 extends JFrame {public TestComboxDemon1(){this.setTitle("我是下拉框");Container container = this.getContentPane();JComboBox status = new JComboBox();status.addItem("广工");status.addItem("中大");status.addItem("华工");status.addItem("湛幼");JPanel panel = new JPanel();panel.add(status,BorderLayout.NORTH);container.add(panel);this.setVisible(true);this.setBounds(500,100,500,500);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new TestComboxDemon1();}}

列表框

package com.liang.lesson5;import javax.swing.*;import java.awt.*;import java.util.Vector;/*** @author 86178* 测试列表框*/public class TestComboxDemon2 extends JFrame {public TestComboxDemon2(){this.setTitle("我是列表框");Container container = this.getContentPane();//生成列表的内容// String[] contents = {"我","知","道","你"};Vector contents = new Vector();contents.add("1.我喜欢你!");contents.add("2.你喜欢我!");contents.add("3.我们互相喜欢!");contents.add("4.我们互相不喜欢!");//把内容放进列表JList list = new JList(contents);JPanel panel = new JPanel();panel.add(list,BorderLayout.CENTER);container.add(panel);this.setVisible(true);this.setBounds(500,100,500,500);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new TestComboxDemon2();}}

应用场景 选择地区或者一些单个选项列表展示信息,一般是动态扩容!

3.7 文本框

文本框

package com.liang.lesson5;import javax.swing.*;import java.awt.*;import java.util.Vector;/*** @author 86178* 测试文本框*/public class TestTextDemon extends JFrame {public TestTextDemon(){this.setTitle("我是文本框");Container container = this.getContentPane();JTextField textField1 = new JTextField("我是初始值1",20);JTextField textField2 = new JTextField("我是初始值2",20);JPanel panel1 = new JPanel();JPanel panel2 = new JPanel();panel1.add(textField1);panel2.add(textField2);container.add(panel1, BorderLayout.NORTH);container.add(panel2,BorderLayout.SOUTH);this.setVisible(true);this.setBounds(500,100,500,500);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new TestTextDemon();}}

密码框

package com.liang.lesson5;import javax.swing.*;import java.awt.*;/*** @author 86178* 测试密码框*/public class TestJpassword extends JFrame {public TestJpassword(){this.setTitle("我是密码框");Container container = this.getContentPane();//密码框JPasswordField password = new JPasswordField(20);password.setEchoChar('*');JPanel panel1 = new JPanel();panel1.add(password);container.add(panel1, BorderLayout.CENTER);this.setVisible(true);this.setBounds(500,100,500,500);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new TestJpassword();}}

文本域

package com.liang.lesson5;import javax.swing.*;import java.awt.*;/*** @author 86178* 测试文本域名*/public class TestTextAreaDemon extends JFrame {public TestTextAreaDemon(){this.setTitle("我是文本域");Container container = this.getContentPane();//文本域//要设定初始大小才会显示,不然不会显示JTextArea textArea = new JTextArea(20,50);textArea.setText("我是初始值");JScrollPane panel = new JScrollPane(textArea);container.add(panel);this.setVisible(true);this.setBounds(500,100,500,500);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new TestTextAreaDemon();}}

4.总结

要开始考核了,祝福我吧!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。