Java-GUI编程(2)Swing

2.Swing

2.1.窗口,面板

import javax.swing.*;
import java.awt.*;

public class JFrameTest2 {
    public static void main(String[] args) {
        new MyJframe2().init();
    }
}

class MyJframe2 extends JFrame{
    //init() 初始化
    public void init(){
        //顶级窗体
        this.setVisible(true);
        this.setBounds(100,100,200,200);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        //设置文字 JLabel
        JLabel jLabel = new JLabel("一段文字");
        add(jLabel);
        //让文本标签居中,设置水平对齐
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);

        //容器实例化,获得一个容器
        Container contentPane = this.getContentPane();
        contentPane.setBackground(Color.blue);
    }
}

2.2.弹窗

JDialog

默认有关闭事件

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//主窗体
public class DialogTest extends JFrame {

    public DialogTest(){
        this.setBounds(200,200,300,300);
        this.setVisible(true);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        //获取窗体的容器
        Container container = this.getContentPane();
        //绝对布局
        container.setLayout(null);

        //按钮
        Button button = new Button("Dialog");
        button.setBounds(30,30,200,100);

        //点击这个按钮的时候,弹出一个弹窗
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //弹窗
                new MyDialog();
            }
        });

        container.add(button);
    }

    public static void main(String[] args) {
        new DialogTest();
    }
}

//弹窗的窗体
class MyDialog extends JDialog{
    public MyDialog() {
        this.setVisible(true);
        this.setBounds(200,200,300,300);
        //弹窗默认有关闭方式
        //this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        Container container = this.getContentPane();
        container.setLayout(null);
        JLabel jLabel = new JLabel("这是一个弹窗");
        jLabel.setBounds(100,100,200,200);
        container.add(jLabel);
    }
}

2.3.标签

JLabel

new JLabel("");

