AWT相当于是爸爸,Swing是后来生出来的崽儿
经过进化,能做更多的事情。
3.1 窗口, 面板
JFrameDemo.java
package com.mysoft.lesson04;
import javax.swing.*;
public class JFrameDemo {
//init(); 初始化
public void init() {
JFrame jFrame = new JFrame();
jFrame.setVisible(true);
jFrame.setBounds(100, 100, 200, 200);
//关闭事件
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
//建立一个新窗口
new JFrameDemo().init();
}
}
实行效果:
再给窗口加一个背景色,
用到了JFrame的contentPane。
JFrameDemo.java
package com.mysoft.lesson04;
import javax.swing.*;
import java.awt.*;
public class JFrameDemo {
//init(); 初始化
public void init() {
//JFrame 顶级容器
JFrame jf = new JFrame();
jf.setVisible(true);
jf.setBounds(100, 100, 200, 200);
//jf.setBackground(Color.CYAN);
//容器
Container contentPane = jf.getContentPane();
contentPane.setBackground(Color.blue);
//设置文字 Jlabel
JLabel label = new JLabel("欢迎来到学习区");
//jf.add(label);
contentPane.add(label);
//关闭事件
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
//建立一个新窗口
new JFrameDemo().init();
}
}
执行效果:
//让文本标签居中 设置水平对齐
label.setHorizontalAlignment(SwingConstants.CENTER);
package com.mysoft.lesson04;
import javax.swing.*;
import java.awt.*;
public class JFrameDemo {
//init(); 初始化
public void init() {
//JFrame 顶级容器
JFrame jf = new JFrame();
jf.setVisible(true);
jf.setBounds(100, 100, 200, 200);
//jf.setBackground(Color.CYAN);
//容器
Container contentPane = jf.getContentPane();
contentPane.setBackground(Color.blue);
//设置文字 Jlabel
JLabel label = new JLabel("欢迎来到学习区");
//让文本标签居中 设置水平对齐
label.setHorizontalAlignment(SwingConstants.CENTER);
//jf.add(label);
contentPane.add(label);
//关闭事件
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
//建立一个新窗口
new JFrameDemo().init();
}
}
执行效果:
3.2 弹窗
JDialog,用来被弹出,默认就有关闭事件!
弹窗其实很简单,就是用事件去绑定它就行了。
给按钮添加ActionListener
弹窗就这么点儿代码:
//弹窗的窗口
class MyDialog extends JDialog {
public MyDialog() {
this.setVisible(true);
this.setBounds(500, 500, 300, 300);
//this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = this.getContentPane();
container.setLayout(null);
container.add(new Label("带你学java"));
}
}
完整的代码如下:
package com.mysoft.lesson04;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//主窗口
public class DialogDemo extends JFrame {
public DialogDemo() {
this.setVisible(true);
this.setSize(700, 500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//JFrame 放东西,容器
Container contentPane = this.getContentPane();
//绝对布局
contentPane.setLayout(null);
//按钮
JButton button = new JButton("点击弹出一个对话框");//创建
button.setBounds(30, 30, 200, 50);
//点这个按钮时弹出一个弹窗
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//弹窗
new MyDialog();
}
});
contentPane.add(button);
}
public static void main(String[] args) {
new DialogDemo();
}
}
//弹窗的窗口
class MyDialog extends JDialog {
public MyDialog() {
this.setVisible(true);
this.setBounds(500, 500, 300, 300);
//this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = this.getContentPane();
container.setLayout(null);
container.add(new Label("带你学java"));
}
}
执行效果:
3.3 标签
图标放在标签,也可以放在按钮上
package com.mysoft.lesson04;
import javax.swing.*;
import java.awt.*;
public class IconDemo extends JFrame implements Icon {
private int width;
private int height;
public IconDemo() {//无参构造
}
public IconDemo(int width, int height) {
this.width = width;
this.height = height;
}
public void init() {
IconDemo iconDemo = new IconDemo(20, 20);
//图标放在标签,也可以放在按钮上
JLabel label = new JLabel("icontest", iconDemo, SwingConstants.CENTER);
Container container = getContentPane();
container.add(label);
setVisible(true);
this.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;
}
}
实行效果:
也可以读取图片。
例如:tx.PNG
注意:用如下语句获得图片的url。图片需要放到java文件所在的文件夹。
URL url = ImageIconDemo.class.getResource("tx.PNG");
package com.mysoft.lesson04;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class ImageIconDemo extends JFrame {
public ImageIconDemo() {
//获取图片的地址
JLabel label = new JLabel("ImageIcon");
URL url = ImageIconDemo.class.getResource("tx.PNG");
ImageIcon imageIcon = new ImageIcon(url);//命名不要冲突了
label.setIcon(imageIcon);
label.setHorizontalAlignment(SwingConstants.CENTER);
Container container = getContentPane();
container.add(label);
this.setBounds(100,100,300,200);
this.setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ImageIconDemo();
}
}
实行效果:
3.4 面板
JPanel 面板
package com.mysoft.lesson05;
import javax.swing.*;
import java.awt.*;
public class JPanleDemo extends JFrame {
public JPanleDemo() {
Container container = this.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"));
JPanel panel2 = new JPanel(new GridLayout(2,2));
panel2.add(new JButton("2"));
panel2.add(new JButton("2"));
panel2.add(new JButton("2"));
panel2.add(new JButton("2"));
JPanel panel3 = new JPanel(new GridLayout(2, 3));
panel3.add(new JButton("1-3"));
panel3.add(new JButton("1-3"));
panel3.add(new JButton("1-3"));
panel3.add(new JButton("2-3"));
panel3.add(new JButton("2-3"));
panel3.add(new JButton("2-3"));
container.add(panel1);
container.add(panel2);
container.add(panel3);
this.setVisible(true);
this.setSize(500, 500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JPanleDemo();
}
}
实行效果:
JScrollPane
package com.mysoft.lesson05;
import javax.swing.*;
import java.awt.*;
public class JScrollPanelDemo extends JFrame {
public JScrollPanelDemo() {
Container container = this.getContentPane();
//文本域
JTextArea textArea = new JTextArea(20, 50);
textArea.setText("欢迎学习java");
//Scroll面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setVisible(true);
this.setBounds(100, 100, 400, 300);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollPanelDemo();
}
}
实行效果:
3.5 按钮
下面的这些控件,其实就是记住一些写法。
- 普通按钮 图片按钮
JButtonDemo.java
package com.mysoft.lesson06;
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("tx.PNG");
ImageIcon imageIcon = new ImageIcon(resource);
//把这个图标放在按钮上
JButton button = new JButton();
button.setIcon(imageIcon);
button.setSize(100,50);
button.setToolTipText("这是一个图片按钮");
//add
container.add(button);
this.setVisible(true);
this.setSize(500, 300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo();
}
}
实行效果:
- 单选按钮
//由于单选框只能选择一个,分组,一个组中只能选择一个
ButtonGroup group = new ButtonGroup();
group.add(radioButton01);
group.add(radioButton02);
package com.mysoft.lesson06;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo02 extends JFrame {
public JButtonDemo02() {
Container container = this.getContentPane();
//将一个图片变成图标
URL resource = JButtonDemo.class.getResource("tx.PNG");
ImageIcon imageIcon = new ImageIcon(resource);
//单选框
//JRadioButton radioButton01 = new JRadioButton("JRadioButton01", imageIcon);
//JRadioButton radioButton02 = new JRadioButton("JRadioButton02", imageIcon);
//JRadioButton radioButton03 = new JRadioButton("JRadioButton03", imageIcon);
JRadioButton radioButton01 = new JRadioButton("JRadioButton01");
JRadioButton radioButton02 = new JRadioButton("JRadioButton02");
JRadioButton radioButton03 = new JRadioButton("JRadioButton03");
//由于单选框只能选择一个,分组,一个组中只能选择一个
ButtonGroup group = new ButtonGroup();
group.add(radioButton01);
group.add(radioButton02);
group.add(radioButton03);
container.add(radioButton01, BorderLayout.NORTH);
container.add(radioButton02, BorderLayout.CENTER);
container.add(radioButton03, BorderLayout.SOUTH);
this.setVisible(true);
this.setBounds(100, 100, 400, 300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo02();
}
}
实行效果:
- 多选按钮
package com.mysoft.lesson06;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo03 extends JFrame {
public JButtonDemo03() {
Container container = this.getContentPane();
//将一个图片变成图标
URL resource = JButtonDemo.class.getResource("tx.PNG");
ImageIcon imageIcon = new ImageIcon(resource);
//多选选框
JCheckBox checkbox1 = new JCheckBox("checkbox01");
JCheckBox checkbox2 = new JCheckBox("checkbox02");
JCheckBox checkbox3 = new JCheckBox("checkbox03");
container.add(checkbox1, BorderLayout.NORTH);
container.add(checkbox2, BorderLayout.CENTER);
container.add(checkbox3, BorderLayout.SOUTH);
this.setVisible(true);
this.setBounds(100, 100, 400, 300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo03();
}
}
实行效果:
3.6 列表
- 下拉框
package com.mysoft.lesson07;
import javax.swing.*;
import java.awt.*;
public class TestComboxDemo01 extends JFrame {
public TestComboxDemo01() {
Container container = this.getContentPane();
JComboBox status = new JComboBox();
status.addItem(null);
status.addItem("正在热映");
status.addItem("已下架");
status.addItem("即将上映");
container.add(status);
this.setVisible(true);
this.setBounds(100, 100, 300, 200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboxDemo01();
}
}
执行效果:
- 列表框
不是下拉的,给你展示出来。
例如:
1111,
2222,
3333
package com.mysoft.lesson07;
import javax.swing.*;
import java.awt.*;
public class TestComboxDemo02 extends JFrame {
public TestComboxDemo02() {
Container container = this.getContentPane();
//生成列表的内容
String[] contents = {"1", "2", "3"};
//列表中需要放入内容
JList jList = new JList(contents);
container.add(jList);
this.setVisible(true);
this.setBounds(100, 100, 300, 200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboxDemo02();
}
}
实行效果:
刚才是静态添加列表的内容。
//生成列表的内容
String[] contents = {"1", "2", "3"}; //静态添加
也可以动态添加列表的内容。例如:
Vector contents = new Vector();
contents.add("aaa");//动态添加
contents.add("bbb");
contents.add("ccc");
package com.mysoft.lesson07;
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
public class TestComboxDemo02 extends JFrame {
public TestComboxDemo02() {
Container container = this.getContentPane();
//生成列表的内容
//String[] contents = {"1", "2", "3"}; //静态添加
Vector contents = new Vector();
//列表中需要放入内容
JList jList = new JList(contents);
contents.add("aaa");//动态添加
contents.add("bbb");
contents.add("ccc");
container.add(jList);
this.setVisible(true);
this.setBounds(100, 100, 300, 200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboxDemo02();
}
}
实行效果:
应用场景:
- 选择地区,或者一些单个选项
- 列表,展示信息,一般是动态扩容!
3.7 文本框
- 文本框
package com.mysoft.lesson07;
import javax.swing.*;
import java.awt.*;
public class TestTextDemo01 extends JFrame {
public TestTextDemo01() {
Container container = this.getContentPane();
JTextField textField1 = new JTextField("hello");
JTextField textField2 = new JTextField("world",20);
container.add(textField1,BorderLayout.NORTH);
container.add(textField2,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(500, 200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextDemo01();
}
}
实行效果:
- 密码框
隐藏的问题除了默认的星号,也可以设置成其他字符。
eg:passwordField.setEchoChar('@');
package com.mysoft.lesson07;
import javax.swing.*;
import java.awt.*;
public class TestPasswordDemo extends JFrame {
public TestPasswordDemo() {
Container container = this.getContentPane();
JPasswordField passwordField = new JPasswordField();
//passwordField.setEchoChar('@');
container.add(passwordField);
this.setVisible(true);
this.setSize(500, 200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestPasswordDemo();
}
}
实行效果:
- 文本框
JTextArea
package com.mysoft.lesson05;
import javax.swing.*;
import java.awt.*;
public class JScrollPanelDemo extends JFrame {
public JScrollPanelDemo() {
Container container = this.getContentPane();
//文本域
JTextArea textArea = new JTextArea(20, 50);
textArea.setText("欢迎学习java");
//Scroll面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setVisible(true);
this.setBounds(100, 100, 400, 300);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollPanelDemo();
}
}
实行效果: