GUI——Swing

Swing

1、窗口(JFrame)

public class JFrameDemo {

    // init() 初始化
    public void init(){
        // 顶级窗口
        JFrame frame = new JFrame("这是一个JFrame窗口");
        frame.setVisible(true);
        frame.setBounds(100,100,500,500);

        // 设置文字 JLabel
        JLabel jLabel = new JLabel("hello,world");
        jLabel.setBackground(Color.red);
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);//设置水平对齐
        frame.add(jLabel);

        // 获得一个容器
        Container contentPane = frame.getContentPane();
        contentPane.setBackground(Color.yellow);

        //关闭事件
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new JFrameDemo().init();
    }
}
  • 标签居中

    xxx.setHorizontalAlignment(SwingConstants.CENTER);//设置水平对齐
    

2、弹窗(JDialog)

  • 弹窗中,默认有窗口关闭事件。
/**
 * 主窗口
 */
public class DialogDemo {

    public void init(){
        JFrame frame = new JFrame();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setBounds(100,100,700,500);

        //获取容器
        Container contentPane = frame.getContentPane();
        //绝对布局
        contentPane.setLayout(null);

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

        contentPane.add(button);

    }

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

/**
 *  弹窗的窗口
 */
class MyDialog extends JDialog{
    public MyDialog(){
        this.setVisible(true);
        this.setBounds(100,100,400,400);

        //获得容器
        Container container = this.getContentPane();
        container.setLayout(null);
        container.add(new Label("hello!"));
    }
}

3、标签(JLabel)

JLabel

JLabel jLabel = new JLabel("hello,world");
jLabel.setBackground(Color.red);
jLabel.setHorizontalAlignment(SwingConstants.CENTER);//居中

图标(Icon)

public class IconDemo {
    MyFrame frame = null;

