窗口面板 JFrame
基础实现
package com.zlsaxx.lesson4;
import javax.swing.*;
public class JFrameDemo {
//init() 初始化
public void init(){
JFrame frame = new JFrame("这是一个JFrame窗口");
frame.setVisible(true);
frame.setBounds(100,100,500,500);
//关闭事件
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JFrameDemo().init();
}
}
运行弹窗:
实现标签居中
package com.zlsaxx.lesson4;
import javax.swing.*;
import java.awt.*;
public class JFrameDemo02 {
public static void main(String[] args) {
new MyJFrame2().init();
}
}
class MyJFrame2 extends JFrame{
public void init(){
this.setVisible(true);
this.setBounds(10,10,500,500);
//获得一个容器
Container contentPane = this.getContentPane();
contentPane.setBackground(Color.cyan);
//设置文字
JLabel label = new JLabel("ZLSAXX");
add(label);
//让文本居中
label.setHorizontalAlignment(SwingConstants.CENTER);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
运行弹窗:
弹窗 Dialog
基础实现
package com.zlsaxx.lesson4;
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 container = this.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 MyDialogDemo();
}
});
container.add(button);
}
public static void main(String[] args) {
new DialogDemo();
}
}
//弹窗窗口
class MyDialogDemo extends JDialog{
public MyDialogDemo(){
this.setVisible(true);
this.setBounds(100,100,500,500);
//默认就有关闭事件,不用重复添加
//this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = this.getContentPane();
container.setLayout(null);
Label label = new Label("ZLSAXX");
//Label要设置大小才能显示出来
label.setSize(50,50);
container.add(label);
}
}
运行弹窗:
标签
package com.zlsaxx.lesson4;
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.jpg");
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();
}
}
运行弹窗:
面板
普通面板
package com.zlsaxx.lesson5;
import javax.swing.*;
import java.awt.*;
//面板
public class JPanelDemo extends JFrame {
public JPanelDemo() {
Container container = this.getContentPane();
// 2 行 1 列 上下左右间距为 10
container.setLayout(new GridLayout(2,1,10,10));
JPanel panel = new JPanel(new GridLayout(1,3));
panel.add(new JButton("1"));
panel.add(new JButton("2"));
panel.add(new JButton("3"));
container.add(panel);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setVisible(true);
this.setSize(500,500);
}
public static void main(String[] args) {
new JPanelDemo();
}
}
运行弹窗:
滚动面板
package com.zlsaxx.lesson5;
import javax.swing.*;
import java.awt.*;
//有滚动条的面板
public class JScrollDemo extends JFrame {
public JScrollDemo(){
Container container = this.getContentPane();
//文本域
JTextArea jTextArea = new JTextArea(20,50);
jTextArea.setText("ZLSAXX");
//scroll面板
JScrollPane scrollPane = new JScrollPane(jTextArea);
container.add(scrollPane);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setVisible(true);
this.setBounds(100,100,500,500);
}
public static void main(String[] args) {
new JScrollDemo();
}
}
运行弹窗:
按钮
图片按钮
package com.zlsaxx.lesson5;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo01 extends JFrame {
public JButtonDemo01() {
Container container = this.getContentPane();
//将一个图片变为一个图标
URL url = JButtonDemo01.class.getResource("tx.jpg");
Icon icon = new ImageIcon(url);
//将图标放在按钮上
JButton button = new JButton();
button.setIcon(icon);
button.setToolTipText("这是一个图片按钮");
//将按钮加在容器上
container.add(button);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setVisible(true);
this.setSize(500,300);
}
public static void main(String[] args) {
new JButtonDemo01();
}
}
运行弹窗:
单选按钮
package com.zlsaxx.lesson5;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo02 extends JFrame{
public JButtonDemo02() {
Container container = this.getContentPane();
//将一个图片变为一个图标
URL url = JButtonDemo01.class.getResource("tx.jpg");
Icon icon = new ImageIcon(url);
//单选框
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.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setVisible(true);
this.setSize(500,300);
}
public static void main(String[] args) {
new JButtonDemo02();
}
}
运行弹窗:
复选按钮
package com.zlsaxx.lesson5;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo03 extends JFrame{
public JButtonDemo03() {
Container container = this.getContentPane();
//将一个图片变为一个图标
URL url = JButtonDemo01.class.getResource("tx.jpg");
Icon icon = new ImageIcon(url);
//多选框
JCheckBox checkBox1 = new JCheckBox("checkBox01");
JCheckBox checkBox2 = new JCheckBox("checkBox02");
container.add(checkBox1,BorderLayout.NORTH);
container.add(checkBox2,BorderLayout.SOUTH);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setVisible(true);
this.setSize(500,300);
}
public static void main(String[] args) {
new JButtonDemo03();
}
}
运行弹窗:
列表
下拉框
package com.zlsaxx.lesson6;
import javax.swing.*;
import java.awt.*;
public class TestComboboxDemo01 extends JFrame {
public TestComboboxDemo01() {
Container container = this.getContentPane();
//下拉选择框
JComboBox status = new JComboBox();
status.addItem(null);
status.addItem("正在热映");
status.addItem("已下架");
status.addItem("即将上映");
container.add(status);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setVisible(true);
this.setSize(500,300);
}
public static void main(String[] args) {
new TestComboboxDemo01();
}
}
运行弹窗:
列表框
package com.zlsaxx.lesson6;
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
public class TestComboboxDemo02 extends JFrame {
public TestComboboxDemo02() {
Container container = this.getContentPane();
//(静态)生成列表内容
//String[] contents = {"1","2","3"};
//(动态)列表内容
Vector contents = new Vector();
//可以添加和删除列表元素
contents.add("1");
contents.add("2");
contents.add("3");
JList jList = new JList(contents);
container.add(jList);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setVisible(true);
this.setSize(500,300);
}
public static void main(String[] args) {
new TestComboboxDemo02();
}
}
运行弹窗:
文本框
普通文本框
package com.zlsaxx.lesson6;
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.WEST);
container.add(textField2,BorderLayout.EAST);
this.setVisible(true);
this.setSize(500,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextDemo01();
}
}
运行弹窗:
密码框
package com.zlsaxx.lesson6;
import javax.swing.*;
import java.awt.*;
public class TestTextDemo02 extends JFrame {
public TestTextDemo02() {
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 TestTextDemo02();
}
}
运行弹窗:
文本域
在之前的面板环节已经展示过
Container container = this.getContentPane();
//文本域
JTextArea jTextArea = new JTextArea(20,50);
jTextArea.setText("ZLSAXX");
//scroll面板
JScrollPane scrollPane = new JScrollPane(jTextArea);
container.add(scrollPane);