文章目录
Gui
GUI核心技术:Swing AWT
不流行的原因:
- 界面不美观
- 需要jre环境!
为什么我们学习?
- 可以写出自己心中想要的一些小工具
- 工作时也可能需要维护到swing界面
- 了解MVC架构,了解监听
AWT
AWT(抽象窗口)介绍:
- 包含了很多类和接口,
- 元素:窗口、按钮、文本框
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-IJ7HoX3I-1635068187041)(D:\学习截图\屏幕截图 2021-07-16 163748.png)]
组件和容器
frame
package com.jiateng.study;
import java.awt.*;
//gui的第一个窗口
public class TestFrame {
public static void main(String[] args) {
Frame frame = new Frame("我的第一个java图形界面窗口");//ctrl+左键alt+7
//需要设置可见性
frame.setVisible(true);
//大小
frame.setSize(444,444);
//背景颜色
frame.setBackground(new Color(40, 40, 108));//颜色这里三个数字
//初始位置
frame.setLocation(200,200);
//大小固定
frame.setResizable(false);
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cQeMRujU-1635068187052)(D:\学习截图\窗口.png)]
问题:窗口不可以被关掉!解决办法停止程序!
多窗口
package com.jiateng.study;
import java.awt.*;
public class MyFrame{
public static void main(String[] args) {
MyFrame1 myFrame1 = new MyFrame1(100, 100, 200, 200, Color.cyan);
MyFrame1 myFrame2 = new MyFrame1(300, 100, 200, 200, Color.cyan);
MyFrame1 myFrame3 = new MyFrame1(100, 300, 200, 200, Color.yellow);
MyFrame1 myFrame4= new MyFrame1(300, 300, 200, 200, Color.GREEN);
}
}
//实现多窗口
class MyFrame1 extends Frame {
//数量
static int id=0;
public MyFrame1(int x,int y,int w,int h,Color color){
super("MyFrame"+(++id));//调用父类构造器
setBounds(x,y,w,h);//初始位置和大小
setVisible(true);//可视
setBackground(color);//背景颜色
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-56XuEweD-1635068187055)(D:\学习截图\多窗口.png)]
Panel
package com.jiateng.study;
import java.awt.*;
public class TestPanle {
//Panel可以看成一个空间但是不能独立存在
public static void main(String[] args) {
Frame frame = new Frame();
Panel panel = new Panel();
//设置布局
frame.setLayout(null);
//坐标
frame.setBounds(200,200,600,600);
//颜色
frame.setBackground(new Color(1,2,3));
//Panel坐标,颜色设置(相对于Frame)
panel.setBounds(100,100,60,60);
panel.setBackground(new Color(232, 236, 240));
//frame.add(panel)
frame.add(panel);
frame.setVisible(true);
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DmhhB6e9-1635068187059)(D:\学习截图\panel.png)]
如何关闭窗口
package com.jiateng.study;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//多窗口
public class Frame04 {
public Frame04(int x,int y,int w,int h,Color color) {
int id=0;
id++;
Frame frame = new Frame("第"+id+"个窗口");
//可视化
frame.setVisible(true);
// 坐标
frame.setBounds(x,y,w,h);
// 背景
frame.setBackground(color);
// 固定
frame.setResizable(false);
//适配器模式:
//监听事件 退出System.exit()
/*这里本来是要new一个WindowListener(接口),但是方法太多,
我们可以用它的子类WindowAdapter(抽象类)
抽象的子类可以选择性实现其基类的抽象方法 接口的子类必须全部实现
*/
frame.addWindowListener(new WindowAdapter() {
@Override//重写方法
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class win{
public static void main(String[] args) {
Frame04 frame04 = new Frame04(100, 100, 400, 400, Color.yellow);
Frame04 frame03 = new Frame04(400, 100, 400, 400, Color.yellow);
Frame04 frame02 = new Frame04(100, 400, 400, 400, Color.yellow);
Frame04 frame62= new Frame04(400,400, 400, 400, Color.yellow);
}
}
布局管理器
-
流式布局FlowLayout
package com.jiateng.study; import java.awt.*; //流式布局 public class Test01 { public static void main(String[] args) { Frame frame = new Frame(); //组件-按钮 Button button1 = new Button("1"); Button button2 = new Button("2"); Button button3 = new Button("a中文3"); frame.setBounds(100,100,200,200); frame.setVisible(true); //流式布局 frame.setLayout(new FlowLayout(0)); //把按钮添加上去 frame.add(button1); frame.add(button2); frame.add(button3); } }
-
东西南北中BorderLayout
package com.jiateng.study; import java.awt.*; //东西南北中 public class Test02 { public static void main(String[] args) { Frame frame = new Frame("东西南北中"); frame.setVisible(true); frame.setSize(300,300); //按钮 Button east = new Button("东"); Button west = new Button("西"); Button south = new Button("南"); Button north = new Button("北"); Button center = new Button("中"); //布局&&添加按钮 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); } }
-
表格布局GridLayout
package com.jiateng.study;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//表格布局
public class TestGrid01 {
public static void main(String[] args) {
Frame frame = new Frame("东西南北中");
//按钮
Button east = new Button("东");
Button west = new Button("西");
Button south = new Button("南");
Button north = new Button("北");
Button center = new Button("中");
Button cente = new Button("中");
//表格布局
frame.setLayout(new GridLayout(3,2));//三行两列
//添加按钮
frame.add(east);
frame.add(west);
frame.add(south);
frame.add(north);
frame.add(center);
frame.add(cente);
frame.pack();//自动布局
frame.setVisible(true);
//关闭
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
设计面板
package com.jiateng.study;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestExe {
public static void main(String[] args) {
Frame frame = new Frame();
//4个面板
//Panel可以这样布局,但是Frame不可以(源码点进去是名字的命名)
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("buotton00"),BorderLayout.WEST);//第一个按钮放在第一个面板的西面
panel1.add(new Button("buotton01"),BorderLayout.EAST);//第二个按钮放在第一个面板的东面
panel2.add(new Button("buo1"));
panel2.add(new Button("buo2"));
//把面板2放进1里
panel1.add(panel2,BorderLayout.CENTER);
//下半部分同上
panel3.add(new Button("buotton10"),BorderLayout.WEST);
panel3.add(new Button("buotton11"),BorderLayout.EAST);
panel4.add(new Button("buotton center1"));
panel4.add(new Button("buotton center2"));
panel4.add(new Button("buotton center3"));
panel4.add(new Button("buotton center4"));
//把面板4放进3里
panel3.add(panel4);
//给窗口添加面板
frame.setLayout(new GridLayout(2,1));
frame.add(panel1);
frame.add(panel3);
//窗口
frame.setVisible(true);
frame.setBounds(100,100,600,600);
//适配器模式:
//监听事件 退出System.exit()
/*这里本来是要new一个WindowListener(接口),但是继承接口需要实现它的全部方法,
我们可以用它的子类WindowAdapter(抽象类)
抽象的子类可以选择性实现其基类的抽象方法 接口的子类必须全部实现
*/
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dFLwswwF-1635068187061)(D:\学习截图\设计面板.png)]
总结:
- Frame是一个顶级窗口
- Panel是面板,无法单独显示,必须添加到某个元素中
- 布局管理器
- 流式FowlLayout
- 东西南北中BorderLayout添加按钮时直接用也可
- 表格Gridlayout
- 大小,坐标,颜色背景,可见性,(监听)退出。
事件监听
package com.jiateng.study;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 List { public static void main(String[] args) { //按下按钮会触发一些事件 Frame frame = new Frame(); Button button = new Button(); //因为addActionListener()需要一个ActionListener,所以我们创建一个MyActionListener MyActionListener myActionListener = new MyActionListener(); button.addActionListener(myActionListener); //按钮放进窗口中 frame.pack(); frame.add(button,BorderLayout.CENTER); frame.setVisible(true); //关闭 windowClose(frame); } //关闭窗口的事件 public 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("jaiteng "); }}
事件监听两个按钮实现一个监听类
package com.jiateng.study;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;/** * 两个按钮实现一个监听 * 可以在监听类里面进行判断 * if(e.getActionCommand==??) getActionCommand可以获得按钮的一些信息,当然按钮也可以用set进行设置 * * @author jiateng */public class Text04 { public static void main(String[] args) { Frame frame = new Frame(); Button button1 = new Button("我是按钮1"); Button button2 = new Button("我是按钮2"); //窗口设置 frame.setVisible(true); frame.pack(); button1.setActionCommand("我被修改了"); button2.setActionCommand("woyebeixiugaile"); //窗口监听 frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0);//退出jvm,如果有多个窗口会一起退出 } }); //按钮监听 button1.addActionListener(new MyActionListeners()); button2.addActionListener(new MyActionListeners()); //添加按钮 frame.setLayout(new GridLayout(1,2));//表格布局 frame.add(button1); frame.add(button2); }}class MyActionListeners implements ActionListener { @Override public void actionPerformed(ActionEvent e) {//通过if来实现多个按钮共用一个监听类 if(e.getActionCommand().equals("我被修改了")){ System.out.println(e.getActionCommand()); } if (e.getActionCommand().equals("woyebeixiugaile")){ System.out.println(e.getActionCommand()); } }}
文本框TextField
1.面向过程
package com.jiateng.study;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class TestCalc {//简易计算器 public static void main(String[] args) { Calculator calculator = new Calculator(); }}//计算器类class Calculator extends Frame{ public Calculator(){ //窗口 Frame frame = new Frame(); //三个文本框 TextField field = new TextField(10); TextField field2 = new TextField(10); TextField field3 = new TextField(20); //一个按钮 Button button = new Button("="); //把参数传过去 button.addActionListener(new MyCalculatorlist(field,field2,field3)); //一个标签 Label label = new Label("+"); setVisible(true); pack(); //排序 setLayout(new FlowLayout()); add(field); add(label); add(field2); add(button); add(field3); }}//监听类class MyCalculatorlist implements ActionListener{ private TextField num1,num2,num3; //用构造器把参数传过来 public MyCalculatorlist(TextField field1, TextField field2, TextField field3){ this.num1=field1; this.num2=field2; this.num3=field3; } @Override public void actionPerformed(ActionEvent e) { //获得加数和被加数 //String变成了int型 int n1=Integer.parseInt(num1.getText()); int n2=Integer.parseInt(num2.getText()); //将两数相加后给第三个框 num3.setText(""+(n1+n2)); //清空前两个框 num1.setText(""); num2.setText(""); }}
2.面向对象
package com.jiateng.study;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class TestCalc {//简易计算器 public static void main(String[] args) { Calculator calculator = new Calculator(); calculator.lodFrame(); new WindowClose(calculator); }}//计算器类class Calculator extends Frame{ //属性 TextField field,field2, field3; //方法 public void lodFrame(){ Frame frame = new Frame(); //三个文本框 field = new TextField(10); field2 = new TextField(10); field3 = new TextField(20); //一个按钮 Button button = new Button("="); //把他自己传过去 button.addActionListener(new MyCalculatorlist(this)); //一个标签 Label label = new Label("+"); setVisible(true); pack(); //排序 setLayout(new FlowLayout()); add(field); add(label); add(field2); add(button); add(field3); }}//监听类class MyCalculatorlist implements ActionListener{ //获取计算器这个类 //组合思想,之前太low;更加面向对象 private Calculator calculato=null; public MyCalculatorlist(Calculator calculato){ this.calculato=calculato; } @Override public void actionPerformed(ActionEvent e) { int n1=Integer.parseInt(calculato.field.getText()); int n2=Integer.parseInt(calculato.field2.getText()); //加双引号是为了变成String型 calculato.field3.setText(""+(n1+n2)); calculato.field.setText(""); calculato.field2.setText(""); }}
3.内部类
package com.jiateng.study;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class TestCalc {//简易计算器 public static void main(String[] args) { Calculator calculator = new Calculator(); calculator.lodFrame(); new WindowClose(calculator); }}//计算器类class Calculator extends Frame{ //属性 TextField field,field2, field3; //方法 public void lodFrame(){ Frame frame = new Frame(); //三个文本框 field = new TextField(10); field2 = new TextField(10); field3 = new TextField(20); //一个按钮 Button button = new Button("="); button.addActionListener(new MyCalculatorlist()); //一个标签 Label label = new Label("+"); setVisible(true); pack(); //排序 setLayout(new FlowLayout()); add(field); add(label); add(field2); add(button); add(field3); } //监听类 //内部类最大的好处就是可以畅通无阻的访问外部类 class MyCalculatorlist implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { //1.获得两个加数 //2.把两个数相加放到第三个框里 //3.清空前两个框 int n1=Integer.parseInt(field.getText()); int n2=Integer.parseInt(field2.getText()); //加双引号是为了变成String型 field3.setText(""+(n1+n2)); field.setText(""); field2.setText(""); } }}
画笔Paint
package com.jiateng.study;import java.awt.*;public class TestPaint01 { public static void main(String[] args) { new MyPaint().MyFrame03(); }}class MyPaint extends Frame { public void MyFrame03(){ setBounds(100,100,600,600); setVisible(true); } //画笔 @Override public void paint(Graphics g) { //颜色,可以画画 g.setColor(Color.black); //g.drawOval(100,100,100,100); g.fillOval(100,100,100,100); }}
制作绘图本(鼠标监听)
package com.jiateng.study;import java.awt.*;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.util.ArrayList;import java.util.Iterator;/** * 鼠标监听事件 * * @author jiateng */public class Demo07 { public static void main(String[] args) { MyFrame03 paint = new MyFrame03("图画本"); }}class MyFrame03 extends Frame{ //存放点的集合 ArrayList<Point> points; public MyFrame03(String title){ super(title); setBounds(100,100,300,300); setVisible(true); //存放点 points=new ArrayList(); //窗口监听鼠标 this.addMouseListener(new MyMouseListener()); } //画画 @Override public void paint(Graphics g) { //利用迭代器把集合里面的点遍历出来 Iterator iterator = points.iterator(); while(iterator.hasNext()){ Point next = (Point)iterator.next();//集合里面的元素类型是Point类型 g.setColor(Color.cyan); g.fillOval(next.x,next.y,5,5); } } //不使用内部类情况的代码// public void addpoint(Point point){// points.add(point); } //监听类 适配器模式 private class MyMouseListener extends MouseAdapter{ //鼠标 按压 弹起 按住不放 @Override public void mousePressed(MouseEvent e) { //点击的时候会出现一个点 MyFrame03 source =(MyFrame03)e.getSource(); //把点击时的鼠标坐标放入集合 points.add(new Point(e.getX(),e.getY())); //重画 source.repaint();以上代码为内部类编写代码// source.addpoint(new Point(e.getX(),e.getY()));// //把点传输到自己的方法中// source.repaint(); } }}
窗口监听
package com.jiateng.study;import java.awt.*;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;/** * 窗口的监听事件 * * @author jiateng */public class Demo08 { public static void main(String[] args) { new MyFrame05(); }}class MyFrame05 extends Frame{ public MyFrame05() { setBounds(100,100,300,300); setVisible(true); //窗口的监听 addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.out.println("窗口关闭了"); System.exit(0); } @Override public void windowActivated(WindowEvent e) { MyFrame05 myFrame05 = (MyFrame05) e.getSource(); myFrame05.setTitle("窗口被激活了"); } }); }}
键盘监听
package com.jiateng.study;import java.awt.*;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;/** * 键盘监听 * * @author jiateng */public class Demo09 { public static void main(String[] args) { new MyKey(); }}class MyKey extends Frame{ public MyKey() { setBounds(10,20,300,300); setVisible(true); //键盘监听 addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { //键值的ASCLL Integer keyCode = e.getKeyCode(); System.out.println(keyCode); if(keyCode==KeyEvent.VK_UP){ System.out.println("你按下了上键"); } } }); }}
Swing
JFrame
组件添加在容器中
获得容器:Container contentPane = jFrame.getContentPane();
package com.jiateng.study;import javax.swing.*;import java.awt.*;/** * JFrame窗体 * * @author jiateng */public class Demo10 { public static void main(String[] args) { new MyJframe01().init(); }}class MyJframe01 { //初始化 public void init(){ JFrame jFrame = new JFrame("Jframe窗口"); jFrame.setBounds(10,10,200,200); jFrame.setVisible(true); //标签 JLabel jLabel = new JLabel("欢迎来到我的界面"); //局中 JLabel里面的方法 jLabel.setHorizontalAlignment(SwingConstants.CENTER); jFrame.add(jLabel); //设置背景 注意:需要先获得容器,设置容器的背景。 Container contentPane = jFrame.getContentPane(); contentPane.setBackground(Color.CYAN); //关闭 jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); }}
弹窗JDialog
package com.jiateng.study;import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;/** * JDialog * 弹窗 * * @author jiateng */public class Demo11 extends JFrame { public Demo11(){ //初始化 this.setBounds(10,10,300,300); this.setVisible(true); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //按钮 JButton jButton = new JButton("点击我出现一个弹窗"); //获得容器 添加颜色 添加按钮 Container contentPane = this.getContentPane(); contentPane.setBackground(Color.gray); contentPane.add(jButton); //绝对布局 contentPane.setLayout(null); jButton.setBounds(50,50,170,100); //监听 jButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //弹出弹窗 new MyJDialog(); } }); } public static void main(String[] args) { new Demo11(); }}//弹窗class MyJDialog extends JDialog{//JDialog继承了Dialog public MyJDialog() {//这个弹窗不需要设置关闭 this.setBounds(10,10,200,200); this.setVisible(true); //标签 JLabel jLabel = new JLabel("警告"); Container contentPane = this.getContentPane(); //绝对布局 contentPane.setLayout(null); contentPane.add(jLabel); jLabel.setBounds(10,10,100,100); }}
图标ICON与ImageIcon
package com.jiateng.study;import javax.swing.*;import java.awt.*;/** * Icon图标 添加到按钮上和标签 * * @author jiateng */public class Demo12 extends JFrame implements Icon { private int width; private int height; public Demo12() {//无参构造 方便实例化 } public Demo12(int height, int width) {//有参构造 this.height=height; this.width=width; } public void init(){ Demo12 Icon = new Demo12(15,15);//图标已经设置好,只差添加。 JLabel label = new JLabel("我是一个带图标的标签",Icon,SwingConstants.CENTER);//添加成功,并进行了布局 //在容器上添加标签 Container contentPane = getContentPane(); contentPane.add(label); this.setVisible(true);//注意这里的this this.setBounds(10,10,400,400); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//退出 } public static void main(String[] args) { new Demo12().init();//起动 } //重写Icon里面的方法 //画笔 @Override public void paintIcon(Component c, Graphics g, int x, int y) { g.fillOval(x,y,width,height);//x y坐标 为了重写这三个方法设置了width与height } //获得长 @Override public int getIconWidth() { return width; } //获得宽 @Override public int getIconHeight() { return height; }}
package com.jiateng.study;import javax.swing.*;import java.awt.*;import java.net.URL;/** * 将图片添加到标签上 * * @author jiateng */public class Demo13 extends JFrame { public void init(){ JLabel jLabel = new JLabel("我是一个带图片的标签"); //获得图片的地址 //下面代码为反射内容没学,可以用绝对路径表示 URL url = Demo13.class.getResource("12.jpg");//可以获得同级目录下的文件路径 //把图片的路径给图标类 ImageIcon imageIcon = new ImageIcon(url);//ImageIcon implements //可以用JLabel里面的set方法进行添加 jLabel.setIcon(imageIcon); jLabel.setHorizontalAlignment(SwingConstants.CENTER); //窗口 Container contentPane = getContentPane(); contentPane.add(jLabel); //窗口三件套 this.setVisible(true); this.setBounds(100,100,300,300); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new Demo13().init();//启动 }}
面板JScrollPane 文本域JTextArea
package com.jiateng.study;import javax.swing.*;import java.awt.*;/** * Jpanel * JTextArea文本域 * JScrollPane//面板 滚动条 * @author jaiteng */public class Demo14 extends JFrame { public Demo14() { Container contentPane = this.getContentPane(); this.setVisible(true); this.setBounds(100,100,300,300); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //文本域 JTextArea jTextArea = new JTextArea(20,50); jTextArea.setText("学习java"); //面板 JScrollPane jScrollPane = new JScrollPane(jTextArea);//文本域添加在面板上 contentPane.add(jScrollPane);//文本域添加在容器上 } public static void main(String[] args) { new Demo14(); }}
图片按钮 单选框 多选框
package com.jiateng.study;import javax.swing.*;import java.awt.*;import java.net.URL;public class Demo15 extends JFrame { public Demo15(){ Container contentPane = this.getContentPane(); //将图片变成图标 URL resource = Demo15.class.getResource("12.jpg"); ImageIcon imageIcon = new ImageIcon(resource); // JButton jButton = new JButton(); jButton.setToolTipText("当鼠标选中时出现此内容"); jButton.setIcon(imageIcon); //添加按钮 contentPane.add(jButton); this.setSize(300,300); this.setVisible(true); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new Demo15(); }}
package com.jiateng.study;//单选框import javax.swing.*;import java.awt.*;import java.net.URL;public class Demo15 extends JFrame { public Demo15(){ Container contentPane = this.getContentPane(); //将图片变成图标 URL resource = Demo15.class.getResource("12.jpg"); ImageIcon imageIcon = new ImageIcon(resource); //单选框 JRadioButton jRadioButton1 = new JRadioButton("1"); JRadioButton jRadioButton2 = new JRadioButton("2"); JRadioButton jRadioButton3 = new JRadioButton("3"); //单选框只能选一个,分在一组 ButtonGroup buttonGroup = new ButtonGroup(); buttonGroup.add(jRadioButton1); buttonGroup.add(jRadioButton2); buttonGroup.add(jRadioButton3); //布局 contentPane.setLayout(new FlowLayout()); //添加按钮 contentPane.add(jRadioButton1); contentPane.add(jRadioButton2); contentPane.add(jRadioButton3); this.setSize(300,300); this.setVisible(true); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new Demo15(); }}
package com.jiateng.study;//多选框import javax.swing.*;import java.awt.*;import java.net.URL;public class Demo15 extends JFrame { public Demo15(){ Container contentPane = this.getContentPane(); //将图片变成图标 URL resource = Demo15.class.getResource("12.jpg"); ImageIcon imageIcon = new ImageIcon(resource); //多选框 JCheckBox jCheckBox1 = new JCheckBox("1"); JCheckBox jCheckBox2 = new JCheckBox("2"); //布局 contentPane.setLayout(new FlowLayout()); //添加按钮 contentPane.add(jCheckBox1); contentPane.add(jCheckBox2); this.setSize(300,300); this.setVisible(true); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new Demo15(); }}
下拉框 列表框
package com.jiateng.study;import javax.swing.*;import java.awt.*;import java.util.ArrayList;import java.util.Vector;/** * 列表框 * @author jiateng */public class Demo16 extends JFrame { public Demo16() { Container contentPane = this.getContentPane(); Vector<String> objects = new Vector<>(); //列表框 JList jList = new JList(objects);//ArrayList不行 objects.add("佳腾"); objects.add("佳腾2"); contentPane.add(jList); this.setVisible(true); this.setSize(300,300); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new Demo16(); }}
package com.jiateng.study;import javax.swing.*;import java.awt.*;import java.util.ArrayList;import java.util.Vector;/** * 下拉框 * @author jiateng */public class Demo16 extends JFrame { public Demo16() { Container contentPane = this.getContentPane(); //下拉框 可以实现监听 objectJComboBox.addActionListener(); JComboBox<String> objectJComboBox = new JComboBox<>(); objectJComboBox.addItem(null); objectJComboBox.addItem("yi"); objectJComboBox.addItem(null); contentPane.add(objectJComboBox); this.setVisible(true); this.setSize(300,300); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new Demo16(); }}
- 应用场景
- 下拉框 选择地区,或者一些单选项
- 列表 展示信息
密码框
package com.jiateng.study;import javax.swing.*;import java.awt.*;import java.util.ArrayList;import java.util.Vector;/** * 密码框 * @author jiateng */public class Demo16 extends JFrame { public Demo16() { Container contentPane = this.getContentPane(); //密码框 JPasswordField jPasswordField = new JPasswordField(); jPasswordField.setEchoChar('*'); //布局 contentPane.setLayout(new FlowLayout()); //添加 contentPane.add(jPasswordField); this.setVisible(true); this.setSize(300,300); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new Demo16(); }}