图标Icon

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class ImageIconTest extends JFrame{


    public ImageIconTest(){
        //获取图片的地址
        JLabel label = new JLabel();
        URL url = ImageIconTest.class.getResource("图片.jpg");  //获取本类同级目录下的资源

        ImageIcon imageIcon = new ImageIcon(url);
        label.setIcon(imageIcon);
        label.setHorizontalAlignment(SwingConstants.CENTER);

        Container container = getContentPane();
        container.add(label);

        setVisible(true);
        setBounds(500,500,300,300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {
        new ImageIconTest();
    }
}

2.4.面板

1.面板

JPanel

import javax.swing.*;
import java.awt.*;

public class JPanelTest extends JFrame {

    public JPanelTest() {
        Container container = getContentPane();
        container.setLayout(new GridLayout(2,1,10,10)); //后面的两个参数为间距

        JPanel panel1 = new JPanel(new GridLayout(1,3));
        JPanel panel2 = new JPanel(new GridLayout(1,2));
        JPanel panel3 = new JPanel(new GridLayout(2,1));
        JPanel panel4 = new JPanel(new GridLayout(2,2));

        panel1.add(new Button("1"));
        panel1.add(new Button("1"));
        panel1.add(new Button("1"));
        panel2.add(new Button("2"));
        panel2.add(new Button("2"));
        panel3.add(new Button("3"));
        panel3.add(new Button("3"));
        panel4.add(new Button("4"));
        panel4.add(new Button("4"));
        panel4.add(new Button("4"));
        panel4.add(new Button("4"));

        container.add(panel1);
        container.add(panel2);
        container.add(panel3);
        container.add(panel4);
        this.setVisible(true);
        this.setBounds(100,100,200,200);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new JPanelTest();
    }
}
2.滚动面板

JScrollPanel

import javax.swing.*;
import java.awt.*;

public class JScrollPanelTest extends JFrame {

    public JScrollPanelTest(){
        Container container = getContentPane();

        //文本域
        JTextArea textArea = new JTextArea();
        textArea.setText("这是一个文本域");

        //Scroll面板
        JScrollPane scrollPane = new JScrollPane(textArea);
        container.add(scrollPane);


        this.setVisible(true);
        this.setBounds(200,300,300,400);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new JScrollPanelTest();
    }
}

2.5.按钮

1.图片按钮
import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class JButtonTest01 extends JFrame {

    public JButtonTest01(){
        Container container = this.getContentPane();
        //将一个图片变为图标
        URL url = JButtonTest01.class.getResource("图片02.jpg");
        Icon icon = new ImageIcon(url);

        //把这个图片添加到按钮
        JButton button = new JButton();
        button.setIcon(icon);
        button.setToolTipText("图片按钮");

        container.add(button);

        this.setVisible(true);
        this.setBounds(200,200,300,500);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new JButtonTest01();
    }
}
2.单选按钮

RadioButton

import javax.swing.*;
import java.awt.*;

public class JButtonTest02 extends JFrame {

    public JButtonTest02(){
        Container container = this.getContentPane();

        //单选框
        JRadioButton radioButton1 = new JRadioButton("01");
        JRadioButton radioButton2 = new JRadioButton("02");
        JRadioButton radioButton3 = new JRadioButton("03");

        //由于单选框只能选一个,分组,一个组只能选一个
        ButtonGroup group = new ButtonGroup();
        group.add(radioButton1);
        group.add(radioButton2);
        group.add(radioButton3);

        container.add(radioButton1, BorderLayout.NORTH);
        container.add(radioButton2, BorderLayout.CENTER);
        container.add(radioButton3, BorderLayout.SOUTH);

        this.setVisible(true);
        this.setBounds(200,200,300,500);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new JButtonTest02();
    }
}
3.复选按钮

CheckBox

import javax.swing.*;
import java.awt.*;

public class JButtonTest03 extends JFrame {

    public JButtonTest03(){
        Container container = this.getContentPane();

        JCheckBox checkBox1 = new JCheckBox("01");
        JCheckBox checkBox2 = new JCheckBox("02");

        container.add(checkBox1, BorderLayout.NORTH);
        container.add(checkBox2, BorderLayout.SOUTH);

        this.setVisible(true);
        this.setBounds(200,200,300,500);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new JButtonTest03();
    }
}

2.6.列表

1.下拉框

JComboBox

import javax.swing.*;
import java.awt.*;

public class ComboboxTest extends JFrame {
    public ComboboxTest(){

        Container container = this.getContentPane();

        JComboBox comboBox = new JComboBox();
        comboBox.addItem(null);
        comboBox.addItem("正在热映");
        comboBox.addItem("即将上映");
        comboBox.addItem("已下架");

        container.add(comboBox);

        this.setVisible(true);
        this.setBounds(200,200,300,500);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new ComboboxTest();
    }
}
2.列表框

JList

import javax.swing.*;
import java.awt.*;
import java.util.Vector;

public class ListTest extends JFrame{
    public ListTest(){

        Container container = this.getContentPane();

        //生成列表的内容
        //String[] contents = {"1","2","3","4","5"};  //数组的静态初始化

        Vector contents = new Vector();  //动态添加内容

        contents.add("1");
        contents.add("2");
        contents.add("3");
        contents.add("4");
        contents.add("5");

        //列表中需要放入内容
        JList jList = new JList(contents);

        container.add(jList);

        this.setVisible(true);
        this.setBounds(200,200,300,500);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new ListTest();
    }
}

2.7.文本框

1.文本框

JTextField

import javax.swing.*;
import java.awt.*;

public class TextTest extends JFrame {
    public TextTest(){
        Container container = this.getContentPane();

        JTextField field1 = new JTextField("Hello");
        JTextField field2 = new JTextField("World");

        container.add(field1, BorderLayout.NORTH);
        container.add(field2, BorderLayout.SOUTH);

        this.setVisible(true);
        this.setBounds(300,300,500,500);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TextTest();
    }
}
2.密码框

JPasswordField

import javax.swing.*;
import java.awt.*;

public class PasswordFieldTest extends JFrame {
    public PasswordFieldTest(){
        Container container = this.getContentPane();

        JPasswordField passwordField = new JPasswordField();
        passwordField.setEchoChar('*');

        container.add(passwordField);

        this.setVisible(true);
        this.setBounds(300,300,500,500);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new PasswordFieldTest();
    }
}
3.文本域

JTextArea

import javax.swing.*;
import java.awt.*;

public class JScrollPanelTest extends JFrame {

    public JScrollPanelTest(){
        Container container = getContentPane();

        //文本域
        JTextArea textArea = new JTextArea();
        textArea.setText("这是一个文本域");

        //Scroll面板
        JScrollPane scrollPane = new JScrollPane(textArea);
        container.add(scrollPane);


        this.setVisible(true);
        this.setBounds(200,300,300,400);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new JScrollPanelTest();
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值