GUI编程!
组件:
- 窗口
- 弹窗
- 面板
- 文本框
- 列表框
- 按钮
- 图片
- 监听事件
- 鼠标事件
- 键盘事件
1.简介
GUI的核心技术:Swing AWT
不流行的原因
1.因为页面不美观
2.需要jre环境!
2.AWT
2.1AWT介绍
1.包含了很多类和接口!GUI!
2.元素:窗口,按钮,文本框
3.java.awt
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NZ0OI4k1-1629811816154)(I:\JavaScript自学笔记\images\image-20210822165617181.png)]
2.2组件和容器
1.Frame
package com.Su.lesson01;
import java.awt.*;
public class TestFrame {
public static void main(String[] args) {
//Frame对象,jdk
Frame frame = new Frame("我的一个java图形窗口");
//需要设置可见性
frame.setVisible(true);
//设置窗口大小
frame.setSize(400,400);
//设置背景颜色
frame.setBackground(new Color(20, 234, 38));
//弹出的初始位置
frame.setLocation(200,200);
//设置大小固定
frame.setResizable(false);
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TwyvWRHS-1629811816156)(I:\JavaScript自学笔记\images\image-20210822170949504.png)]
问题:发现窗口关闭不掉,停止java运行
package com.Su.lesson01;
import java.awt.*;
public class TestFrame01 {
public static void main(String[] args) {
//展示多个窗口
MyFrame myFrame1 = new MyFrame(100,100,200,200,Color.cyan);
MyFrame myFrame2 = new MyFrame(300,300,200,200,Color.yellow);
MyFrame myFrame3 = new MyFrame(100,100,200,200,Color.pink);
MyFrame myFrame4 = new MyFrame(300,300,200,200,Color.magenta);
}
}
class MyFrame extends Frame{
static int id = 0;//可能存在多个窗口,我们需要一个计数器
public MyFrame(int x,int y,int w,int h,Color color){
super("MyFrame"+(++id));
setBackground(color);
setBounds(x,y,w,h);
setVisible(true);
}
}
2.面板panel
解决了关闭事件!
package com.Su.lesson01;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
/**
* 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(20, 234, 38));
//panel设置坐标,相对于frame
panel.setBounds(50,50,400,400);
panel.setBackground(new Color(7, 101, 7, 253));
//frame.add(panel)
frame.add(panel);
frame.setVisible(true);
//监听事件,监听关闭窗口事件System.exit(0)
//适配器模式 :
frame.addWindowListener(new WindowAdapter() {
//点击关闭的时候要做的事情
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
//结束程序
});
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-w2RH9nv0-1629811816158)(I:\JavaScript自学笔记\images\image-20210823091543490.png)]
2.3.布局管理系统
- 流式布局
package com.Su.lesson01;
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.setSize(200,200);
//添加按钮
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
- 东西南北中
package com.Su.lesson01;
import java.awt.*;
public class TestBorderLayout {
public static void main(String[] args) {
Frame frame = new Frame("BorderLayout");
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(500,500);
frame.setVisible(true);
}
}
- 表格布局
package com.Su.lesson01;
import java.awt.*;
public class TestGirdLayout {
public static void main(String[] args) {
Frame frame = new Frame("GirdLayout");
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();
frame.setSize(500,500);
frame.setVisible(true);
}
}
布局练习
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Zi8PPsM9-1629811816159)(I:\JavaScript自学笔记\images\image-20210823102306111.png)]
代码
package com.Su.lesson01;
import java.awt.*;
public class classTest {
public static void main(String[] args) {
Frame frame = new Frame("课堂练习");
frame.setVisible(true);
frame.setLayout(new GridLayout(2,1));
frame.setSize(300,400);
frame.setLocation(400,300);
Panel p1 = new Panel();
Panel p2 = new Panel();
Panel p3 = new Panel();
Panel p4 = new Panel();
p1.setLayout(new BorderLayout(0,0));
p1.setBounds(0,0,300,200);
p1.add(new Button("East-1"),BorderLayout.EAST);
p1.add(p2,BorderLayout.CENTER);
p1.add(new Button("West-1"),BorderLayout.WEST);
p2.setLayout(new GridLayout(2,1));
p2.add(new Button("Center-1"));
p2.add(new Button("Center-2"));
p3.setLayout(new BorderLayout(0,0));
p3.setBounds(0,200,300,200);
p3.add(new Button("East-2"),BorderLayout.EAST);
p3.add(p4,BorderLayout.CENTER);
p3.add(new Button("West-2"),BorderLayout.WEST);
p4.setLayout(new GridLayout(2,2));
p4.add(new Button("Center-3"));
p4.add(new Button("Center-4"));
p4.add(new Button("Center-5"));
p4.add(new Button("Center-6"));
frame.add(p1);
frame.add(p3);
}
}
总结
1.Frame是一个顶级窗口
2.panel无法单独显示,必须添加到某个容器中
3.布局管理器
1.流式
2.东西南北中
3.表格
4.大小,定位,背景颜色,可见性,监听!
2.4.事件监听
事件监听:当某个事情发生的时候,干什么?
package com.Su.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 TestAction {
public static void main(String[] args) {
//按下按钮会触发生什么
Frame frame = new Frame();
Button button = new Button();
//因为addActionListener需要一个ActionListener,所以我们需要构造出一个ActionListener
MyActionListener myActionListener = new MyActionListener();
button.addActionListener(myActionListener);
//文本框
windClosing(frame);
frame.add(button,BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
//关闭窗口事件
public static void windClosing(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
//事件监听!
class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("苏旭大帅逼!");
}
}
多个按钮,共享一个事件
package com.Su.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 TestActionEven {
public static void main(String[] args) {
//两个按钮事件同一个监听
//开始按钮 停止按钮
Frame frame = new Frame("开始-停止");
Button button = new Button("start");
Button button1 = new Button("stop");
//可以显示的定义的触发会返回的命令,如果不显示定义则会走默认的值
//可以多个按钮只写一个监听类
button1.setActionCommand("button1-stop");
MyMonitor myMonitor = new MyMonitor();
button.addActionListener(myMonitor);
button1.addActionListener(myMonitor);
frame.add(button,BorderLayout.SOUTH);
frame.add(button1,BorderLayout.NORTH);
windClosing(frame);
frame.setVisible(true);
frame.pack();
}
public static void windClosing(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) {
//获得按钮的信息
System.out.println("按钮被点击了:msg"+e.getActionCommand());
}
}
2.5.输入框TextField监听
package com.Su.lesson02;
import javax.xml.soap.Text;
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{
@Override
public void actionPerformed(ActionEvent e) {
TextField field = (TextField) (e.getSource());//获得一些资源,返回了一个对象
System.out.println(field.getText());//获得输入框的文本
field.setText("");
}
}
2.6.简单计算器,组和+内部类回顾复习!
opp原则:组和大于继承
class A extends B{
//这是继承
}
class A{
public B b;
这是组和
}
package com.Su.lesson02;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* 简易计算器
*/
public class TestCalc {
public static void main(String[] args) {
Calclator calclator = new Calclator();
}
}
class Calclator extends Frame{
public Calclator(){
//三个文本框
TextField number1 = new TextField(10);//字符数
TextField number2 = new TextField(10);//字符数
TextField number3 = new TextField(20);//字符数
//1个按钮
Button button = new Button("=");
button.addActionListener(new MyCalclatorListener(number1,number2,number3));
//一个标签
Label label = new Label("+");
//布局
setLayout(new FlowLayout());
add(number1);
add(label);
add(number2);
add(button);
add(number3);
pack();
setVisible(true);
}
}
//监听器类
class MyCalclatorListener implements ActionListener{
//获取三个变量
private TextField number1,number2,number3;
public MyCalclatorListener(TextField number1, TextField number2, TextField number3) {
this.number1 = number1;
this.number2 = number2;
this.number3 = number3;
}
@Override
public void actionPerformed(ActionEvent e) {
//1.获得加数和被加数
int n1 = Integer.parseInt(number1.getText());
int n2 = Integer.parseInt(number2.getText());
//2.将这个值加法运算后放到第三个框
number3.setText(""+(n1+n2));
//3.清除前两个框
number1.setText("");
number2.setText("");
}
}
完全改造成面向对象
package com.Su.lesson02;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestText02 {
public static void main(String[] args) {
new CesiumMath().Addition();
}
}
class CesiumMath extends Frame {
TextField num1,num2,num3;
public void Addition(){
num1 = new TextField(10);//字符数
num2 = new TextField(10);//字符数
num3 = new TextField(20);//字符数
Button button = new Button("=");
button.addActionListener(new MyCesiumMathListener(this));
Label label = new Label("+");
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
}
class MyCesiumMathListener implements ActionListener{
CesiumMath cesiumMath = null;
public MyCesiumMathListener(CesiumMath cesiumMath){
this.cesiumMath = cesiumMath ;
}
@Override
public void actionPerformed(ActionEvent e) {
int n1 = Integer.parseInt(cesiumMath.num1.getText());
int n2 = Integer.parseInt(cesiumMath.num2.getText());
//2.将这个值加法运算后放到第三个框
cesiumMath.num3.setText(""+(n1+n2));
//3.清除前两个框
cesiumMath.num1.setText("");
cesiumMath.num2.setText("");
}
}
更好的包装
- 内部类(内部类可以畅通无阻的访问外部类)
package com.Su.lesson02;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestText02 {
public static void main(String[] args) {
new CesiumMath().Addition();
}
}
class CesiumMath extends Frame {
TextField num1,num2,num3;
public void Addition(){
num1 = new TextField(10);//字符数
num2 = new TextField(10);//字符数
num3 = new TextField(20);//字符数
Button button = new Button("=");
button.addActionListener(new MyCesiumMathListener());
Label label = new Label("+");
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
private class MyCesiumMathListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
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.Su.lesson03;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestPaint {
public static void main(String[] args) {
new MyPaint().localFrameP();
}
}
class MyPaint extends Frame {
public void localFrameP(){
setBounds(200,200,800,800);
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
//画笔
@Override
public void paint(Graphics g){
//画笔需要有颜色
//画笔需要画画
g.setColor(Color.red);
g.draw3DRect(100,100,100,100,true);
}
}
2.8鼠标监听
目的:想要实现鼠标的画画
package com.Su.lesson03;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
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,300,300);
//存鼠标监听的点
points = new ArrayList<>();
//鼠标监听器,正对这个窗口
this.addMouseListener(new MyMouseListener());
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
@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 myFrame = (MyFrame) e.getSource();
//点击的时候就会在界面上产生一个点!
//这个点就是鼠标的点
myFrame.addPaint(new Point(e.getX(),e.getY()));
//每次点击鼠标都要重新画一遍
myFrame.repaint();//刷新
}
}
}
2.9窗口监听
package com.Su.lesson03;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestWindow {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame{
public WindowFrame(){
setBackground(Color.CYAN);
setBounds(100,100,300,300);
setVisible(true);
//addWindowListener(new MyWindowListener());
this.addWindowListener(
new WindowAdapter() {
//关闭窗口
@Override
public void windowClosing(WindowEvent e) {
System.out.println("WindowFrame.windowClosing");
System.exit(0);
}
//激活窗口
@Override
public void windowActivated(WindowEvent e) {
WindowFrame source = (WindowFrame) e.getSource();
source.setTitle("被激活了");
System.out.println("WindowFrame.windowActivated");
}
}
);
}
}
2.10键盘监听
package com.Su.lesson03;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class TestKeyListener {
public static void main(String[] args) {
new KeyFrame();
}
}
class KeyFrame extends Frame{
public KeyFrame(){
setBounds(1,2,300,400);
setVisible(true);
this.addKeyListener(new KeyAdapter() {
//键盘按下
@Override
public void keyPressed(KeyEvent e) {
//获得键盘按下的键是哪一个,当前码
int KeyCode = e.getKeyCode();
if (KeyCode == KeyEvent.VK_UP){
System.out.println("你按下了上键");
}
//根据按下的键位不同来实现不同的结果
}
});
}
}
3.Swing
3.1窗口,面板
package com.Su.lesson04;
import javax.swing.*;
import java.awt.*;
public class JFrameDemo {
//init();初始化
public void init(){
JFrame jf = new JFrame("这是一个JFrame窗口");
jf.setVisible(true);
jf.setBounds(1,1,300,300);
//设置背景颜色!
jf.getContentPane().setBackground(Color.CYAN);
//设置文字Jlabel
JLabel label = new JLabel("欢迎来王者荣耀",SwingConstants.CENTER);
jf.add(label);
//关闭事件
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
//建立一个窗口
new JFrameDemo().init();
}
}
3.2弹窗
JDialog 用来被弹出,默认就有关闭事件
package com.Su.lesson04;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//主窗口
public class DialogDemo extends JFrame {
public DialogDemo() {
setVisible(true);
setSize(700,500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//JFrame 放东西容器
Container container = 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 MyDialogListener();
}
});
container.add(jButton);
}
public static void main(String[] args) {
new DialogDemo();
}
}
//弹窗的窗口
class MyDialogListener extends JDialog{
public MyDialogListener() {
this.setVisible(true);
this.setBounds(100,100,500,500);
Container container = this.getContentPane();
// container.setLayout(null);
container.add(new JLabel("苏大旭学java",SwingConstants.CENTER));
}
}
3.3标签
label
new JLabel("XXXX")
图标 Icon
package com.Su.lesson04;
import javax.swing.*;
import java.awt.*;
/**
* 图标,需要实现类,Frame继承
*/
public class IconDemo extends JFrame implements Icon {
private int width;
private int height;
public IconDemo(int width, int height) {
this.width = width;
this.height = height;
}
public IconDemo() {
}
public void init(){
IconDemo iconDemo = new IconDemo(15,15);
//图标放在标签上,也可以放在按钮上
JLabel label = new JLabel("icontest", iconDemo, SwingConstants.CENTER);
Container container = getContentPane();
container.add(label);
setVisible(true);
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.Su.lesson04;
import javax.swing.*;
import java.awt.*;
public class IconImageDemo extends JFrame {
public IconImageDemo(){
JLabel label = new JLabel("ImageIcon");
String url = "I:\\code\\Java_code\\GUI\\src\\com\\Su\\lesson04\\pm.png";
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 IconImageDemo();
}
}
3.4面板
JPanel
package com.Su.lesson05;
import javax.swing.*;
import java.awt.*;
public class TestJPanelDemo extends JFrame {
public TestJPanelDemo() {
Container container = getContentPane();
container.setLayout(new GridLayout(2,1,10,10));//后面两个是间距的意思
JPanel panel1 = new JPanel(new GridLayout(1,3));
panel1.add(new JButton("1"));
panel1.add(new JButton("1"));
panel1.add(new JButton("1"));
container.add(panel1);
setVisible(true);
setBounds(0,0,800,800);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJPanelDemo();
}
}
JScroll
package com.Su.lesson05;
import javax.swing.*;
import java.awt.*;
public class JScrollDemo extends JFrame {
public JScrollDemo(){
Container container = getContentPane();
//文本域 可以换行
JTextArea textArea = new JTextArea(20, 50);
textArea.setText("欢迎学医java");
//Scroll面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
setVisible(true);
setBounds(100,100,300,500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo();
}
}
3.5按钮
图片按钮
package com.Su.lesson05;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo extends JFrame {
public JButtonDemo(){
Container container = getContentPane();
//讲一个图片转化为一个图标
URL resource = JButtonDemo.class.getResource("pm.jpg");
Icon icon = new ImageIcon(resource);
//把这个图标放在按钮上
JButton button = new JButton();
button.setIcon(icon);
button.setToolTipText("图片按钮" );
container.add(button);
setVisible(true);
setBounds(100,100,500,500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo();
}
}
- 单选按钮
package com.Su.lesson05;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo02 extends JFrame {
public JButtonDemo02(){
Container container = getContentPane();
//讲一个图片转化为一个图标
URL resource = JButtonDemo02.class.getResource("pm.jpg");
Icon icon = new ImageIcon(resource);
JRadioButton button1 = new JRadioButton("JRadioButton01");
JRadioButton button2 = new JRadioButton("JRadioButton02");
JRadioButton button3 = new JRadioButton("JRadioButton03");
//由于单选框只能选择一个,分组 一个组中只能选一个
ButtonGroup group = new ButtonGroup();
group.add(button1);
group.add(button2);
group.add(button3);
container.add(button1,BorderLayout.CENTER);
container.add(button2,BorderLayout.NORTH);
container.add(button3,BorderLayout.SOUTH);
setVisible(true);
setBounds(100,100,500,500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo02();
}
}
- 复选按钮
package com.Su.lesson05;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo03 extends JFrame {
public JButtonDemo03(){
Container container = getContentPane();
//讲一个图片转化为一个图标
URL resource = JButtonDemo03.class.getResource("pm.jpg");
Icon icon = new ImageIcon(resource);
//多选框
Checkbox checkbox01 = new Checkbox("Box01");
Checkbox checkbox02 = new Checkbox("Box02");
Checkbox checkbox03 = new Checkbox("Box03");
container.add(checkbox01,BorderLayout.CENTER);
container.add(checkbox02,BorderLayout.SOUTH);
container.add(checkbox03,BorderLayout.NORTH);
setVisible(true);
setBounds(100,100,500,500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo03();
}
}
3.6列表
- 下拉框
package com.Su.lesson06;
import javafx.scene.control.ComboBox;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class TestComboboxDemo01 extends JFrame {
public TestComboboxDemo01() {
Container container = getContentPane();
JComboBox comboBox = new JComboBox();
comboBox.addItem(null);
comboBox.addItem("正在上映");
comboBox.addItem("已经下架");
comboBox.addItem("即将上映");
container.add(comboBox);
setVisible(true);
setBounds(100,100,600,600);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboboxDemo01();
}
}
- 列表框
package com.Su.lesson06;
import javax.swing.*;
import java.awt.*;
public class TestComboboxDemo02 extends JFrame {
public TestComboboxDemo02() {
Container container = getContentPane();
//生成列表的内容
String[] contents = {"1","2","3"};
//列表中需要放入内容
JList jList = new JList(contents);
container.add(jList);
setVisible(true);
setBounds(100,100,600,600);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboboxDemo02();
}
}
- 应用场景(下拉框)
- 选择地区,或者一些单个选项
- 列表,展示信息,一般就是动态扩容
3.7文本框
- 文本框 JTextField
package com.Su.lesson06;
import javax.swing.*;
import java.awt.*;
public class TestTextDemo01 extends JFrame {
public static void main(String[] args) {
new TestTextDemo01();
}
public TestTextDemo01(){
Container container = getContentPane();
JTextField textField1 = new JTextField("hello");
JTextField textField2 = new JTextField("world");
container.add(textField1,BorderLayout.NORTH);
container.add(textField2,BorderLayout.SOUTH);
setVisible(true);
setBounds(100,100,500,500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
- 密码框 JPasswordField
package com.Su.lesson06;
import javax.swing.*;
import java.awt.*;
public class TestTextDemo02 extends JFrame {
public static void main(String[] args) {
new TestTextDemo02();
}
public TestTextDemo02(){
Container container = getContentPane();
JPasswordField passwordField = new JPasswordField();
passwordField.setEchoChar('*');
container.add(passwordField);
setVisible(true);
setBounds(100,100,500,500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
- 文本域 JTextArea
package com.Su.lesson05;
import javax.swing.*;
import java.awt.*;
public class JScrollDemo extends JFrame {
public JScrollDemo(){
Container container = getContentPane();
//文本域 可以换行
JTextArea textArea = new JTextArea(20, 50);
textArea.setText("欢迎学医java");
//Scroll面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
setVisible(true);
setBounds(100,100,300,500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo();
}
}
container.add(textField2,BorderLayout.SOUTH);
setVisible(true);
setBounds(100,100,500,500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
- 密码框 JPasswordField
package com.Su.lesson06;
import javax.swing.*;
import java.awt.*;
public class TestTextDemo02 extends JFrame {
public static void main(String[] args) {
new TestTextDemo02();
}
public TestTextDemo02(){
Container container = getContentPane();
JPasswordField passwordField = new JPasswordField();
passwordField.setEchoChar('*');
container.add(passwordField);
setVisible(true);
setBounds(100,100,500,500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
- 文本域 JTextArea
package com.Su.lesson05;
import javax.swing.*;
import java.awt.*;
public class JScrollDemo extends JFrame {
public JScrollDemo(){
Container container = getContentPane();
//文本域 可以换行
JTextArea textArea = new JTextArea(20, 50);
textArea.setText("欢迎学医java");
//Scroll面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
setVisible(true);
setBounds(100,100,300,500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo();
}
}