1、简介
GUI的核心技术:Swing AWT(这两个类)
为什么不被广泛应用?
1.因为界面不美观
2.需要jre环境!
为什么还要学习?
1.可以写出一些小工具
2.工作时候,也可能许哟啊维护到swing界面,概率极小!
2.了解MVC架构,了解监听!
2、AWT
2.1、Awt介绍
1.包含了很多类和接口 GUI:(图形用户界面编程)
2.元素:窗口、按钮、文本框
3.java.awt
2.2、组件和容器
(1).Frame
import java.awt.*;
//GUI的第一个界面
public class TestFrameDemo1 {
public static void main(String[] args) {
//窗口是frame对象
Frame frame = new Frame("新的一天开始了");
//窗口可视化
frame.setVisible(true);
//设置窗口大小
frame.setSize(400,400);
//设置窗口颜色
frame.setBackground(new Color(1, 253, 173));
//弹出初始位置
frame.setLocation(200,200);
//设置窗口大小固定
frame.setResizable(false);
}
}
运行结果
问题:发现窗口关闭不掉,停止java程序!
回顾封装
import java.awt.*;
//弹出多个界面
public class TestFrameDemo02 {
public static void main(String[] args) {
MyFrame myFrame1 = new MyFrame(100, 100, 200, 200, Color.black);
MyFrame myFrame2 = new MyFrame(300, 100, 200, 200, Color.blue);
MyFrame myFrame3 = new MyFrame(100, 300, 200, 200, Color.white);
MyFrame myFrame4 = 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("新的一天开始了");
setBounds(x,y,w,h);
setBackground(color);
setVisible(true);
}
}
(2).面板 Panel
解决了关闭事件
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//Panel 可以看成是一个空间,但是不能单独存在,是内嵌在Frame窗口中的
public class TestPanelDemo03 {
public static void main(String[] args) {
Frame frame = new Frame();
Panel panel = new Panel();
//设置布局
frame.setLayout(null);
//坐标和窗口背景颜色
frame.setBounds(300,300,300,300);
frame.setBackground(new Color(253, 149,1));
panel.setBounds(20,20,100,100);
panel.setBackground(new Color(253, 200, 1));
frame.add(panel);
frame.setVisible(true);
//监听事件,监听窗口关闭事件 System.exit(0)
//适配器模式
frame.addWindowListener(new WindowAdapter() {
//窗口点击关闭时做的事情
@Override
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
}
2.3布局管理器
(1)流式布局
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestFlowLayoutDemo04 {
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.setSize(300,300);
frame.setLocation(400,400);
//把按钮添加上去
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);
}
});
}
}
运行截图
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-800dFMVa-1614856570787)(C:\Users\帅气的影山瑞\AppData\Roaming\Typora\typora-user-images\image-20210301200604644.png)]
(2)东西南北中
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestBorderLayoutDemo05 {
public static void main(String[] args) {
Frame frame = new Frame();
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.setBounds(300,300,300,300);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
运行截图
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-b9WyjeyY-1614856570789)(C:\Users\帅气的影山瑞\AppData\Roaming\Typora\typora-user-images\image-20210301200635617.png)]
(3)表格布局 Gird
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();
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
Button button4 = new Button("button4");
Button button5 = new Button("button5");
Button button6 = new Button("button6");
frame.setLayout(new GridLayout(3,2));
frame.setBounds(400,400,400,400);
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.add(button5);
frame.add(button6);
frame.pack();//是一个Java函数,他会生成一个最优的布局 形式(有时候不一定需要使用
//有pack()可以不需要设置setBounds,它会自动生成
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
代码:
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Demo08TestPractice {
public static void main(String[] args) {
//总窗口
Frame frame = new Frame();
frame.setLayout(new GridLayout(2,1));
frame.setBounds(300,400,400,300);
frame.setBackground(new Color(1,30,3));
frame.setVisible(true);
frame.setLayout(new GridLayout(2,1));
//四个面板
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,1));
panel1.add(new Button("East-1"),BorderLayout.EAST);
panel1.add(new Button("West-1"),BorderLayout.WEST);
panel2.add(new Button("p2j-1"));
panel2.add(new Button("p2j-2"));
panel1.add(panel2,BorderLayout.CENTER);
panel3.add(new Button("East-2"),BorderLayout.EAST);
panel3.add(new Button("West-2"),BorderLayout.WEST);
for (int i = 0; i < 4; i++) {
panel4.add(new Button("for"+i));
}
panel3.add(panel4,BorderLayout.CENTER);
frame.add(panel1);
frame.add(panel3);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
总结:
1.Frame是一个顶级窗口
2.Panel无法单独显示,必须添加到某个容器中
3.布局管理器
(1)流式
(2)东西南北中
(3)表格
4.大小(setSize)定位(setLaction)背景颜色(setBackground)可见性(setVisible) 监听(addWindowListener(new WindowAdapter)
2.4事件监听
事件监听:按下按钮发生一些事情
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 Demo08TestActionEvent {
public static void main(String[] args) {
//按下按钮时触发一些事件
Frame frame = new Frame();
Button button = new Button();
//因为,addActionListener()需要一个ActionListener,所以我们构造一个ActionListener
// button.addActionListener(new ActionListener() {
// @Override
// public void actionPerformed(ActionEvent e) {
// System.out.println("aaa");
// }
// });
MyActionListener myActionListener = new MyActionListener();
button.addActionListener(myActionListener);
frame.add(button,BorderLayout.CENTER);
frame.pack();
windowClose(frame);
frame.setVisible(true);
}
//关闭窗口的事件
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
//事件监听
class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("aaa");
}
}
多个按钮控制同一个事件(监听)
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Demo09TestActionEventTwo {
public static void main(String[] args) {
//两个按钮实现同一个监听
//开始 - 停止
Frame frame = new Frame();
Button button1 = new Button("start");
Button button2 = new Button("stop");
//如果不显示定义会走默认的值
button2.setActionCommand("button2-stop");
//可以多个按钮只写一个监听类
MyMoniter myMoniter = new MyMoniter();
button1.addActionListener(myMoniter);
MyMoniter myMoniter1 = new MyMoniter();
button2.addActionListener(myMoniter1);
frame.add(button1,BorderLayout.EAST);
frame.add(button2,BorderLayout.WEST);
frame.pack();
frame.setVisible(true);
}
}
class MyMoniter implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//e.getActionCommand()获得按钮信息
System.out.println("按钮被点击了:msg"+e.getActionCommand());
}
}
2.5输入框TextField监听
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 Demo10TestTextField {
public static void main(String[] args) {
//正常写代码main方法里面只管启动
new MyFrame1();
}
}
class MyFrame1 extends Frame {
public MyFrame1(){
TextField textField = new TextField();
add(textField);
//监听这个文本框输入的文字
MyActionListener1 myActionListener1 = new MyActionListener1();
//按下enter 就会触发这个输入框的事件
textField.addActionListener(myActionListener1);
//设置替换编码
textField.setEchoChar('*');
pack();
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setVisible(true);
}
}
class MyActionListener1 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
TextField field = (TextField) e.getSource();//获得一些资源,返回的一个对象
System.out.println(field.getText());
field.setText("");//null ""
}
}
2.6简易计算机(组合+内部类回顾复习)
初始代码
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 Demo11TestCalc {
//简易计算机
public static void main(String[] args) {
new Calculator();
}
}
class Calculator extends Frame {
public Calculator(){
// 3 个文本框
TextField t1 = new TextField(10);
TextField t2 = new TextField(10);
TextField t3 = new TextField(20);
//一个按钮
Button button = new Button("=");
MyCalculatorListener myCalculatorListener = new MyCalculatorListener(t1,t2,t3);
button.addActionListener(myCalculatorListener);
//一个标签
Label label = new Label("+");
setLayout(new FlowLayout());
add(t1);
add(label);
add(t2);
add(button);
add(t3);
pack();
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyCalculatorListener implements ActionListener{
private TextField t1, t2, t3;
public MyCalculatorListener(TextField t1, TextField t2, TextField t3){
this.t1 = t1;
this.t2 = t2;
this.t3 = t3;
}
@Override
public void actionPerformed(ActionEvent e) {
//获得加数和被加数
int n1 = Integer.parseInt(t1.getText());
int n2 = Integer.parseInt(t2.getText());
//将这个值 + 加法运算算后放到第三个框中
t3.setText(""+(n1+n2));
//3.清除前两个框
t1.setText("");
t2.setText("");
}
}
完全改造为面向对象的写法
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 Demo11TestCalc {
//简易计算机
public static void main(String[] args) {
new Calculator();
}
}
class Calculator extends Frame {
//属性
TextField t1,t2,t3;
public Calculator(){
// 3 个文本框
t1 = new TextField(10);
t2 = new TextField(10);
t3 = new TextField(20);
//一个按钮
Button button = new Button("=");
MyCalculatorListener myCalculatorListener = new MyCalculatorListener(this);
button.addActionListener(myCalculatorListener);
//一个标签
Label label = new Label("+");
setLayout(new FlowLayout());
add(t1);
add(label);
add(t2);
add(button);
add(t3);
pack();
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyCalculatorListener implements ActionListener{
Calculator calculator;
public MyCalculatorListener(Calculator calculator){
this.calculator = calculator;
}
@Override
public void actionPerformed(ActionEvent e) {
//获得加数和被加数
int n1 = Integer.parseInt(calculator.t1.getText());
int n2 = Integer.parseInt(calculator.t2.getText());
//将这个值 + 加法运算算后放到第三个框中
calculator.t3.setText(""+(n1+n2));
//3.清除前两个框
calculator.t1.setText("");
calculator.t2.setText("");
}
}
内部类:
(1)更好的包装
//内部类
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 Demo11TestCalc {
//简易计算机
public static void main(String[] args) {
new Calculator();
}
}
class Calculator extends Frame {
//属性
TextField t1,t2,t3;
public Calculator(){
// 3 个文本框
t1 = new TextField(10);
t2 = new TextField(10);
t3 = new TextField(20);
//一个按钮
Button button = new Button("=");
//内部类的最大好处就是可以畅通无阻的访问外部类的属性和方法!
button.addActionListener(new MyCalculatorListener());
//一个标签
Label label = new Label("+");
setLayout(new FlowLayout());
add(t1);
add(label);
add(t2);
add(button);
add(t3);
pack();
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
//内部类
private class MyCalculatorListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//获得加数和被加数
int n1 = Integer.parseInt(t1.getText());
int n2 = Integer.parseInt(t2.getText());
//将这个值 + 加法运算算后放到第三个框中
t3.setText(""+(n1+n2));
//3.清除前两个框
t1.setText("");
t2.setText("");
}
}
}
2.7画笔
import java.awt.*;
public class Demo12TestPaint {
public static void main(String[] args) {
new MyPaint().loadFrame();
}
}
class MyPaint extends Frame{
public void loadFrame(){
setBounds(200,200,600,400);
setVisible(true);
}
//画笔
@Override
public void paint(Graphics g) {
//画笔需要有颜色,画笔可以画画
g.setColor(Color.BLUE);
g.drawRect(200,200,500,100);
g.fillOval(300,300,101,101);
//养成习惯,画笔用完还原他最初的颜色
}
}
2.8鼠标监听
目的:实现鼠标画画
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MZE7LuuU-1614856570794)(C:\Users\帅气的影山瑞\AppData\Roaming\Typora\typora-user-images\image-20210303193412832.png)]
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
//鼠标监听事件
public class Demo13TestMouseListener {
public static void main(String[] args) {
new MyFrame2("画图");
}
}
//自己的类
class MyFrame2 extends Frame{
ArrayList points;
//画画需要画笔,需要监听鼠标当前的位置,需要集合来存储这个点
public MyFrame2(String name){
super(name);
this.setBounds(200,200,400,300);
//存鼠标点击的点
points = new ArrayList();
//鼠标监听器,是针对窗口而言
addMouseListener(new MymouseListener());
setVisible(true);
}
@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) {
MyFrame2 myFrame = (MyFrame2) e.getSource();
//点击的时候就会在界面上产生一个点!
//这个点就是鼠标的点
myFrame.addPaint( new Point(e.getX(), e.getY()));
myFrame.repaint();//刷新
}
}
}
2.9窗口监听
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.SQLOutput;
public class Demo14TestWindowListener {
public static void main(String[] args) {
new MyFrame01();
}
}
class MyFrame01 extends Frame {
public MyFrame01(){
setBounds(200,200,200,200);
setBackground(Color.cyan);
setVisible(true);
addWindowListener(new WindowAdapter() {
//关闭窗口
@Override
public void windowClosing(WindowEvent e) {
System.out.println("windowClosing");
}
//激活窗口
@Override
public void windowActivated(WindowEvent e) {
MyFrame2 source = (MyFrame2) e.getSource();
source.setTitle("被激活了");
System.out.println("WindowActivated");
}
});
}
}
2.10键盘监听
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class Demo15TestKeyListener {
public static void main(String[] args) {
new MyKeyListener();
}
}
class MyKeyListener extends Frame{
public MyKeyListener(){
setBounds(200,200,220,220);
setVisible(true);
this.addKeyListener( new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();//不需要去记录这个数值,直接使用静态属性 VK_XXX
System.out.println(keyCode);
if (keyCode == KeyEvent.VK_UP){
System.out.println("你按下了上键");
}
}
});
}
3、Swing
3.1窗口、面板
package Swing;
import javax.swing.*;
import java.awt.*;
public class Demo01Jframe {
public void init(){
//顶级窗口JFrame
JFrame jf = new JFrame();
jf.setBounds(100,100,200,200);
jf.setVisible(true);
JLabel jLabel = new JLabel("欢迎来到624");
jf.add(jLabel);
Container contenter = jf.getContentPane();
contenter.setBackground(new Color(3,33,225));
//让标签文本居中
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
//关闭事件
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
//建立一个窗口
new Demo01Jframe().init();
}
}
3.2弹窗
JDialog,用来弹出,默认就有关闭事件!
package Swing;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//主窗口
public class Demo02Dialog extends JFrame {
public void init(){
setVisible(true);
setSize(300,300);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//JFrame放东西,容器
Container container = 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 MyDialog();
}
});
container.add(button);
}
public static void main(String[] args) {
new Demo02Dialog().init();
}
}
//弹出窗口
class MyDialog extends JDialog{
public MyDialog(){
setVisible(true);
setSize(300,200);
//弹窗默认自带关闭程序
Container containter = getContentPane();
JLabel label = new JLabel("这是一个弹窗");
label.setHorizontalAlignment(SwingConstants.CENTER);
containter.add(label);
}
}
3.3标签
Label
new JLable("XXXXX");
图标ICON
package Swing;
import javax.swing.*;
import java.awt.*;
//图标,需要实现类, Frame继承
public class Demo03Icon extends JFrame implements Icon {
private int width;
private int height;
public Demo03Icon(){
}
public Demo03Icon(int width, int height){
this.height = height;
this.width = width;
}
public void init(){
Demo03Icon icon = new Demo03Icon(15, 15);
//图标放在标签上(也可以放在其他地方)
JLabel label = new JLabel("iconText", icon, SwingConstants.CENTER);
Container container = getContentPane();
container.add(label);
setVisible(true);
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 this.width;
}
@Override
public int getIconHeight() {
return this.height;
}
public static void main(String[] args) {
new Demo03Icon().init();
}
}
3.4面板
JPanel
package Swing;
import javax.swing.*;
import java.awt.*;
public class Demo05JPanel extends JFrame {
public static void main(String[] args) {
new Demo05JPanel();
}
public Demo05JPanel(){
Container container = getContentPane();
container.setLayout(new GridLayout(2,2,10,10));//后面参数的意思是间距
JPanel jpanel1 = new JPanel(new GridLayout(2,1));
JPanel jpanel2 = new JPanel(new GridLayout(1,3));
JPanel jpanel3 = new JPanel(new GridLayout(3,1));
JPanel jpanel4 = new JPanel(new GridLayout(2,2));
jpanel1.add(new JButton("1"));
jpanel1.add(new JButton("2"));
jpanel2.add(new JButton("3"));
jpanel2.add(new JButton("4"));
jpanel2.add(new JButton("5"));
jpanel3.add(new JButton("6"));
jpanel3.add(new JButton("7"));
jpanel3.add(new JButton("8"));
jpanel4.add(new JButton("9"));
jpanel4.add(new JButton("10"));
jpanel4.add(new JButton("11"));
jpanel4.add(new JButton("12"));
container.add(jpanel1);
container.add(jpanel2);
container.add(jpanel3);
container.add(jpanel4);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
JScrollPanel(可以滚动的面板)
package Swing;
import javax.swing.*;
import java.awt.*;
public class Demo06JScrollPanel extends JFrame {
public static void main(String[] args) {
new Demo06JScrollPanel();
}
public Demo06JScrollPanel() {
Container container = getContentPane();
//文本域
JTextArea area = new JTextArea(20,50);
//设置默认文本
area.setText("mikasa");
JScrollPane jScrollPane = new JScrollPane(area);
container.add(jScrollPane);
setVisible(true);
setBounds(200,200,300,340);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
3.5按钮
(1)图片按钮
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class Demo07JButtonIcon extends JFrame {
public Demo07JButtonIcon() {
Container container = getContentPane();
URL icon = Demo07JButtonIcon.class.getResource("tupian.jpg");
ImageIcon imageIcon = new ImageIcon(icon);
JButton jButton = new JButton(imageIcon);
container.add(jButton);
setVisible(true);
setBounds(200,200,300,300);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Demo07JButtonIcon();
}
}
(2)单选按钮(单选框JRadioButton)
package Swing;
import javax.swing.*;
import java.awt.*;
public class Demo08JRadioButton extends JFrame {
public Demo08JRadioButton() {
Container container = getContentPane();
JRadioButton button1 = new JRadioButton("radioButton-1");
JRadioButton button2 = new JRadioButton("radioButton-2");
JRadioButton button3 = new JRadioButton("radioButton-3");
ButtonGroup group = new ButtonGroup();
group.add(button1);
group.add(button2);
group.add(button3);
container.add(button1,BorderLayout.NORTH);
container.add(button2,BorderLayout.CENTER);
container.add(button3,BorderLayout.SOUTH);
setVisible(true);
setBounds(200,200,300,300);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Demo08JRadioButton();
}
}
(3)复选按钮(复选框CheckBox)
package Swing;
import javax.swing.*;
import java.awt.*;
public class Demo09CheckBox extends JFrame {
public static void main(String[] args) {
new Demo09CheckBox();
}
public Demo09CheckBox() {
Container container = getContentPane();
Checkbox checkbox1 = new Checkbox("checkbox");
Checkbox checkbox2 = new Checkbox("checkbox");
container.add(checkbox1,BorderLayout.NORTH);
container.add(checkbox2,BorderLayout.SOUTH);
setVisible(true);
setBounds(200,200,390,200);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
3.6列表
(1)下拉框
combobox 下拉列表框
package Swing;
import javax.swing.*;
import java.awt.*;
public class Demo10Combobox extends JFrame {
public static void main(String[] args) {
new Demo10Combobox();
}
public Demo10Combobox(){
Container container = getContentPane();
JComboBox status = new JComboBox();
status.addItem("火影忍者");
status.addItem("野良神");
status.addItem("海贼王");
container.add(status);
setVisible(true);
setSize(300,250);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
(2)列表框
package Swing;
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
public class Demo11JList extends JFrame {
public static void main(String[] args) {
new Demo11JList();
}
public Demo11JList() {
Container container = getContentPane();
//生成列表的内容
//String[] contains = {"1","2","3"};
Vector contains = new Vector();
JList jList = new JList(contains);
contains.add("胡成");
contains.add("zzw");
contains.add("谭腾达");
container.add(jList);
setVisible(true);
setSize(200,200);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
3.7文本框
(1)文本框
package Swing;
import javax.swing.*;
import java.awt.*;
public class Demo12TextField extends JFrame {
public static void main(String[] args) {
new Demo12TextField();
}
public Demo12TextField() {
Container container = getContentPane();
JTextField textField1 = new JTextField("hello!");
JTextField textField2 = new JTextField("world",20);
container.add(textField1,BorderLayout.NORTH);
container.add(textField2,BorderLayout.SOUTH);
setVisible(true);
setSize(300,200);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
(2)密码框
package Swing;
import javax.swing.*;
import java.awt.*;
public class Demo13PasswordField extends JFrame {
public static void main(String[] args) {
new Demo13PasswordField();
}
public Demo13PasswordField() {
Container container = getContentPane();
JPasswordField passwordField = new JPasswordField();//.....
passwordField.setEchoChar('*');
container.add(passwordField);
setVisible(true);
setSize(400,400);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
(3)文本域
package Swing;
import javax.swing.*;
import java.awt.*;
public class Demo06JScrollPanel extends JFrame {
public static void main(String[] args) {
new Demo06JScrollPanel();
}
public Demo06JScrollPanel() {
Container container = getContentPane();
//文本域
JTextArea area = new JTextArea(20,50);
//设置默认文本
area.setText("mikasa");
JScrollPane jScrollPane = new JScrollPane(area);
container.add(jScrollPane);
setVisible(true);
setBounds(200,200,300,340);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}