Java-GUI之Swing

什么是Swing?

  • Java中的AWT提供了很多的Windows函数,所以又被称为重量级组件。而Swing是一种轻量级的图形界面组件,使用Swing可以更简单更快速的形成图形界面。
  • Swing中的所有组件都是从JComponent中扩展出来的。
  • 演示:创建一个窗体
public class JFrameTest {
    public static void main(String[] args) {
        JFrame jFrame = new JFrame();
        jFrame.setBounds(10,10,400,400);
        jFrame.setBackground(Color.CYAN);
        jFrame.setVisible(true);
        jFrame.setLayout(new FlowLayout());
        //添加标签和按钮
        jFrame.add(new Label("HelloJFrame!"));
        jFrame.add(new Button("btn-1"));
        //jframe中提供了可以关闭窗体的方法
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }
}

程序结果:
在这里插入图片描述

在Swing中添加组件

  • 添加一个文字居中的标签
  • 演示:
public class JlableTest {
    public static void main(String[] args) {
        JFrame jFrame = new JFrame();
        jFrame.setBounds(10,10,400,400);
        jFrame.setVisible(true);
        JLabel jLabel = new JLabel("这是一个标签");
        jFrame.add(jLabel);
        //调用设置标签内容位置的方法,传入参数
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);
        //关闭窗体
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

程序结果:
在这里插入图片描述

  • 在窗体中添加弹窗
    • 弹窗要使用JDialog类
    • 演示:
public class DialogTest {
    public static void main(String[] args) {
        JFrame jFrame = new JFrame();
        jFrame.setBounds(10, 10, 400, 400);
        jFrame.setVisible(true);
        //给窗体中添加组件
        JButton jButton = new JButton("点击按钮");
        jButton.setBounds(20, 20, 100, 100);
        jFrame.add(jButton);
        //给按钮添加监听事件
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //弹出弹窗
                JDialog jDialog = new JDialog();
                jDialog.setBounds(10, 10, 300, 300);
                jDialog.setBackground(Color.CYAN);
                jDialog.setVisible(true);
                //在弹窗中添加标签
                JLabel jLabel = new JLabel("弹窗弹出了");
                //设置标签中的内容居中
                jLabel.setHorizontalAlignment(SwingConstants.CENTER);
                jDialog.add(jLabel);
            }
        });
        //关闭窗体
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

程序结果:
在这里插入图片描述

  • 给标签添加图标
    • 要使用标签就要实现Icon接口,实现接口后要重写画图标方法以及返回图标的长宽方法
    • 演示:
public class IconTest implements Icon {
    private int width;
    private int height;

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

    public static void main(String[] args) {
        JFrame jFrame = new JFrame();
        jFrame.setBounds(10,10,400,400);
        jFrame.setVisible(true);
        //声明容器添加组件
        Container container = jFrame.getContentPane();
        IconTest icon = new IconTest(20, 20);
        JLabel jLabel = new JLabel("这是一个图标",icon,SwingConstants.CENTER);
        container.add(jLabel);
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

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

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

程序结果:
在这里插入图片描述

  • 在按钮中添加图标
    • 先把图片变为图标,然后添加到按钮中
  • 演示:
public class ButtonIcon {
    public static void main(String[] args) {
        JFrame jFrame = new JFrame();
        jFrame.setBounds(10,10,400,400);
        jFrame.setVisible(true);
        //声明要添加的图片
        ImageIcon imageIcon = new ImageIcon(ButtonIcon.class.getResource("th.jpg"));
        JButton jButton = new JButton();
        jButton.setIcon(imageIcon);
        //把按钮添加到组件中
        Container container = jFrame.getContentPane();
        container.add(jButton);
        container.setLayout(new FlowLayout());
        //关闭窗体
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

程序结果:
在这里插入图片描述

  • 在JFrame中添加面板—JPanel
    • 演示:
public class JPanelTest {
    public static void main(String[] args) {
        JFrame jFrame = new JFrame();
        jFrame.setBounds(10,10,400,400);
        //声明组件
        Container container = jFrame.getContentPane();
        JPanel jPanel = new JPanel(new GridLayout(2,2));
        jPanel.add(new JButton("btn-1"));
        jPanel.add(new JButton("btn-2"));
        jPanel.add(new JButton("btn-3"));
        jPanel.add(new JButton("btn-4"));
        container.add(jPanel);
        //关闭窗体
        jFrame.setVisible(true);
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

程序结果:
在这里插入图片描述

  • 注意最好形成习惯把setvisiable放在最后边,这样可以避免需要改变窗口的大小,窗体中的组件才会展示。
  • 添加单选按钮,多选框
    • 添加单选按钮的特殊之处在于要把所有的按钮放在ButtonGroup中,多选框则是放置在JCheckBox中
    • 演示:
public class RaidoButtonTest {

    public static void main(String[] args) {
        JFrame jFrame = new JFrame();
        jFrame.setBounds(10,10,400,400);
        //声明组件
        Container container = jFrame.getContentPane();
        //声明单选按钮
        JRadioButton button1 = new JRadioButton("btn-1");
        JRadioButton button2 = new JRadioButton("btn-2");
        JRadioButton button3 = new JRadioButton("btn-3");
        //因为要实现单选,所以把他们放在一个按钮组中
        ButtonGroup group = new ButtonGroup();
        group.add(button1);
        group.add(button2);
        group.add(button3);
        container.add(button1,BorderLayout.NORTH);
        container.add(button2,BorderLayout.CENTER);
        container.add(button3,BorderLayout.SOUTH);
        //设置可见以及关闭窗体
        jFrame.setVisible(true);
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

程序结果:
在这里插入图片描述

  • 添加下拉栏–使用JComboBox
    • 使用JComboBox中的addItem可以添加任意类型的数据
    • 演示:
public class ComBoxTest {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setBounds(10,10,400,400);
        Container container = frame.getContentPane();
        JComboBox comboBox = new JComboBox();
        comboBox.setSize(100,300);
        comboBox.addItem("1");
        comboBox.addItem("2");
        comboBox.addItem("3");
        container.add(comboBox);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

程序结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值