GUI编程-3

3.Swing

3.1.窗口

public class JFrameDemo {
    public static void main(String[] args) {
        new MyJFrame();
    }
}
class MyJFrame extends JFrame {
    public MyJFrame(){
        setBounds(200,200,200,200);
        setVisible(true);

        JLabel jLabel = new JLabel("欢迎你");
        add(jLabel);
        //让文本标签居中 设置水平对齐
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);

//        setBackground(Color.BLUE);在JFrame中不行
        //获得一个容器
        Container container = getContentPane();
        container.setBackground(Color.black);

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

3.2.弹窗

public class MyDialog extends JFrame {
    public void init(){
        setVisible(true);
        setBounds(200,200,200,200);

        Container container = getContentPane();
        container.setBackground(Color.BLUE);

        JButton jButton = new JButton("按钮");
        add(jButton);

        //点这个按钮的时候,弹出一个窗口
        jButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new MyJDialog();
            }
        });
    }
    
    //弹出的窗口
    private class MyJDialog extends JDialog{
        public MyJDialog(){
            setVisible(true);
            setBounds(200,200,200,200);
            Container contentPane = getContentPane();
            contentPane.setBackground(Color.BLACK);
        }
    }

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

3.3.标签

label

new JLabel("xxx");

图标icon

//图标接口,需要实现类,JFrame继承
public class IconDemo extends JFrame implements Icon {
    private int width;
    private int height;

    public IconDemo(){}

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

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

        Container contentPane = getContentPane();
        contentPane.add(label);
        setVisible(true);
        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, height);
    }

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

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

图片icon

public class IconDemo1 extends JFrame {
    public static void main(String[] args) {
        new IconDemo1();
    }
    public IconDemo1(){
        JLabel jLabel = new JLabel("abc");
        URL url = IconDemo1.class.getResource("1.jpg");
        ImageIcon imageIcon = new ImageIcon(url);
        int i =1;
        jLabel.setIcon(imageIcon);

        Container contentPane = getContentPane();
        contentPane.add(jLabel);
        setVisible(true);
        setBounds(200,200,200,200);
    }
}

在这里插入图片描述

3.4.面板

public class JPanelTest extends JFrame{
    public static void main(String[] args) {
        new JPanelTest();
    }
    public JPanelTest(){
        JPanel jPanel = new JPanel();
        JButton jButton = new JButton("我是一个按钮");
        jPanel.add(jButton);
        Container contentPane = getContentPane();
        contentPane.add(jPanel);
        setBounds(200,200,200,200);
        setVisible(true);
    }
}
public class JTextTest extends JFrame {
    public JTextTest(){
        Container container = getContentPane();

        //文本域
        JTextArea area = new JTextArea(10,10);
        area.setText("hello");

        //Scroll下拉面板
        JScrollPane pane = new JScrollPane(area);
        //pane.add(area);这样不行


        container.add(pane);

        setBounds(200,200,200,200);
        setVisible(true);
    }

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

3.5.按钮

图片按钮

public class TestIconButton extends JFrame {
    public TestIconButton(){
        Container container = getContentPane();

        JButton button = new JButton();

        URL url = TestIconButton.class.getResource("1.jpg");

        ImageIcon imageIcon = new ImageIcon(url);
        button.setIcon(imageIcon);
        
        container.add(button);
        setVisible(true);
        setBounds(200,200,200,200);
    }

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

单选按钮

public class TestIconButton extends JFrame {
    public TestIconButton(){
        Container container = getContentPane();

        //单选按钮
        JRadioButton radioButton1 = new JRadioButton();
        JRadioButton radioButton2 = new JRadioButton();

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

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

        setVisible(true);
        setBounds(200,200,200,200);
    }

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

在这里插入图片描述

多选按钮

public class TestIconButton extends JFrame {
    public TestIconButton(){
        Container container = getContentPane();
        container.setLayout(new FlowLayout());
        JCheckBox jCheckBox1 = new JCheckBox();
        JCheckBox jCheckBox2 = new JCheckBox();

        container.add(jCheckBox1);
        container.add(jCheckBox2);
        setVisible(true);
        setBounds(200,200,200,200);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

在这里插入图片描述

3.6.列表

下拉列表

public class TestComboBox extends JFrame {
    public static void main(String[] args) {
        new TestComboBox();
    }
    public TestComboBox(){
        Container container = getContentPane();
        JComboBox comboBox = new JComboBox();
        comboBox.addItem("准备上映");
        comboBox.addItem("已经上映");
        comboBox.addItem("上映完毕");

        container.add(comboBox);

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);
        setBounds(200,200,200,200);
    }
}

在这里插入图片描述

列表框

public class TestJlist extends JFrame {
    public TestJlist(){
        Container container = getContentPane();
        
        //生成列表的内容
        String[] sr = {"1","2","3"};
        //列表中需要放入内容
        JList list = new JList(sr);

        container.add(list);

        setVisible(true);
        setBounds(200,200,200,200);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

在这里插入图片描述

3.7.文本框

public class TestText extends JFrame {
    public static void main(String[] args) {
        new TestText();
    }
    public TestComboBox(){
        Container container = getContentPane();
        JTextField textField1 = new JTextField();
        JTextField textField2 = new JTextField();
        container.add(textField1,BorderLayout.NORTH);
        container.add(textField2,BorderLayout.SOUTH);

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);
        setBounds(200,200,200,200);
    }
}
public class JTextTest extends JFrame {
    public JTextTest(){
        Container container = getContentPane();

        //文本域
        JTextArea area = new JTextArea(10,10);
        area.setText("hello");

        //Scroll下拉面板
        JScrollPane pane = new JScrollPane(area);
        //pane.add(area);这样不行


        container.add(pane);

        setBounds(200,200,200,200);
        setVisible(true);
    }

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

3.8.密码框

public class TestComboBox extends JFrame {
    public static void main(String[] args) {
        new TestPW();
    }
    public TestPW(){
        Container container = getContentPane();
        
        JPasswordField passwordField = new JPasswordField();
        //密码框默认为..... 也可通过passwordField.setEchoChar('*');手动设置为*
        
        container.add(passwordField);

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);
        setBounds(200,200,200,200);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值