文章目录
GUI编程
1、简介
Gui核心技术:Swing,AWT
学习Gui的用处:了解MVC架构,了解监听器!
2、AWT
2.1、AWT介绍
1. 包含很多类和接口!GUI:图形用户界面
2. 元素:窗口,按钮,文本框
3. java.awt
2.2、组件和容器
1、frame
package com.sgl.lession01;
import java.awt.*;
//GUI的第一个界面
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(170, 31, 31));
//frame.setBackground(Color.BLACK);
//弹出的初始位置
frame.setLocation(300,300);
//设置大小固定
frame.setResizable(false);
}
}
运行效果:
创建多个窗口(封装)
package com.sgl.lession01;
import java.awt.*;
public class TestFrame2 {
public static void main(String[] args) {
//创建多个窗口
MyFrame myFrame = new MyFrame(100,100,200,200,Color.blue);
MyFrame myFrame1 = new MyFrame(300,100,200,200,Color.CYAN);
MyFrame myFrame2 = new MyFrame(100,300,200,200,Color.GREEN);
MyFrame myFrame3 = new MyFrame(300,300,200,200,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));
setBackground(color);//窗口大小和起始位置
setBounds(x,y,w,h);
setVisible(true);
}
}
运行结果:
2、Panel
顺便解决了关闭事件
package com.sgl.lession01;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestPannel {
public static void main(String[] args) {
Frame frame = new Frame("hello Panel");
//布局的概念
Panel panel = new Panel();
//设置布局
frame.setLayout(null);
//坐标
frame.setBounds(300,300,500,500);
frame.setBackground(new Color(23, 73, 199));
//Panel设置坐标,相对于Frame
panel.setBounds(50,50,400,400);
panel.setBackground(new Color(21, 153, 28));
//添加面板
frame.add(panel);
//设置可见性
frame.setVisible(true);
//监听事件,监听窗口关闭事件 System.exit(0)
//适配器模式:
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
运行结果:
2.3、布局管理器
- 流式布局FlowLayout
package com.sgl.lession01;
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(FlowLayout.CENTER));//默认居中
//frame.setLayout(new FlowLayout(FlowLayout.LEFT));
frame.setSize(400,400);
//把按钮添加
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
}
}
- 东西南北中BorderLayout
package com.sgl.lession01;
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(400,400);
frame.setVisible(true);
}
}
- 表格布局GridLayout
package com.sgl.lession01;
import java.awt.*;
public class TestGridLayout {
public static void main(String[] args) {
Frame frame = new Frame("GridLayout");
Button bt1 = new Button("bt1");
Button bt2 = new Button("bt2");
Button bt3 = new Button("bt3");
Button bt4 = new Button("bt4");
Button bt5 = new Button("bt5");
Button bt6 = new Button("bt6");
frame.setLayout(new GridLayout(3,2));
frame.add(bt1);
frame.add(bt2);
frame.add(bt3);
frame.add(bt4);
frame.add(bt5);
frame.add(bt6);
//Java函数! 自动选择最优的布局
frame.pack();
frame.setVisible(true);
}
}
运行结果:
案例:
package com.sgl.lession01;
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();
frame.setSize(400,400);
frame.setLocation(300,300);
frame.setBackground(new Color(0xAC1451));
frame.setVisible(true);
frame.setLayout(new GridLayout(2,1));
//4个面板
Panel p1 = new Panel(new BorderLayout());
Panel p2 = new Panel(new GridLayout(2,1));
Panel p3 = new Panel(new BorderLayout());
Panel p4 = new Panel(new GridLayout(2,2));
//上面布局搞定
p1.add(new Button("East-1"),BorderLayout.EAST);
p1.add(new Button("West-1"),BorderLayout.WEST);
p2.add(new Button("p2-1"));
p2.add(new Button("p2-2"));
p1.add(p2,BorderLayout.CENTER);
//下面布局
p3.add(new Button("East-2"),BorderLayout.EAST);
p3.add(new Button("West-2"),BorderLayout.WEST);
//下面中间4个
for (int i = 0; i < 4; i++) {
p4.add(new Button("p4-"+i));
}
p3.add(p4,BorderLayout.CENTER);
frame.add(p1);
frame.add(p3);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
总结:
- Frame是一个顶级窗口
- Panel无法单独显示,必须添加到某个容器中
- 布局管理器:流式布局管理器FlowLayout,东西南北中布局BorderLayout,表格布局GridLayout
- 大小,定位,背景颜色,可见性,监听!
2.4、事件监听
package com.sgl.lession01.lession2;
import java.awt.*;
import java.awt.event.*;
public class Test {
public static void main(String[] args) {
//按下按钮,触发一些事件
Frame frame = new Frame();
Button button = new Button("btn1");
//因为addActionListener()需要一个ActionListener,所以需要构造一个ActionListener
MyActionListener myActionListener = new MyActionListener();
button.addActionListener(myActionListener);
frame.add(button, BorderLayout.CENTER);
frame.pack();
windowClose(frame);//调用方法windowClose()
frame.setVisible(true);
}
private static void windowClose(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("Action");
}
}
多个按钮共享一个事件
package com.sgl.lession01.lession2;
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 Test {
public static void main(String[] args) {
//两个按钮 实现同一个监听
//开始 停止
Frame frame = new Frame();
Button button1 = new Button("start");
Button button2 = new Button("stop");
//可以显示的定义触发会返回的命令,如果不显示定义,则会走默认值
//可以多个按钮只写一个监听类
button2.setActionCommand("button2-stop");
MyActionListenner myActionListenner = new MyActionListenner();
button1.addActionListener(myActionListenner);
button2.addActionListener(myActionListenner);
frame.add(button1,BorderLayout.WEST);
frame.add(button2,BorderLayout.EAST);
windowClose(frame);
frame.pack();
frame.setVisible(true);
}
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyActionListenner implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//e.getActionCommand() 获取按钮的信息
System.out.println("按钮被点击了:msg-->"+e.getActionCommand());
}
}
2.5、输入框TextField
package com.sgl.lession01.lession2;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 Test { public static void main(String[] args) { MyFrame myFrame = new MyFrame(); windowClose(myFrame); } private static void windowClose(MyFrame myFrame) { myFrame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); }}class MyFrame extends Frame { public MyFrame(){ TextField textField = new TextField(); add(textField); //监听这个文本框输入的文字 MyActionLintener myActionLintener = new MyActionLintener(); //按下Enter,就会触发输入框的事件 textField.addActionListener(myActionLintener); //设置替换编码 textField.setEchoChar('*'); pack(); setVisible(true); }}class MyActionLintener implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { TextField field = (TextField) e.getSource();//获得一些资源,返回一个对象 System.out.println(field.getText());//获得输入框的文本 field.setText("");//按下Enter,清空 }}
2.6、简易计算器,组合+内部类回顾
package com.sgl.lession01.lession2;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;//简易计算机public class Test { 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(15); //1个按钮 Button button = new Button("="); //MyActionListener myActionListener = new MyActionListener(); button.addActionListener(new MyActionListener(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 MyActionListener implements ActionListener{ //获取3个变量 private TextField num1,num2,num3; public MyActionListener(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(""); }}
运行结果:
面向对象写法:
package com.sgl.lession01.lession2;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Test {
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(15);
//1个按钮
Button button = new Button("=");
//MyActionListener myActionListener = new MyActionListener();
button.addActionListener(new MyActionListener(this));
//1个标签
Label label = new Label("+");
//布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
}
//监听类
class MyActionListener implements ActionListener{
//获取计算机这个对象,在一个类中组合另一个类
Calculator calculator = null;
public MyActionListener(Calculator calculator){
this.calculator = calculator;
}
@Override
public void actionPerformed(ActionEvent e) {
//1.获得加数和被加数
//2.将这个值 + 运算后,放到第三个框
//3.清除前两个框
int n1 = Integer.parseInt(calculator.num1.getText());
int n2 = Integer.parseInt(calculator.num2.getText());
calculator.num3.setText(""+(n1+n2));
calculator.num1.setText("");
calculator.num2.setText("");
}
}
内部类:
package com.sgl.lession01.lession2;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class Test { 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(15); Button button = new Button("="); //MyActionListener myActionListener = new MyActionListener(); button.addActionListener(new MyActionListener()); Label label = new Label("+"); setLayout(new FlowLayout()); add(num1); add(label); add(num2); add(button); add(num3); pack(); setVisible(true); } //内部类可以直接访问外部类的属性和方法 private class MyActionListener implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { int n1 = Integer.parseInt(num1.getText()); int n2 = Integer.parseInt(num2.getText()); num3.setText(""+(n1+n2)); num1.setText(""); num2.setText(""); } }}
2.7、画笔
package com.sgl.lession01.lession2;import java.awt.*;public class Test { public static void main(String[] args) { new MyPaint().loadFrame(); }}class MyPaint extends Frame{ public void loadFrame(){ setBounds(200,200,600,600); setVisible(true); } //画笔 @Override public void paint(Graphics g) { //画笔颜色 //g.setColor(Color.BLUE); g.drawOval(100,100,50,50); g.fillOval(200,200,50,50); //g.setColor(Color.black); g.fillRect(300,300,50,60); }}
2.8、鼠标监听
package com.sgl.lession01.lession2;import java.awt.*;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.util.ArrayList;import java.util.Iterator;//鼠标监听事件public class Test { public static void main(String[] args) { new MyFrame("画图"); }}class MyFrame extends Frame{ //监听鼠标的位置,用集合存储这个点 ArrayList<Object> points; public MyFrame(String title) { super(title); setBounds(200,200,400,400); setVisible(true); //存鼠标点击的点 points = new ArrayList<>(); //鼠标监听事件,针对这个窗口 this.addMouseListener(new MyMouseListener()); } @Override public void paint(Graphics g) { //画画,监听鼠标的事件 Iterator<Object> 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.sgl.lession01.lession2;import java.awt.*;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;public class Test { public static void main(String[] args) { new WindowFrame(); }}class WindowFrame extends Frame { public WindowFrame() { setBackground(Color.CYAN); setBounds(100,100,200,200); setVisible(true); //addWindowListener(new MyWindowListener()); this.addWindowListener( //匿名内部类 new WindowAdapter() { //关闭窗口 @Override public void windowClosing(WindowEvent e) { System.out.println("windowClosing"); } //激活窗口 @Override public void windowActivated(WindowEvent e) { WindowFrame source = (WindowFrame) e.getSource(); source.setTitle("被激活了"); System.out.println("windowActivated"); } } ); } /* class MyWindowListener extends WindowAdapter { @Override public void windowClosing(WindowEvent e) { setVisible(false);//隐藏窗口 System.exit(0);//正常退出 } } */}
2.10、键盘监听
package com.sgl.lession01.lession2;import java.awt.*;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;public class Test { public static void main(String[] args) { new KeyFrame(); }}class KeyFrame extends Frame { public KeyFrame() { setBounds(1, 2, 400, 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.sgl.lession01.lession2;
import javax.swing.*;
import java.awt.*;
public class JFrameDemo {
//init(); 初始化
public void init(){
JFrame jFrame = new JFrame();
jFrame.setVisible(true);
jFrame.setBounds(100,100,200,200);
jFrame.setBackground(Color.CYAN);
//获得一个容器
Container container = jFrame.getContentPane();
container.setBackground(Color.cyan);
//关闭事件
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//设置文字
JLabel jLabel = new JLabel("欢迎来到HeNan");
jFrame.add(jLabel);
//让文本标签居中 设置水平对其
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
}
public static void main(String[] args) {
//建立一个窗口
new JFrameDemo().init();
}
}
3.2、弹窗
Jdialog,用来被弹出,默认有关闭事件
package com.sgl.lession01.lession2;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class JFrameDemo extends JFrame{
public JFrameDemo(){
this.setVisible(true);
this.setSize(700,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//JFrame 放东西 容器
Container container = this.getContentPane();
//绝对布局
container.setLayout(null);
//按钮
JButton button = new JButton("点击弹出对话窗");
button.setBounds(30,30,200,200);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//弹窗
new MyDialogDemo();
}
});
container.add(button);
}
public static void main(String[] args) {
new JFrameDemo();
}
}
//弹窗的窗口
class MyDialogDemo extends JDialog{
public MyDialogDemo() {
this.setBounds(100,50,500,500);
this.setVisible(true);
this.setSize(200,200);
//this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 系统自带,加上系统会提醒
Container container = this.getContentPane();
container.setBackground(Color.BLUE);
container.setLayout(null);
JLabel jLabel = new JLabel("跟着秦老师学Java");
container.add(jLabel);
}
}
3.3、标签
label
new Jlable("xxx");
图标ICON
package com.sgl.lession01.lession2;import javax.swing.*;import java.awt.*;//图标。需要实现类,JFrame继承public class IconDemo extends JFrame implements Icon{ private int width; private int height; public IconDemo(){}//无参构造 public IconDemo(int width,int height){ this.height = height; this.width = width; } public void init(){ IconDemo iconDemo = new IconDemo(50,50); //图标放在标签上,也可以放在按钮上 JLabel jLabel = new JLabel("icontext", iconDemo, SwingConstants.CENTER); Container container = this.getContentPane(); container.add(jLabel); 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; } public static void main(String[] args) { IconDemo iconDemo = new IconDemo(); iconDemo.setBounds(200,200,500,500); iconDemo.setVisible(true); iconDemo.init(); //iconDemo.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); }}
图片ICON
package com.sgl.lession01.lession2;import javax.swing.*;import java.awt.*;import java.net.URL;public class ImageIconDemo extends JFrame { public ImageIconDemo(){ //获取图片地址 JLabel label = new JLabel("ImageIcon",SwingConstants.CENTER); URL url = ImageIconDemo.class.getResource("985.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 ImageIconDemo(); }}
3.4、面板
JPanel
package com.sgl.lession01.lession2;import javax.swing.*;import java.awt.*;public class JPanelDemo extends JFrame { public JPanelDemo(){ Container container = this.getContentPane(); container.setLayout(new GridLayout(2,1,10,10));//gap间隙 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 Button("1")); jPanel1.add(new Button("1")); jPanel1.add(new Button("1")); jPanel2.add(new Button("2")); jPanel2.add(new Button("2")); jPanel3.add(new Button("3")); jPanel3.add(new Button("3")); jPanel4.add(new Button("4")); jPanel4.add(new Button("4")); jPanel4.add(new Button("4")); jPanel4.add(new Button("4")); jPanel4.add(new Button("4")); jPanel4.add(new Button("4")); container.add(jPanel1); container.add(jPanel2); container.add(jPanel3); container.add(jPanel4); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); this.setVisible(true); this.setSize(500,500); } public static void main(String[] args) { new JPanelDemo(); }}
JscrollPanel
package com.sgl.lession01.lession2;import javax.swing.*;import java.awt.*;public class JScrollDemo extends JFrame { public JScrollDemo(){ Container container = getContentPane(); //文本域 JTextArea textArea = new JTextArea(20,50); textArea.setText("欢迎欢迎"); //面板 JScrollPane scrollPane = new JScrollPane(textArea); container.add(scrollPane); this.setVisible(true); this.setBounds(100,100,300,500); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new JScrollDemo(); }}
3.5、按钮
- 图片按钮
package com.sgl.lession01.lession2;import javax.swing.*;import java.awt.*;import java.net.URL;public class JButtonDemo extends JFrame { public JButtonDemo(){ Container container = this.getContentPane(); URL resource = JButtonDemo.class.getResource("985.jpg"); Icon icon = new ImageIcon(resource); //把这个图标放在按钮上 JButton button = new JButton(); button.setIcon(icon); button.setToolTipText("图片按钮"); container.add(button); this.setVisible(true); this.setSize(500,300); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new JButtonDemo(); }}
- 单选按钮
package com.sgl.lession01.lession2;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo extends JFrame {
public JButtonDemo(){
Container container = this.getContentPane();
URL resource = JButtonDemo.class.getResource("985.jpg");
Icon icon = new ImageIcon(resource);
//单选框
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);
container.add(jRadioButton1,BorderLayout.CENTER);
container.add(jRadioButton2,BorderLayout.NORTH);
container.add(jRadioButton3,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(500,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo();
}
}
- 复选按钮
package com.sgl.lession01.lession2;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo extends JFrame {
public JButtonDemo(){
Container container = this.getContentPane();
URL resource = JButtonDemo.class.getResource("985.jpg");
Icon icon = new ImageIcon(resource);
//多选框
JCheckBox jCheckBox01 = new JCheckBox("jCheckBox01");
JCheckBox jCheckBox02 = new JCheckBox("jCheckBox02");
container.add(jCheckBox01,BorderLayout.NORTH);
container.add(jCheckBox02,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(500,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo();
}
}
3.6、列表
- 下拉框
package com.sgl.lession01.lession2;import javax.swing.*;import java.awt.*;public class TextComboboxDemo01 extends JFrame { public TextComboboxDemo01(){ Container container = this.getContentPane(); JComboBox<Object> jComboBox = new JComboBox<>(); jComboBox.addItem(null); jComboBox.addItem("正在热映"); jComboBox.addItem("已下架"); jComboBox.addItem("即将上映"); container.add(jComboBox); this.setVisible(true); this.setSize(500,500); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new TextComboboxDemo01(); }}
- 列表框
package com.sgl.lession01.lession2;import javax.swing.*;import java.awt.*;import java.util.Vector;public class TextComboboxDemo01 extends JFrame { public TextComboboxDemo01(){ Container container = this.getContentPane(); //生成列表内容 //String[] contents = {"1","2","3"}; Vector vector = new Vector(); //列表需要放入的内容 //JList jList = new JList(contents); JList jList = new JList(vector); vector.add("zhangshan"); vector.add("lisi"); vector.add("wangwu"); container.add(jList); this.setVisible(true); this.setSize(500,500); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new TextComboboxDemo01(); }}
3.7、文本框
- 文本框
package com.sgl.lession01.lession2;import javax.swing.*;import java.awt.*;public class TextTextDemo01 extends JFrame { public TextTextDemo01(){ Container container = this.getContentPane(); JTextField textField = new JTextField("Hello"); JTextField textField2 = new JTextField("World",20); container.add(textField,BorderLayout.NORTH); container.add(textField2,BorderLayout.SOUTH); this.setVisible(true); this.setSize(500,300); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new TextTextDemo01(); }}
- 密码框
package com.sgl.lession01.lession2;import javax.swing.*;import java.awt.*;public class TextTextDemo01 extends JFrame { public TextTextDemo01(){ 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); } public static void main(String[] args) { new TextTextDemo01(); }}
- 文本域
package com.sgl.lession01.lession2;import javax.swing.*;import java.awt.*;public class JScrollDemo extends JFrame { public JScrollDemo(){ Container container = getContentPane(); //文本域 JTextArea textArea = new JTextArea(20,50); textArea.setText("欢迎欢迎"); //面板 JScrollPane scrollPane = new JScrollPane(textArea); container.add(scrollPane); this.setVisible(true); this.setBounds(100,100,300,500); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new JScrollDemo(); }}