GUI编程笔记
GUI编程
告诉大家该怎么学?
- 这是什么?
- 它怎么玩?
- 该如何去我们平时运用?
组件 - 窗口
- 弹窗
- 面板
- 文本框
- 列表框
- 按钮
- 图片
- 监听事件
- 鼠标
- 键盘事件
- 破解工具
1、简介
GUi的核心技术:Swing AWT
- 因为界面不美观
- 需要jre环境
为什么我们要学习?
- 可以写出自己心中想要的一些小工具
- 工作时候,也可能需要维护到Swing界面,概率极小
- 了解MVC架构,了解监听!
2、AWT
2.1、Awt介绍
- 包含了很多类和接口
- 元素:窗口,按钮,文本框
- java.awt
2.2 组件和容器
1、Frame
package com.love.lesson01;
import java.awt.*;
public class TestFrame {
public static void main(String[] args) {
//Frame的意思是框架
Frame frame = new Frame("11111");
//设置可见性
frame.setVisible(true);
//设置窗口大小
frame.setSize(400,400);
//设置背景颜色
frame.setBackground(new Color(203, 10, 10, 255));
//弹出的初始位置
frame.setLocation(200,200);
//设置大小固定
frame.setResizable(false);
}
}
问题:发现窗口关闭不掉,停止java程序!
尝试回顾封装
package com.love.lesson01;
import java.awt.*;
public class TestFrame02 {
public static void main(String[] args) {
new MyFrame(100,100,200,200,Color.yellow);
new MyFrame(300,100,200,200,Color.blue);
new MyFrame(100,300,200,200,Color.green);
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);
setResizable(false);
}
}
2、面板Panel
解决了关闭事件!
package com.love.lesson01;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//可以看成一个空间,但是不能单独存在
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(106, 232, 30));
//panel设置坐标,相对于frame
panel.setBounds(50,50,400,400);
panel.setBackground(new Color(233, 10, 10));
frame.add(panel);
frame.setVisible(true);
//frame.setResizable(false); //默认是true,可变大小的
//监听事件,监听窗口关闭事件 System.exit(0)
//适配器模式:
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
2.3、布局管理器
流式布局
package com.love.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(400,400);
//把按钮添加上去
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
}
}
东西南北中
package com.love.lesson01;
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(400,400);
frame.setVisible(true);
}
}
表格布局
package com.love.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.love.homework;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Test01 {
public static void main(String[] args) {
//框架设置
Frame frame = new Frame("Test01");
frame.setSize(400,300);
frame.setLocation(300,400);
frame.setBackground(Color.BLACK);
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));
//Button button = new Button("00");
//button.setBackground(Color.BLUE);
p1.add(new Button("East-1"),BorderLayout.EAST);
p1.add(new Button("West-1"),BorderLayout.WEST);
//p2放两个按钮
p2.add(new Button("p2-btn-1"));
//p2.add(button);
p2.add(new Button("p2-btn -2"));
//再把p2面板放p1面板中间
p1.add(p2,BorderLayout.CENTER);
//下面
p3.add(new Button("East-2"),BorderLayout.EAST);
p3.add(new Button("West-2"),BorderLayout.WEST);
//p4放4个按钮
for (int i = 0; i < 4; i++) {
p4.add(new Button("for-"+i));
}
//再把p4面板放p3面板中间
p3.add(p4,BorderLayout.CENTER);
//把两个面板放进框架中
frame.add(p1);
frame.add(p3);
//监听事件,监听窗口关闭事件 System.exit(0)
//适配器模式:
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
总结:
- Frame是一个顶级窗口
- Panel无法单独显示,必须添加到某个容器中。
- 布局管理器
- 流式
- 东西南北中
- 表格
4.大小,定位,背景颜色,可见性,监听!
2.4、事件监听
事件监听:当某个事情发生的时候,干什么?
package com.love.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("button");
//因为,addActionListener()需要一个ActionListener,所以我们需要构造一个ActionListener
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("aaa");
}
};
button.addActionListener(actionListener);
frame.add(button,BorderLayout.CENTER);
frame.setBounds(400,400,300,300);
frame.pack();
windowClose(frame);
frame.setVisible(true);
}
public static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
多个按钮共享一个事件
package com.love.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 TestActionTwo {
public static void main(String[] args) {
Frame frame = new Frame("开始-停止");
Button button1 = new Button("start");
Button button2 = new Button("stop");
button1.setActionCommand("button-1-");
MyMonitor myMonitor = new MyMonitor();
button1.addActionListener(myMonitor);
button2.addActionListener(myMonitor);
frame.add(button1,BorderLayout.NORTH);
frame.add(button2,BorderLayout.SOUTH);
frame.pack();//自适应大小按钮
windowClose(frame); //关闭窗口
frame.setVisible(true);//可见性
}
//监听事件,监听窗口关闭事件 System.exit(0)
//适配器模式:
public static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyMonitor implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//获得按钮点击信息
String message = e.getActionCommand();
System.out.println("按钮被点击了:"+e.getActionCommand());
}
}
2.5、输入框TextField监听
package com.love.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 TextText01 {
public static void main(String[] args) {
//启动
new MyFrame();
}
}
//我的框架对象
class MyFrame extends Frame{
public MyFrame(){
TextField textField = new TextField();//一行文本框
//因为是继承frame,所以不用加Frame. 直接用Frame里面的方法
add(textField);
MyActionListener2 myActionListener2 = new MyActionListener2();
textField.addActionListener(myActionListener2);
//设置替换编码
textField.setEchoChar('*');
setLocation(500,500);
windowClose();
pack();
setVisible(true);
}
//关闭窗口
public void windowClose(){
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
//我的动作监听器
class MyActionListener2 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField field = (TextField) e.getSource();//获取一些资源,返回的一个对象
System.out.println(field.getText());//获取输入文本信息
field.setText("");//回车一次清空一次文本框
}
}
2.6、简易计算器,组合+内部类回顾复习
oop原则:组合,大于继承!
package com.love.lesson02;
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(){
//三个文本框
TextField num1 = new TextField(10);//最多字符数
TextField num2 = new TextField(10);
TextField num3 = new TextField(20);
//一个按钮
Button button = new Button("=");
//增加按钮监听事件
button.addActionListener(new MyCalculatorListener(num1,num2,num3));
//一个标签
Label label = new Label("+");
add(num1);
add(label);
add(num2);
add(button);
add(num3);
//布局
setLayout(new FlowLayout());
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;
}
@Override
public 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("");
}
}*/
//--------------------------------------------------------------------
/*
//简易计算器_完全改造为面向对象写法
public class TestCalc {
public static void main(String[] args) {
new Calculator();
}
}
//计算器类
class Calculator extends Frame{
//属性
TextField num1,num2,num3;
public Calculator(){
//三个文本框
num1 = new TextField(10);//最多字符数
num2 = new TextField(10);
num3 = new TextField(20);
//一个按钮
Button button = new Button("=");
//增加按钮监听事件
button.addActionListener(new MyCalculatorListener(this));
//一个标签
Label label = new Label("+");
add(num1);
add(label);
add(num2);
add(button);
add(num3);
//布局
setLayout(new FlowLayout());
pack();
setVisible(true);
}
}
//监听器类 动作监听器
class MyCalculatorListener implements ActionListener{
//获取计算器这个对象,在一个类中组合成为另一个类
Calculator calculator = null;
public MyCalculatorListener(Calculator calculator){
this.calculator = calculator;
}
@Override
public 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("");
}
}
*/
//--------------------------------------------------------------------
//简易计算器_内部类
public class TestCalc {
public static void main(String[] args) {
new Calculator().loadFrame();
}
}
//计算器类
class Calculator extends Frame{
//属性
TextField num1,num2,num3;
public void loadFrame(){
//三个文本框
num1 = new TextField(10);//最多字符数
num2 = new TextField(10);
num3 = new TextField(20);
//一个按钮
Button button = new Button("=");
//增加按钮监听事件
button.addActionListener(new MyCalculatorListener());
//一个标签
Label label = new Label("+");
add(num1);
add(label);
add(num2);
add(button);
add(num3);
//布局
setLayout(new FlowLayout());
pack();
setVisible(true);
}
//监听器类 动作监听器
//内部类最大的好处,就是可以畅通无阻的访问外部的属性和方法
private class MyCalculatorListener implements ActionListener{
@Override
public 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、画笔
package com.love.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.red);
g.fillOval(100,100,100,100);
g.setColor(Color.GREEN);
g.fillRect(150,200,200,200);
//养成习惯,画笔用完将他还原到最初的颜色
g.setColor(Color.BLACK);
}
}
2.8、鼠标监听
目的:想要实现鼠标画画!
package com.love.lesson03;
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,400);
//存储鼠标的点
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.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) {
MyFrame frame = (MyFrame)e.getSource();
frame.addPaint(new Point(e.getX(),e.getY()));
//每次点击鼠标都需要重新画一遍
//frame.repaint();//刷新
}
}
}
2.9、窗口监听
package com.love.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.BLUE);
setBounds(100,100,200,200);
setVisible(true);
addWindowListener(
//匿名内部类
new WindowAdapter() {
//关闭窗口
@Override
public void windowClosing(WindowEvent e) {
System.out.println("WindowClosing");
System.exit(0);
}
//激活窗口
@Override
public void windowActivated(WindowEvent e) {
WindowFrame source = (WindowFrame)e.getSource();
source.setTitle("被激活了");
System.out.println("windowActivated");
}
});
}
}
2.10、键盘监听
package com.love.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();
System.out.println(keyCode);
if(keyCode == KeyEvent.VK_UP){
System.out.println("你按下了上键");
}
}
});
}
}
3、Swing
3.1、窗口、面板
package com.love.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.setBounds(10,10,400,600);
this.setVisible(true);
JLabel label = new JLabel("欢迎");
this.add(label);
//让文本标签居中,设置水平对齐
label.setHorizontalAlignment(SwingConstants.CENTER);
//获得一个容器
Container container = this.getContentPane();
container.setBackground(Color.YELLOW);
}
}
标签居中
package com.love.lesson04;
import javax.swing.*;
import java.awt.*;
public class JFrameDemo{
public static void main(String[] args) {
new JFrameDemo().init();
}
//init(); 初始化
public void init(){
JFrame jf = new JFrame("这是一个JFrame窗口");
jf.setVisible(true);
jf.setBounds(100,100,200,200);
//jf.setBackground(Color.CYAN);
JLabel label = new JLabel("这是一个JLabel");
jf.add(label);
label.setHorizontalAlignment(SwingConstants.CENTER);
//关闭事件
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = jf.getContentPane();
container.setBackground(Color.YELLOW);
}
}
3.2、弹窗
package com.love.lesson04;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//主窗口
public class DialogDemo extends JFrame {
public static void main(String[] args) {
new DialogDemo();
}
public DialogDemo(){
this.setVisible(true);
this.setSize(700,500);
this.setLocation(100,200);
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 MyDialogDemo();
}
});
container.add(button);
}
}
//弹窗
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.love.lesson04;
import javax.swing.*;
import java.awt.*;
public class IconDemo extends JFrame implements Icon {
public static void main(String[] args) {
new IconDemo().init();
}
private int width;
private int height;
public IconDemo(){
}
public IconDemo(int width, int height){
this.width = width;
this.height = height;
}
public void init(){
IconDemo iconDemo = new IconDemo(30, 30);
//图标放在标签上
JLabel label = new JLabel("iconTest", iconDemo, SwingConstants.CENTER);
Container container = getContentPane();
container.add(label);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillOval(x,y,width,height);
}
@Override
public int getIconWidth() {
return width;
}
@Override
public int getIconHeight() {
return height;
}
}
图片Icon
package com.love.lesson04;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class ImageIconDemo extends JFrame {
public static void main(String[] args) {
new ImageIconDemo();
}
public ImageIconDemo(){
JLabel label = new JLabel("ImageIcon");
//获取图片地址 https://i.loli.net/2020/10/23/zcRDbvyjxgYa7km.png
URL url = ImageIconDemo.class.getResource("123tp.jpg");
//为啥不显示图片,我不清楚,应该是在Mac电脑的原因,下次回Windows系统试一试
//ok了,我换成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);
}
}
3.4、面板
JPanel
package com.love.lesson05;
import javax.swing.*;
import java.awt.*;
public class JPanelDemo extends JFrame {
public static void main(String[] args) {
new JPanelDemo();
}
public JPanelDemo(){
//Container:容器
Container container = this.getContentPane();
//container.setLayout(new GridLayout(2,1));
//后面参数意思是间距
container.setLayout(new GridLayout(2,1,10,10));
JPanel panel1 = new JPanel(new GridLayout(1, 3));
JPanel panel2 = new JPanel(new GridLayout(1, 2));
JPanel panel3 = new JPanel(new GridLayout(2, 1));
JPanel panel4 = new JPanel(new GridLayout(3, 2));
panel1.add(new Button("1"));
panel1.add(new Button("1"));
panel1.add(new Button("1"));
panel2.add(new Button("2"));
panel2.add(new Button("2"));
panel3.add(new Button("3"));
panel3.add(new Button("3"));
panel4.add(new Button("4"));
panel4.add(new Button("4"));
panel4.add(new Button("4"));
panel4.add(new Button("4"));
panel4.add(new Button("4"));
panel4.add(new Button("4"));
container.add(panel1);
container.add(panel2);
container.add(panel3);
container.add(panel4);
this.setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
JScrollPanel
package com.love.lesson05;
import javax.swing.*;
import java.awt.*;
public class JScrollDemo extends JFrame {
public static void main(String[] args) {
new JScrollDemo();
}
public JScrollDemo(){
//获取内容面板
Container container = this.getContentPane();
//文本域
JTextArea textArea = new JTextArea(20,50);
textArea.setText(" Who am i");
//Scroll(滚动)面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setVisible(true);
this.setBounds(100,100,300,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
3.5、按钮
图片按钮
package com.love.lesson05;
import com.love.lesson04.ImageIconDemo;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo01 extends JFrame {
public static void main(String[] args) {
new JButtonDemo01();
}
public JButtonDemo01(){
Container container = this.getContentPane();
//将一个图片变为图标
URL url = JButtonDemo01.class.getResource("123tp.jpg");
//icon:图标
ImageIcon icon = new ImageIcon(url);
//这个图标放在按钮上
JButton button = new JButton();
button.setIcon(icon);
//Tool Tip Text 工具提示文本
button.setToolTipText("图片按钮");
container.add(button);
this.setVisible(true);
this.setSize(500,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
单选按钮
package com.love.lesson05;
import com.love.lesson04.ImageIconDemo;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo02 extends JFrame {
public static void main(String[] args) {
new JButtonDemo02();
}
public JButtonDemo02(){
Container container = this.getContentPane();
//将一个图片变为图标
URL url = JButtonDemo01.class.getResource("123tp.jpg");
//icon:图标
ImageIcon icon = new ImageIcon(url);
//单选框
JRadioButton radioButton01 = new JRadioButton("JRadioButton01");
JRadioButton radioButton02 = new JRadioButton("JRadioButton02");
JRadioButton radioButton03 = new JRadioButton("JRadioButton03");
//由于单选框只能选择一个,所以进行分组
ButtonGroup group = new ButtonGroup();
group.add(radioButton01);
group.add(radioButton02);
group.add(radioButton03);
//加到容器中
container.add(radioButton01,BorderLayout.NORTH);
container.add(radioButton02,BorderLayout.CENTER);
container.add(radioButton03,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(500,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
复选按钮
package com.love.lesson05;
import com.love.lesson04.ImageIconDemo;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo03 extends JFrame {
public static void main(String[] args) {
new JButtonDemo03();
}
public JButtonDemo03(){
Container container = this.getContentPane();
//将一个图片变为图标
URL url = JButtonDemo01.class.getResource("123tp.jpg");
//icon:图标
ImageIcon icon = new ImageIcon(url);
//复选框
Checkbox checkbox01 = new Checkbox("checkbox01");
Checkbox checkbox02 = new Checkbox("checkbox02");
//复选框可以选多个,不用进行分组,
//加到容器中
container.add(checkbox01,BorderLayout.NORTH);
container.add(checkbox02,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(500,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
3.6、列表
下拉框
package com.love.lesson06;
import javax.swing.*;
import java.awt.*;
/**
* 下拉框
*/
public class TestComboboxDemo01 extends JFrame {
public static void main(String[] args) {
new TestComboboxDemo01();
}
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.setSize(500,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
列表框
package com.love.lesson06;
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
/**
* 列表框
*/
public class TestComboboxDemo02 extends JFrame {
public static void main(String[] args) {
new TestComboboxDemo02();
}
public TestComboboxDemo02(){
Container container = this.getContentPane();
//生成列表内容
//String[] contents = {"1","2","3"};
//动态添加
Vector contents = new Vector();
//列表中需要放入内容
JList jList = new JList(contents);
contents.add("zhangsan");
contents.add("lisi");
contents.add("wangwu");
container.add(jList);
this.setVisible(true);
this.setSize(500,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
应用场景
- 选择地区,或者一些单个选项
- 列表,展示信息,一般是动态扩容!
3.7、文本框
文本框
package com.love.lesson06;
import javax.swing.*;
import java.awt.*;
/**
*文本框
*/
public class TestTextDemo01 extends JFrame {
public static void main(String[] args) {
new TestTextDemo01();
}
public TestTextDemo01(){
Container container = this.getContentPane();
JTextField textField = new JTextField("hello");
JTextField textField2 = new JTextField("world");
container.add(textField,BorderLayout.SOUTH);
container.add(textField2,BorderLayout.NORTH);
this.setVisible(true);
this.setSize(500,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
密码框
package com.love.lesson06;
import javax.swing.*;
import java.awt.*;
/**
*密码框
*/
public class TestTextDemo02 extends JFrame {
public static void main(String[] args) {
new TestTextDemo02();
}
public TestTextDemo02(){
Container container = this.getContentPane();
JPasswordField passwordField = new JPasswordField();
passwordField.setEchoChar('*');
container.add(passwordField);
this.setVisible(true);
this.setSize(500,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
文本域
package com.love.lesson05;
import javax.swing.*;
import java.awt.*;
public class JScrollDemo extends JFrame {
public static void main(String[] args) {
new JScrollDemo();
}
public JScrollDemo(){
//获取内容面板
Container container = this.getContentPane();
//文本域
JTextArea textArea = new JTextArea(20,50);
textArea.setText(" Who am i");
//Scroll(滚动)面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setVisible(true);
this.setBounds(100,100,300,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
GUI编程笔记的更多相关文章
- GUI编程笔记(java)09:GUI控制文本框只能输入数字字符案例
1.首先我们看看我的需求,如下: 控制文本框只能输入数字字符 2.源代码: package cn.itcast_07; import java.awt.FlowLayout; import jav ...
- GUI编程笔记(java)01:GUI和CLI
GUI Graphical User Interface(图形用户接口). 用图形的方式,来显示计算机操作的界面,这样更方便更直观. CLI Command line User Interface ( ...
- GUI编程笔记(java)11:使用Netbeans工具进行GUI编程
Netbeans工具:是基于java语言进行GUI界面设计的工具 Visual Studio工具:是基于C#语言进行GUI界面设计的工具
- GUI编程笔记(java)10:GUI实现一级菜单
1.首先:菜单组件 MenuBar,Menu,MenuItem 先创建菜单条,再创建菜单,每一个菜单中建立菜单项. 也可以菜单添加到菜单中,作为子菜 ...
- GUI编程笔记(java)08:GUI通过鼠标移动到按钮上更改背景色案例
首先我们看看源代码如下: package cn.itcast_06; import java.awt.Button; import java.awt.Color; import java.awt.Fl ...
- GUI编程笔记(java)07:GUI把文本框的值移到文本域案例
1.首先我们了解一下我们的需求,如下: 输入“风清扬”,点击“数据转移”,这样的文本会出现到下面的文本域中,这就是我们的需求. 2.代码如下: package cn.itcast_05; import ...
- GUI编程笔记(java)06:GUI窗体添加按钮并对按钮添加事件案例
1.需求:把按钮添加到窗体,并对按钮添加一个点击事件. 步骤: (1)创建窗体对象(2)创建按钮对象(3)把按钮添加到窗体(4)窗体显示 2.编写程序思路: 窗体布局:窗体中组件的排列方式 布局分类 ...
- GUI编程笔记(java)05:GUI事件监听机制原理和举例说明
1.事件监听机制: A:事件源 事件发生的地方 B:事件 就是要发生的事情 C:事件处理 就是针对发生的事情做 ...
- GUI编程笔记(java)04:GUI(HelloWorld)窗体案例
1.Frame 在JAVA中,Frame是一种控件,可作为父窗体加载其他swing控件.案例: package cn.itcast_01; import java.awt.Frame; public ...
随机推荐
- 备战秋招之十大排序——O(n^2)级排序算法
一.冒泡排序 冒泡排序是入门级的算法,但也有一些有趣的玩法.通常来说,冒泡排序有三种写法: 一边比较一边向后两两交换,将最大值 / 最小值冒泡到最后一位: 经过优化的写法:使用一个变量记录当前轮次的比 ...
- SpringBoot监听redis过期key
开启过期监听 vim /etc/redis.conf 取消notify-keyspace-events Elg的注释 pom.xml 添加: <dependency> <groupI ...
- 从350ms到80ms,揭秘阿里工程师 iOS 短视频优化方案
内容作为 App 产品新的促活点,受到了越来越多的重视与投入,短视频则是增加用户粘性.增加用户停留时长的一把利器.短视频的内容与体验直接关系到用户是否愿意长时停留,盒马也提出全链路内容视频化的规划,以 ...
- Spring @Order注解的使用
注解@Order或者接口Ordered的作用是定义Spring IOC容器中Bean的执行顺序的优先级,而不是定义Bean的加载顺序,Bean的加载顺序不受@Order或Ordered接口的影响: 1 ...
- 【CSS简介、基础选择器、字体属性、文本属性、引入方式】前端小抄(2) - Pink老师自学笔记
[CSS简介.基础选择器.字体属性.文本属性.引入方式]前端小抄(2) 本学习笔记是个人对 Pink 老师课程的总结归纳,转载请注明出处! 一.CSS简介 CSS 的主要使用场景就是布局网页,美化页面 ...
- easyexcel
导出: package com.example.demo.excel.demo0; import com.alibaba.excel.annotation.ExcelProperty; import ...
- RabbitMq内存分页
- cmd(命令行 )的命令
cmd是command的缩写.即命令行 CMD命令锦集 1. gpedit.msc-----组策略 2. sndrec32-------录音机 3. Nslookup-------IP地址侦测器 ,是 ...
- 什么是云效持续集成?如何关联Jenkins进行持续集成?
什么是云效持续集成?如何关联Jenkins进行持续集成?云效流水线 Flow是一款企业级.自动化的研发交付流水线, 提供灵活易用的持续集成.持续验证. 持续发布功能,帮助企业高质量.高效率的交付业务. ...
- javascript(2)运算符
### js运算符 1.运算符 1.typeof 获取当前变量类型 运算符(特殊) 2.= 赋值运算符 3.== 简要比较运算符(忽略变量的类型) 4.=== 标准比较运算符(严格变量的类型.判断是否 ...