    IconDemo(){
        frame = new MyFrame();
        frame.setBounds(100,100,500,500);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    class MyFrame extends JFrame{

        MyIcon icon = null;

        MyFrame(){
            icon = new MyIcon();
            icon.init();
        }

        /**
         * 图标,需要实现类,JFrame继承
         */
        class MyIcon implements Icon{

            private int width;
            private int height;

            public MyIcon(){}

            public MyIcon(int width,int height){
                this.width = width;
                this.height = height;
            }

            public void init(){
                MyIcon icon = new MyIcon(15, 15);
                //图标可以放在标签上,也可以放在按钮上。
                JLabel label = new JLabel("icontest",icon,SwingConstants.CENTER);

                Container contentPane = getContentPane();
                contentPane.add(label);
            }

            @Override
            public void paintIcon(Component c, Graphics g, int x, int y) {
                g.fillOval(x,y,this.width,this.height);
            }

            @Override
            public int getIconWidth() {
                return this.width;
            }

            @Override
            public int getIconHeight() {
                return this.height;
            }
        }
    }

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

图片(Image)

public class MyImageIcon {

    MyFrame frame = null;

    MyImageIcon(){
        frame = new MyFrame();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setBounds(100,100,500,500);
    }

    class MyFrame extends JFrame{

        MyImage image = null;

        MyFrame(){
            image = new MyImage();
            image.init();
        }

        class MyImage implements Icon{

            private int width;
            private int height;
            MyImage image = null;

            MyImage(){}

            MyImage(int width,int height){
                this.width = width;
                this.height = height;
            }

            public void init(){
                image = new MyImage(100,100);
                JLabel label = new JLabel("hello!", image, SwingConstants.CENTER);
                Container contentPane = getContentPane();
                contentPane.add(label);
            }

            @Override
            public void paintIcon(Component c, Graphics g, int x, int y) {
                Image ima = (new ImageIcon("图片地址")).getImage();
                g.drawImage(ima,x,y,this.width,this.height,null);
            }

            @Override
            public int getIconWidth() {
                return this.width;
            }

            @Override
            public int getIconHeight() {
                return this.height;
            }
        }

    }

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

4、面板(JPanel)

普通面板

public class JPanelTest {

    JPanelTest(){
        new MyFrame();
    }

    class MyFrame extends JFrame{
        MyFrame(){
            this.setVisible(true);
            this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            this.setSize(500,500);

            Container contentPane = this.getContentPane();
            contentPane.setLayout(new GridLayout(2,2,10,10));//两行一列,上下左右间距为 10

            JPanel panel1 = new JPanel(new GridLayout(1,3));
            JPanel panel2 = new JPanel(new GridLayout(1,2));
            JPanel panel3 = new JPanel(new GridLayout(3,1));
            JPanel panel4 = new JPanel(new GridLayout(2,2));
            panel1.add(new JButton("1"));
            panel1.add(new JButton("1"));
            panel1.add(new JButton("1"));
            panel2.add(new JButton("2"));
            panel2.add(new JButton("2"));
            panel3.add(new JButton("3"));
            panel3.add(new JButton("3"));
            panel3.add(new JButton("3"));
            panel4.add(new JButton("4"));
            panel4.add(new JButton("4"));
            panel4.add(new JButton("4"));
            panel4.add(new JButton("4"));

            contentPane.add(panel1);
            contentPane.add(panel2);
            contentPane.add(panel3);
            contentPane.add(panel4);

        }
    }

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

文本域 JScroll 面板

public class JScrollDemo extends JFrame {
    JScrollDemo(){
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setBounds(100,100,300,350);

        Container container = this.getContentPane();

        //文本域
        TextArea textArea = new TextArea(20, 50);//宽 高
        textArea.setText("hello,world!");

        //JScroll 面板
        JScrollPane scrollPane = new JScrollPane(textArea);

        container.add(scrollPane);
    }

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

5、按钮(JButton)

图片按钮

public class JButtonDemo extends JFrame {

    public JButtonDemo(){
        Container contentPane = this.getContentPane();

        //将一个图片变成图标
        URL url = JButtonDemo.class.getResource("图标1.png");
        Icon icon = new ImageIcon(url);
        //将图标放在按钮上
        JButton button = new JButton(icon);
        button.setToolTipText("图片按钮");

        contentPane.add(button);

        this.setVisible(true);
        this.setSize(600,600);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

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

单选按钮(JRadioButton)

  • 单选按钮只能选择一个,一般会分组,一个组中只能选择一个
/**
 * 单选框
 */
public class JButtonDemo02 extends JFrame {

    public JButtonDemo02(){
        Container contentPane = this.getContentPane();

        //单选框
        JRadioButton jRadioButton1 = new JRadioButton("JRadioButton01");
        JRadioButton jRadioButton2 = new JRadioButton("JRadioButton02");
        JRadioButton jRadioButton3 = new JRadioButton("JRadioButton03");

        //单选框只能选择一个,一般会分组,一个组中只能选择一个
        ButtonGroup group = new ButtonGroup();
        group.add(jRadioButton1);
        group.add(jRadioButton2);
        group.add(jRadioButton3);

        this.setLayout(new GridLayout(1,3));

        contentPane.add(jRadioButton1);
        contentPane.add(jRadioButton2);
        contentPane.add(jRadioButton3);

        this.setVisible(true);
        this.setSize(600,600);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

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

复选按钮(多选按钮 JCheckBox)

/**
 * 多选框
 */
public class JButtonDemo03 extends JFrame {

    public JButtonDemo03(){
        Container contentPane = this.getContentPane();

        //多选框
        JCheckBox jCheckBox1 = new JCheckBox("jCheckBox1");
        JCheckBox jCheckBox2 = new JCheckBox("jCheckBox2");
        JCheckBox jCheckBox3 = new JCheckBox("jCheckBox3");


        contentPane.add(jCheckBox1);
        contentPane.add(jCheckBox2);
        contentPane.add(jCheckBox3);

        this.setLayout(new GridLayout(1,3));

        contentPane.add(jCheckBox1);
        contentPane.add(jCheckBox2);
        contentPane.add(jCheckBox3);

        this.setVisible(true);
        this.setSize(600,600);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

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

6、列表(JCombobox)

下拉框

public class JComboboxDemo extends JFrame {

    JComboboxDemo(){
        Container contentPane = this.getContentPane();

        JComboBox jComboBox = new JComboBox();

        jComboBox.addItem(null);
        jComboBox.addItem("正在热映");
        jComboBox.addItem("即将上架");
        jComboBox.addItem("已下架");

        contentPane.add(jComboBox);

        this.setVisible(true);
        this.setSize(500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

列表框(JList )

public class JListDemo extends JFrame {
    JListDemo(){
        Container contentPane = this.getContentPane();

        //生成列表内容
        String[] contents = {"1","2","3"};
        //创建列表,放入内容
        JList jList = new JList(contents);

        contentPane.add(jList);

        this.setVisible(true);
        this.setSize(500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

应用场景

  • 下拉框:选择地区,或者一些单个选项,
  • 列表框:展示信息,一般是动态扩容的。

7、文本框(JTextField)

文本框

public class JTextFieleDemo extends JFrame {
    JTextFieleDemo(){
        Container contentPane = this.getContentPane();

        JTextField jTextField1 = new JTextField("hello");
        JTextField jTextField2 = new JTextField("world",20);

        this.setLayout(new GridLayout(2,1));

        contentPane.add(jTextField1);
        contentPane.add(jTextField2);

        this.setVisible(true);
        this.setSize(500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

密码框

public class JPasswordFieldTest extends JFrame {
    JPasswordFieldTest(){
        Container contentPane = this.getContentPane();

        //设置密码框
        JPasswordField jPasswordField = new JPasswordField();//默认就是 *
        jPasswordField.setEchoChar('*');

        contentPane.add(jPasswordField);

        this.setVisible(true);
        this.setSize(500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

文本域

  • 通常配合 JScroll 面板使用
public class JTextAreaDemo extends JFrame {

    JTextAreaDemo(){
        Container contentPane = this.getContentPane();

        //文本域
        JTextArea jTextArea = new JTextArea(20,50);//设置大小为20行,50列

        //Scroll面板
        JScrollPane jScrollPane = new JScrollPane(jTextArea);
        contentPane.add(jScrollPane);

        this.setVisible(true);
        this.setSize(500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值