GUI编程
程序员文章站
2022-03-08 10:30:03
...
GUI编程
告诉大家该怎么学?
- 这是什么?
- 它怎么玩?
- 该如何在我们平时运用?
- 反编译:class–>可阅读文件
组件
- 窗口
- 弹窗
- 面板
- 文本框
- 列表框
- 按钮
- 图片
- 监听事件
- 鼠标
- 键盘事件
- 外挂:Java
- **工具:Java
1.简介
GUI的核心技术:Swing、AWT
淘汰原因:
- 界面不美观
- 需要jre环境
为什么我们要学习?
- 可以写出自己心中想要的一些小工具
- 工作的时候,也可能现需要维护到Swing界面,概率极小!
- 了解MVC架构,了解监听!
2.AWT底层实现
2.1Awt介绍
Eclipse:Java
1.包含了很多类和接口!GUI:图形用户编程!
2.元素:窗口,按钮,文本框
3.Java.awt
组件:
button、TextArea、Label、...(存在容器中add)
容器Container
面板Panel:
Applet
Window:
Frame
Dialog
2.2组件和容器
窗口Frame
package com.sjy.lesson01;
import java.awt.*;
//GUI的第一个界面
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(Color.black);
frame.setBackground(new Color(85,150,68));
//弹出的初始位置
frame.setLocation(200,200);
//设置大小固定
frame.setResizable(false);
}
}
问题:发现窗口关闭不掉,停止Java程序!
2.2.1Frame封装:
package com.sjy.lesson01;
import java.awt.*;
public class TestFrame2 {
public static void main(String[] args) {
//展示多个窗口 new
MyFrame myFrame1 = new MyFrame(100, 100, 200, 200, Color.blue);
MyFrame myFrame2 = new MyFrame(300, 100, 200, 200, Color.yellow);
MyFrame myFrame3 = new MyFrame(100, 300, 200, 200, Color.red);
MyFrame myFrame4 = new MyFrame(300, 300, 200, 200, Color.magenta);
}
}
class MyFrame extends Frame {
static int id = 0;//可能存在多个窗口,我们需要一个计数器
//x、y:初始化位置 w h:宽高
public MyFrame(int x,int y,int w,int h,Color color) throws HeadlessException {
super("MyFrame"+(++id));
setBounds(x,y,w,h);
setBackground(color);
setVisible(true);
}
面板Panel
解决了关闭事件!
package com.sjy.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 = 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设置颜色
panel.setBackground(new Color(193,15,60));
//frame.add(panel)
frame.add(panel);
frame.setVisible(true);
//监听事件,监听窗口关闭事件 System.exit(0)
//适配器模式
frame.addWindowListener(new WindowAdapter() {
//窗口点击关闭的时候需要做的事情
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
//结束程序
System.exit(0);
}
});
}
}
2.3布局管理器
- 流式布局(默认)Flow
package com.sjy.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);
}
}
- 东西南北中Border
package com.sjy.lesson01;
import java.awt.*;
public class TsetBorderLayout {
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);
}
}
- 表格布局Grid
package com.sjy.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.sjy.lesson01;
import java.awt.*;
//练习的demo
public class ExerciseDemo {
public static void main(String[] args) {
//总 Frame
Frame frame = new Frame();
frame.setLayout(new GridLayout(2,1));
frame.setSize(400,300);
frame.setLocation(300,400);
frame.setBackground(Color.BLACK);
frame.setVisible(true);
//四个面板
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("West-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("West-2"),BorderLayout.WEST);
panel4.add(new Button("p2-btn-3"));
panel4.add(new Button("p2-btn-4"));
panel4.add(new Button("p2-btn-5"));
panel4.add(new Button("p2-btn-6"));
panel3.add(panel4,BorderLayout.CENTER);
frame.add(panel1);
frame.add(panel3);
}
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
});
}
总结:
- Frame是一个*窗口
- Panel无法单独显示,必须添加到某个容器中
- 布局管理器
- 流式
- 东西南北中
- 表格
- 大小,定位,背景颜色,可见性,监听!
2.4事件监听
事件监听:当某个事情发送的时候,干什么?
package com.sjy.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 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();
windowClose(frame);//关闭窗口
frame.setVisible(true);
}
//关闭窗体的事件
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
});
}
}
class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("aaa");
}
}
多个按钮,共享一个事件:
package com.sjy.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 TestActionEvent2 {
public static void main(String[] args) {
//两个按钮,实现同一个监听
//开始 停止
Frame frame = new Frame("开始-停止");
Button button1 = new Button("start");
Button button2 = new Button("stop");
//可以显示的定义会触发返回的命令
//可以多个按钮致谢一个监听类
button1.setActionCommand("button1-start");
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);
windowClose(frame);
}
//关闭窗体的事件
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
});
}
}
class MyMonitor implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("按钮被点击了:msg" + e.getActionCommand());
}
}
2.5输入框TextField监听
package com.sjy.lesson2;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestText1 {
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{
@Override
public void actionPerformed(ActionEvent e) {
TextField field = (TextField) e.getSource();//获得一些资源,返回的一个对象
System.out.println(field.getText());//获得输入框中的文本
field.setText(""); //null是字符串 ""是空
}
}
2.6简易计算器,组合+内部类回顾复习!
oop原则:组合,大于继承!
class A extends B{
}
class A{
public B b;
}
简易计算器:
package com.sjy.lesson2;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//简易计算器
public class TsetCaculate {
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 MyCalculateListener(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 MyCalculateListener implements ActionListener{
//获取三个变量
private TextField num1,num2,num3;
public MyCalculateListener(TextField num1,TextField num2,TextField num3){
this.num1 = num1;
this.num2 = num2;
this.num3 = num3;
}
@Override
public void actionPerformed(ActionEvent e) {
//1.获得加数和被加数
int i1 = Integer.parseInt(num1.getText());
int i2 = Integer.parseInt(num2.getText());
//2.将这个值+预算后,放到第三个框
num3.setText(""+(i1+i2));
//3.清除前两个框
num1.setText("");
num2.setText("");
}
}
完全改造为面向对象写法:
package com.sjy.lesson2;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//简易计算器
public class TsetCaculate {
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 MyCalculateListener(this));
//1个标签
Label label = new Label("+");
//布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
}
//监听器类
class MyCalculateListener implements ActionListener{
//获取计算器这个对象,在一个类中组合另外一个类
private Calculator calculator = null;
public MyCalculateListener(Calculator calculator){
this.calculator = calculator;
}
@Override
public void actionPerformed(ActionEvent e) {
//1.获得加数和被加数
int i1 = Integer.parseInt(calculator.num1.getText());
int i2 = Integer.parseInt(calculator.num2.getText());
//2.将这个值+预算后,放到第三个框
calculator.num3.setText(""+(i1+i2));
//3.清除前两个框
calculator.num1.setText("");
calculator.num2.setText("");
}
}
内部类:
- 更好的包装
- 最大的好处,就是可以畅通无阻的访问外部的属性和方法!
package com.sjy.lesson2;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//简易计算器
public class TsetCaculate {
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 MyCalculateListener());
//1个标签
Label label = new Label("+");
//布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
//监听器类
private class MyCalculateListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//1.获得加数和被加数
int i1 = Integer.parseInt(num1.getText());
int i2 = Integer.parseInt(num2.getText());
//2.将这个值+预算后,放到第三个框
num3.setText(""+(i1+i2));
//3.清除前两个框
num1.setText("");
num2.setText("");
}
}
}
2.7画笔paint
package com.sjy.lesson3;
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) {
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(200,200,200,200);//矩形
//养成习惯,画笔用完,将他还原到最初的颜色
}
}
2.8鼠标监听
目的:想要实现鼠标画画!
package com.sjy.lesson3;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
//鼠标监听事件
public class TestMouseListener {
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<>();
//鼠标监听器,针对这个窗口的
this.addMouseListener(new MyMouseListener());
setVisible(true);
}
@Override
public void paint(Graphics g) {
super.paint(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{
//鼠标:按下、弹起、按住不放
@Override
public void mousePressed(MouseEvent e) {
super.mousePressed(e);
MyFrame myFrame = (MyFrame) e.getSource();
//这个我们点击的时候,就会在界面上产生一个点!画
//这个点就是鼠标的点
myFrame.addPaint(new Point(e.getX(),e.getY()));
//每次点击鼠标都需要重新画一遍
myFrame.repaint();//刷新
}
}
}
2.9窗口监听
package com.sjy.lesson3;
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());
}
class MyWindowListener extends WindowAdapter{
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
setVisible(false);//通过按钮,隐藏窗口
System.exit(0);//正常退出
}
}
}
2.10键盘监听
package com.sjy.lesson3;
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(100,200,300,400);
setVisible(true);
addKeyListener(new KeyAdapter() {
//键盘按下
@Override
public void keyPressed(KeyEvent e) {
super.keyPressed(e);
//获取键盘按下的键盘死哪一个,当前的码
int keyCode = e.getKeyCode();//不需要去记录这个数值,直接使用静态属性VK_XXX
System.out.println(keyCode);
if(keyCode == KeyEvent.VK_UP){
System.out.println("你按下了上键");
}
//根据按下不同操作,产生不同的结果
}
});
}
}
3.Swing界面
3.1窗口、面板JFrame
package com.sjy.lesson4;
import javax.swing.*;
import java.awt.*;
public class JFrameDemo {
//init();初始化
public void init(){
//*窗口
JFrame jFrame = new JFrame("这是一个JFrame窗口");
jFrame.setVisible(true);
jFrame.setBounds(100,100,200,200);
jFrame.setBackground(Color.cyan);
//设置文字JLabel
JLabel jLabel = new JLabel("欢迎来到GUI编程");
jFrame.add(jLabel);
//容器实例化
Container contentPane = jFrame.getContentPane();
contentPane.setBackground(Color.cyan);
//让文本标签居中
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
//关闭事件
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
//建立一个窗口
new JFrameDemo().init();
}
}
3.2弹窗JDialog
用来被弹出,默认就有关闭事件
package com.sjy.lesson4;
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.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//JFrame 放东西,容器
Container container = this.getContentPane();
//绝对布局
container.setLayout(null);
//按钮
JButton jButton = new JButton("点击弹出一个对话框");//创建
jButton.setBounds(30,30,200,50);
//点击这个按钮的时候,弹出一个弹窗
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//弹窗
new MyDialogDemo();
}
});
container.add(jButton);
}
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 contentPane = this.getContentPane();
contentPane.setLayout(null);
}
}
3.3标签
label
new JLable("xxx");
图标ICON:
package com.sjy.lesson4;
import javax.swing.*;
import java.awt.*;
//图标,需要实现类,JFrame继承
public class IconDemo extends JFrame implements Icon {
private int width;
private int height;
public IconDemo() throws HeadlessException {//无参构造
}
public IconDemo(int width, int height) throws HeadlessException {
this.width = width;
this.height = height;
}
public void init(){
IconDemo iconDemo = new IconDemo(15, 15);
//图标放在标签,也可以放在按钮上!
JLabel jLabel = new JLabel("iconTest", iconDemo, SwingConstants.CENTER);
Container contentPane = getContentPane();
contentPane.add(jLabel);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new IconDemo().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;
}
@Override
public int getIconHeight() {
return this.height;
}
}
图片ICON:
package com.sjy.lesson4;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class ImageIconDemo extends JFrame {
public ImageIconDemo(){
//获取图片的地址
JLabel jLabel = new JLabel("ImageIcon");
URL url = ImageIconDemo.class.getResource("1.bmp");
ImageIcon imageIcon = new ImageIcon(url);//命名不要冲突
jLabel.setIcon(imageIcon);
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
Container contentPane = getContentPane();
contentPane.add(jLabel);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setBounds(100,100,1000,1000);
}
public static void main(String[] args) {
new ImageIconDemo();
}
}
3.4面板
package com.sjy.lesson5;
import javax.swing.*;
import java.awt.*;
public class JPanelDemo extends JFrame {
public JPanelDemo(){
Container contentPane = this.getContentPane();
contentPane.setLayout(new GridLayout(2,1,10,10));//后面的参数的意思是间距
JPanel jPanel1 = new JPanel(new GridLayout(1,3));
JPanel jPanel2 = new JPanel(new GridLayout(1,2));
JPanel jPanel3 = new JPanel(new GridLayout(2,1));
JPanel jPanel4 = new JPanel(new GridLayout(3,2));
jPanel1.add(new JButton("1"));
jPanel1.add(new JButton("1"));
jPanel1.add(new JButton("1"));
jPanel2.add(new JButton("2"));
jPanel2.add(new JButton("2"));
jPanel3.add(new JButton("3"));
jPanel3.add(new JButton("3"));
jPanel4.add(new JButton("4"));
jPanel4.add(new JButton("4"));
jPanel4.add(new JButton("4"));
jPanel4.add(new JButton("4"));
jPanel4.add(new JButton("4"));
jPanel4.add(new JButton("4"));
contentPane.add(jPanel1);
contentPane.add(jPanel2);
contentPane.add(jPanel3);
contentPane.add(jPanel4);
this.setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JPanelDemo();
}
}
JScrollPanel:
package com.sjy.lesson5;
import javax.swing.*;
import java.awt.*;
public class JScrollDemo extends JFrame {
public JScrollDemo() throws HeadlessException {
Container contentPane = this.getContentPane();
//文本域
JTextArea jTextArea = new JTextArea(20, 50);
jTextArea.setText("欢迎来到GUI编程");
//Scroll面板
JScrollPane jScrollPane = new JScrollPane(jTextArea);
contentPane.add(jScrollPane);
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.sjy.lesson5;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo1 extends JFrame {
public JButtonDemo1(){
Container contentPane = this.getContentPane();
//将一个图片变为图标
URL resource = JButtonDemo1.class.getResource("1.bmp");
Icon icon = new ImageIcon(resource);
//把这个图标方法按钮上
JButton jButton = new JButton();
jButton.setIcon(icon);
jButton.setToolTipText("图片按钮");
//add
contentPane.add(jButton);
this.setVisible(true);
this.setBounds(100,100,1000,1000);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo1();
}
}
单选按钮:
package com.sjy.lesson5;
import javax.swing.*;
import java.awt.*;
public class JButtonDemo2 extends JFrame {
public JButtonDemo2(){
Container contentPane = this.getContentPane();
//单选框
JRadioButton jRadioButton1 = new JRadioButton("JRadioButton1");
JRadioButton jRadioButton2 = new JRadioButton("JRadioButton2");
JRadioButton jRadioButton3 = new JRadioButton("JRadioButton3");
//由于单选框只能选择一个,分组,一个族中只能选择一个
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(jRadioButton1);
buttonGroup.add(jRadioButton2);
buttonGroup.add(jRadioButton3);
contentPane.add(jRadioButton1,BorderLayout.CENTER);
contentPane.add(jRadioButton2,BorderLayout.NORTH);
contentPane.add(jRadioButton3,BorderLayout.SOUTH);
this.setVisible(true);
this.setBounds(100,100,1000,1000);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo2();
}
}
复选按钮:
package com.sjy.lesson5;
import javax.swing.*;
import java.awt.*;
public class JButtonDemo3 extends JFrame {
public JButtonDemo3(){
Container contentPane = this.getContentPane();
//多选框
JCheckBox checkBox1 = new JCheckBox("checkBox1");
JCheckBox checkBox2 = new JCheckBox("checkBox2");
contentPane.add(checkBox1,BorderLayout.NORTH);
contentPane.add(checkBox2,BorderLayout.SOUTH);
this.setVisible(true);
this.setBounds(100,100,1000,1000);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo3();
}
}
3.6列表
下拉框:
package com.sjy.lesson6;
import javax.swing.*;
import java.awt.*;
public class TestComboBoxDemo1 extends JFrame {
public TestComboBoxDemo1() {
Container contentPane = this.getContentPane();
JComboBox<Object> jComboBox = new JComboBox<>();
jComboBox.addItem(null);
jComboBox.addItem("正在上映");
jComboBox.addItem("已下架");
jComboBox.addItem("即将上映");
contentPane.add(jComboBox);
this.setVisible(true);
this.setBounds(100,100,500,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboBoxDemo1();
}
}
列表框:
package com.sjy.lesson6;
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
public class TestComboBoxDemo2 extends JFrame {
public TestComboBoxDemo2() {
Container contentPane = this.getContentPane();
//生成列表的内容
String[] contents = {"1","2","3"};
//列表中需要放入内容
JList<String> stringJList = new JList<>(contents);
contentPane.add(stringJList);
Vector<Object> vector = new Vector<>();
JList<Object> objectJList = new JList<>(vector);
contentPane.add(objectJList);
vector.add("张三");
vector.add("李四");
vector.add("王麻子");
this.setVisible(true);
this.setBounds(100,100,500,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboBoxDemo2();
}
}
应用场景:
- 选择地区,或者一些单个选项
- 列表,展示信息,一般是动态扩容!
3.7文本框
文本框:
package com.sjy.lesson6;
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
public class TestTextDemo extends JFrame{
public TestTextDemo() {
Container contentPane = this.getContentPane();
JTextField jTextField1 = new JTextField("hello");
JTextField jTextField2 = new JTextField("world",20);
contentPane.add(jTextField1,BorderLayout.NORTH);
contentPane.add(jTextField2,BorderLayout.SOUTH);
this.setVisible(true);
this.setBounds(100,100,500,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextDemo();
}
}
密码框:
package com.sjy.lesson6;
import javax.swing.*;
import java.awt.*;
public class TestTextDemo2 extends JFrame{
public TestTextDemo2() {
Container contentPane = this.getContentPane();
JPasswordField jPasswordField = new JPasswordField();
jPasswordField.setEchoChar('*');
contentPane.add(jPasswordField);
this.setVisible(true);
this.setBounds(100,100,500,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextDemo2();
}
}
文本域:
package com.sjy.lesson5;
import javax.swing.*;
import java.awt.*;
public class JScrollDemo extends JFrame {
public JScrollDemo() throws HeadlessException {
Container contentPane = this.getContentPane();
//文本域
JTextArea jTextArea = new JTextArea(20, 50);
jTextArea.setText("欢迎来到GUI编程");
//Scroll面板
JScrollPane jScrollPane = new JScrollPane(jTextArea);
contentPane.add(jScrollPane);
this.setVisible(true);
this.setBounds(100,100,300,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo();
}
}
上一篇: Python学习笔记 列表、元组、字符串 DAY4
下一篇: 春季食用的蔬菜都有哪些营养价值高?