package GUI.lesson6;
import javax.swing.*;
import java.awt.*;
public class JFrameDemo {
//init()初始化
public void init(){
//顶级窗口
JFrame jf= new JFrame("这是一个JFrame窗口");
jf.setVisible(true);
jf.setBounds(100,100,200,200);
//设置文字 Jlabel
JLabel label = new JLabel("欢迎");
jf.add(label);
//让文本标签居中,设置水平对齐
label.setHorizontalAlignment(SwingConstants.CENTER);
//容器实例化
Container container = jf.getContentPane();
container.setBackground(Color.BLUE);
//关闭事件
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
//建立一个窗口
new JFrameDemo().init();
}
}
Swing相比于AWT方便了许多,但是本质还是继承了Frame进行的一系列操作,增加了很多类,方便了我们的直接调用,不需要手动去写某些类,比如关闭窗口事件等等。
JDialog,用来被弹出,默认就有关闭事件!
package GUI.lesson6;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//主窗口
public class DialogDemo extends JFrame {
public static void main(String[] args) {
new DialogDemo();
}
public DialogDemo() {
this.setSize(700,500);
// this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//弹窗自带关闭窗口事件
this.setVisible(true);
//JFrame 放东西,容器,容器实例化
Container container = this.getContentPane();
//绝对布局
container.setLayout(null);
//按钮
JButton button = new JButton("点击弹出一个对话框");
button.setBounds(30,30,200,50);
this.add(button);
//点击这个按钮的时候,弹出一个弹窗
button.addActionListener(new ActionListener() {
//监听器
@Override
public void actionPerformed(ActionEvent e) {
//弹窗
new MyDialog();
}
});
}
}
//弹窗的窗口
class MyDialog extends JDialog{
public MyDialog() {
this.setBounds(100,100,500,500);
this.setVisible(true);
// this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = this.getContentPane();
container.setLayout(null);
container.add(new Label("世界"));
}
}
label
new JLable("xxx");
图标ICON
package GUI.lesson6;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//主窗口
public class DialogDemo extends JFrame {
public static void main(String[] args) {
new DialogDemo();
}
public DialogDemo() {
this.setSize(700,500);
// this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//弹窗自带关闭窗口事件
this.setVisible(true);
//JFrame 放东西,容器,容器实例化
Container container = this.getContentPane();
//绝对布局
container.setLayout(null);
//按钮
JButton button = new JButton("点击弹出一个对话框");
button.setBounds(30,30,200,50);
this.add(button);
//点击这个按钮的时候,弹出一个弹窗
button.addActionListener(new ActionListener() {
//监听器
@Override
public void actionPerformed(ActionEvent e) {
//弹窗
new MyDialog();
}
});
}
}
//弹窗的窗口
class MyDialog extends JDialog{
public MyDialog() {
this.setBounds(100,100,500,500);
this.setVisible(true);
// this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = this.getContentPane();
container.setLayout(null);
container.add(new Label("世界"));
}
}
图片ICON
package GUI.lesson6;
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("c61f0f39298eab39dd76447ca63bf49.jpg");
ImageIcon imageIcon = new ImageIcon(url);
label.setIcon(imageIcon);
label.setHorizontalAlignment(SwingConstants.CENTER);
Container container = this.getContentPane();
container.add(label);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new ImageIconDemo();
}
}
JPanel
package GUI.lesson7;
import javax.swing.*;
import java.awt.*;
public class JPanelDemo extends JFrame {
public JPanelDemo() {
Container container = 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("2"));
panel1.add(new JButton("3"));
container.add(panel1);
setSize(500,500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new JPanelDemo();
}
}
package GUI.lesson7;
import javax.swing.*;
import java.awt.*;
public class JScrollDemo extends JFrame {
public JScrollDemo() {
Container container = this.getContentPane();
//文本域
JTextArea textArea = new JTextArea(10,20);
textArea.setText("欢迎学习");
//SCroll面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setBounds(100,100,300,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new JScrollDemo();
}
}
图片按钮
package GUI.lesson7;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo01 extends JFrame {
public JButtonDemo01() {
Container container = getContentPane();
//将一个图片变为图标
URL url = JButtonDemo01.class.getResource("c61f0f39298eab39dd76447ca63bf49.jpg");
Icon icon = new ImageIcon(url);
//把这个图标放在按钮上
JButton button = new JButton();
button.setIcon(icon);
button.setToolTipText("图片按钮");
container.add(button);
this.setBounds(100,100,300,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new JButtonDemo01();
}
}
单选按钮
package GUI.lesson7;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo02 extends JFrame{
public JButtonDemo02() {
Container container = 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.SOUTH);
container.add(radioButton2,BorderLayout.CENTER);
container.add(radioButton3,BorderLayout.NORTH);
this.setBounds(100,100,300,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new JButtonDemo02();
}
}
复选按钮
package GUI.lesson7;
import javax.swing.*;
import java.awt.*;
public class JButtonDemo03 extends JFrame{
public JButtonDemo03() {
Container container = getContentPane();
//多选框
JCheckBox checkBox01 = new JCheckBox("checkBox01");
JCheckBox checkBox02 = new JCheckBox("checkBox02");
container.add(checkBox01,BorderLayout.NORTH);
container.add(checkBox02,BorderLayout.SOUTH);
this.setBounds(100,100,300,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new JButtonDemo03();
}
}
下拉框
package GUI.lesson8;
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("11");
status.addItem("22");
status.addItem("33");
container.add(status);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setBounds(100,100,200,200);
this.setVisible(true);
}
public static void main(String[] args) {
new TestComboboxDemo01();
}
}
列表框
(静态扩容)
package GUI.lesson8;
import javax.swing.*;
import java.awt.*;
public class TestComboboxDemo02 extends JFrame {
public TestComboboxDemo02() {
Container container = this.getContentPane();
//生成列表内容 稀疏数组,压缩数据
String[] contents = {
"1","2","3"};
//列表中需要放入内容
JList jList = new JList(contents);
container.add(jList);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setBounds(100, 100, 200, 200);
this.setVisible(true);
}
public static void main(String[] args) {
new TestComboboxDemo02();
}
}
列表框
(动态扩容)
package GUI.lesson8;
import javax.swing.*;
import javax.swing.text.AbstractDocument;
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();
//列表中需要放入内容
JList jList = new JList(contents);
contents.add("11");
contents.add("22");
contents.add("33");
container.add(jList);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setBounds(100, 100, 200, 200);
this.setVisible(true);
}
public static void main(String[] args) {
new TestComboboxDemo02();
}
}
文本框
package GUI.lesson8;
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
public class TestTextDemo01 extends JFrame {
public TestTextDemo01() {
Container container = this.getContentPane();
JTextField textField1 = new JTextField("hello");
JTextField textField2 = new JTextField("world");
container.add(textField1,BorderLayout.NORTH);
container.add(textField2,BorderLayout.SOUTH);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setBounds(100, 100, 200, 200);
this.setVisible(true);
}
public static void main(String[] args) {
new TestTextDemo01();
}
}
密码框
package GUI.lesson8;
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
public class TestTextDemo02 extends JFrame {
public TestTextDemo02() {
Container container = this.getContentPane();
//尽量用面板去做,此地仅为展示使用
JPasswordField passwordField = new JPasswordField();
passwordField.setEchoChar('*');
container.add(passwordField);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setBounds(100, 100, 200, 200);
this.setVisible(true);
}
public static void main(String[] args) {
new TestTextDemo02();
}
}
文本域
package GUI.lesson7;
import javax.swing.*;
import java.awt.*;
public class JScrollDemo extends JFrame {
public JScrollDemo() {
Container container = this.getContentPane();
//文本域
JTextArea textArea = new JTextArea(10,20);
textArea.setText("欢迎学习");
//SCroll面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setBounds(100,100,300,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new JScrollDemo();
}
}