Swing包括图形用户界面:窗口面板、弹窗、标签、面板、按钮、列表、文本框。
1. 窗口面板
先初始化,new一个顶级窗口,设置背景、大小可见性,设置文字,然后关闭事件
package com.yang.lesson2;
import javax.swing.*;
import java.awt.*;
public class JFrameDemo {
//初始化
public void init(){
JFrame jf = new JFrame("jframe");
jf.setVisible(true);
jf.setBounds(100,100,200,200);
jf.setBackground(Color.cyan);
JLabel label = new JLabel("YANGYANG");
jf.add(label);
/*
标签居中:
1.让文本标签居中,设置水平对齐
label.setHorizontalAlignment(SwingConstants.CENTER);
2.获得一个容器
Container container = jf.getContentPane();
container.setBackground(Color.YELLOW);
* */
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JFrameDemo().init();
}
}
2. 弹窗
弹窗就是我们平时看到的弹窗。
先有一个放东西的容器(container),再new一个按钮,设置按钮大小,点击这个按钮的时候弹出这个弹窗,这里用到监听器,把按钮放进容器里。
package com.yang.lesson2;
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(500,500);
this.setBackground(Color.BLUE);
//弹窗关闭
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//获得容器
Container contentPane = this.getContentPane();
contentPane.setLayout(null);
//新建按钮
JButton jButton = new JButton("kick");
jButton.setBounds(20,20,100,100);
//监听 给按钮加一个监听
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//点击按钮出现的弹框
new MyDialogDemo();
}
});
contentPane.add(jButton);
}
public static void main(String[] args) {
new DialogDemo();
}
}
//弹窗的窗口内容
class MyDialogDemo extends JDialog{
public MyDialogDemo(){
this.setVisible(true);
this.setBounds(100,100,200,200);
// this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = this.getContentPane();
container.setLayout(null);
container.add(new JLabel("yangyang"));
}
}
3. 标签 label
标签有图标标签和有图画标签:
- 图标ICON
需要实现JFrame类继承Icon,设置按钮宽和高,然后初始化,new一个图标,图标可以放在标签上,也可以放在按钮上,放在谁上new一个谁就好,获取一个容器,存放标签或按钮,设置可见可关闭。设置获取图标长宽和位置。
package com.yang.lesson2;
import javax.swing.*;
import java.awt.*;
public class IconDemo extends JFrame implements Icon {
private int width;
private int heigth;
public IconDemo() { }
public IconDemo(int width, int heigth) {
this.width = width;
this.heigth = heigth;
}
public void init(){
//图标 标签
IconDemo iconDemo = new IconDemo(15, 15);
JLabel label = new JLabel("ICON", iconDemo, SwingConstants.CENTER);
Container container = getContentPane();
container.add(label);
this.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,heigth);
}
@Override
public int getIconWidth() {
return this.width;
}
@Override
public int getIconHeight() {
return this.heigth;
}
}
- 图片ICON
package com.yang.lesson2;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class ImageIconDemo extends JFrame {
public ImageIconDemo(){
JLabel label = new JLabel("JAVA");
URL resource = ImageIconDemo.class.getResource("tx.jpg");//当前包下的
ImageIcon imageIcon = new ImageIcon(resource);//图片
label.setIcon(imageIcon);//图片放到标签上
label.setHorizontalAlignment(SwingConstants.CENTER);//标签放到中间
//获取容器
Container container = getContentPane();
container.add(label);
setVisible(true);
setBounds(100,100,100,100);
}
public static void main(String[] args) {
new ImageIconDemo();
}
}
4. 面板
- 不带滑动条的面板
获得一个容器设置这个容器内的布局,装这些面板,new好多面板,将面板放到容器中。
package com.yang.lesson2;
import javax.swing.*;
import java.awt.*;
public class JPanelDemo extends JFrame {
public JPanelDemo(){
Container container = this.getContentPane();
container.setLayout(new GridLayout(2,2,10,10));
JPanel jPanel1 = new JPanel(new GridLayout(1, 3));
JPanel jPanel2 = new JPanel(new GridLayout(1, 2));
JPanel jPanel3 = new JPanel(new GridLayout(2, 3));
JPanel jPanel4 = new JPanel(new GridLayout(3, 2));
jPanel1.add(new JButton("1"));
jPanel1.add(new JButton("1"));
jPanel1.add(new JButton("1"));
jPanel2.add(new JButton("2"));
jPanel2.add(new JButton("2"));
jPanel3.add(new JButton("3"));
jPanel3.add(new JButton("3"));
jPanel3.add(new JButton("3"));
jPanel4.add(new JButton("4"));
jPanel4.add(new JButton("4"));
jPanel4.add(new JButton("4"));
jPanel4.add(new JButton("4"));
jPanel4.add(new JButton("4"));
container.add(jPanel1);
container.add(jPanel2);
container.add(jPanel3);
container.add(jPanel4);
this.setVisible(true);
this.setBounds(200,200,200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JPanelDemo();
}
}
- 带滑动条的面板
容器的显示区域不够显示组建的所有内容时,让容器带滚动条,显示所有内容。获得一个容器、面板、文本域,把文本域放在面板里,把面板放在容器里,设置大小、可见和关闭。
package com.yang.lesson2;
import javax.swing.*;
import java.awt.*;
public class JScrollPanel extends JFrame {
/*带滑动条的面板*/
//方法
public JScrollPanel(){
//获得一个容器
Container container = this.getContentPane();
//文本域
JTextArea textArea = new JTextArea(20, 50);
textArea.setText("YANGYANG");
//Scroll面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setVisible(true);
this.setBounds(100,100,300,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollPanel();
}
}
5. 按钮
获得一个容器,用getResource获取图片的定位得到图片的URL(唯一资源定位符),用ImageIcon将图片变成图标,new一个按钮,把图标放在按钮上,把按钮放在容器里,设置大小可见和关闭。
package com.yang.lesson2;
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.jpg");
Icon icon = new ImageIcon(resource);
JButton button = new JButton();
button.setIcon(icon);
button.setToolTipText("JButton");
container.add(button);
this.setVisible(true);
this.setSize(500, 300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo();
}
}
- 单选按钮 JRadioButton
获得容器,new几个单选框,将每个单选框放入分组,一个分组中只能发一个单选框,将单选框放入容器里,设置大小可见和关闭。
package com.yang.lesson2;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButton02 extends JFrame {
public JButton02(){
Container container = this.getContentPane();
//单选框
JRadioButton radioButton1 = new JRadioButton("JRadioButton01");
JRadioButton radioButton2 = new JRadioButton("JRadioButton02");
JRadioButton radioButton3 = new JRadioButton("JRadioButton03");
//由于单选框只能选择一个,分组,一个组中只能选择一个
ButtonGroup group = new ButtonGroup();
group.add(radioButton1);
group.add(radioButton2);
group.add(radioButton3);
container.add(radioButton1,BorderLayout.CENTER);
container.add(radioButton2,BorderLayout.NORTH);
container.add(radioButton3,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(500,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButton02();
}
}
- 复选按钮 JCheckBox
获得一个容器,new几个多选框,把多选框加入到容器中,设置大小可见和关闭。
package com.yang.lesson2;
import javax.swing.*;
import java.awt.*;
public class JButton3 extends JFrame {
public JButton3(){
Container container = this.getContentPane();
JCheckBox checkBox01 = new JCheckBox("DO YOU LOVE ME? YES!");
JCheckBox checkBox02 = new JCheckBox("DO YOU LOVE REALLY ME? YES!");
container.add(checkBox01,BorderLayout.NORTH);
container.add(checkBox02,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(500,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButton3();
}
}
6. 列表
- 下拉框 JComboBox
new容器和下拉组件,将下拉框中添加数据项目,将下拉框放入容器中。
package com.yang.lesson2;
import javax.swing.*;
import java.awt.*;
public class ComboboxDemo1 extends JFrame {
public ComboboxDemo1(){
Container container = this.getContentPane();
JComboBox status = new JComboBox();
status.addItem("DO YOU LOVE ME?");
status.addItem("YES");
status.addItem("NO");
container.add(status);
this.setVisible(true);
this.setSize(500,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ComboboxDemo1();
}
}
- 列表框
new一个顶层容器和放文本的容器,将放文本的容器放入列表中,列表中放入内容,将列表放入顶级容器中。
package com.yang.lesson2;
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
public class ComboboxDemo2 extends JFrame {
public ComboboxDemo2(){
Container container = this.getContentPane();
//生成列表的内容
Vector contents = new Vector();
//列表中需要放入内容
JList jList = new JList(contents);
contents.add("1");
contents.add("2");
contents.add("3");
container.add(jList);
this.setVisible(true);
this.setSize(500,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ComboboxDemo2();
}
}
应用场景
- 选择地区,或者一些单个选项
- 列表,展示信息,一般是动态扩容!
7. 文本框
- 文本框 JTextField
new一个容器和几个文本框,文本框输入内容,放到容器内。
package com.yang.lesson2;
import javax.swing.*;
import java.awt.*;
public class TextDemo1 extends JFrame {
public TextDemo1(){
Container container = this.getContentPane();
JTextField textField = new JTextField("yangyang");
JTextField textField2 = new JTextField("hello",10);
container.add(textField,BorderLayout.NORTH);
container.add(textField2,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(100,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TextDemo1();
}
}
- 密码框 JPasswordField
new一个顶级容器和密码框,设置密码隐藏,将文本框放入容器。
package com.yang.lesson2;
import javax.swing.*;
import java.awt.*;
public class TextDemo2 extends JFrame {
public TextDemo2(){
Container container = this.getContentPane();
JPasswordField passwordField = new JPasswordField();
//将密码隐藏起来
passwordField.setEchoChar('$');
container.add(passwordField);
this.setVisible(true);
this.setSize(500,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TextDemo2();
}
}
- 文本域 JScrollPane
new一个容器、一个文本域、一个面板,文本域用来输入内容,面板用来放文本域,容器用来放面板。
package com.yang.lesson2;
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("Do you love me?");
//面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setVisible(true);
this.setBounds(100,100,300,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollPanelDemo();
}
}