Java基础之Swing组件的应用

1 篇文章 0 订阅

Java基础之Swing组件的应用

不知道大家是否用过swing这个包下面的各类组件,swing包出现于jdk1.2之后,如果大家想要更清楚了解swing的用法的话,推荐打开jdk下的lib库函数,在javax下的swing包中包含了Java组件的窗口,布局,常用组件,各类事件,对话框等工具类,大家可以逐一测试。

Java swing,swing与awt都是用于GUI设计,swing功能更为强大。

1. 窗口(Window)

  • 普通窗口JFrame(继承Java.awt.Frame)
public class JFrameTest {
    public static void main(String[] args) {
        JFrame window=new JFrame("第一个窗口");//创建窗口
        window.setBounds(100,100,200,300);//设置窗口位置与大小
        window.setVisible(true);//设置窗口可见,默认不可见
        window.setDefaultCloseOperation(2);//设置关闭按钮事件
    }
}

演示图片:
在这里插入图片描述

  • 对话框窗口JDialog(继承Java.awt.Dialog)
public class JDialog {
    public static void main(String[] args) {
        JOptionPane.showMessageDialog(null,"输入非法字符","警告",2);
    }
}

演示图片:
在这里插入图片描述

2. 布局(Layout)

  • FlowLayout() 流布局
    一行一行排列组件,居中对齐
public class FlowWindow extends JFrame implements Serializable {
    private static final long serialVersionUID = -4459293489198922890L;
    FlowWindow(){
        setLayout(new FlowLayout());
        setVisible(true);
        setDefaultCloseOperation(2);
        setBounds(100,100,500,400);
        setTitle("流式布局");
    }
    public static void main(String[] args) {
        FlowWindow flowWindow=new FlowWindow();
        flowWindow.add(new JButton("登录"));
        flowWindow.add(new JButton("注册"));
        JButton jButton=new JButton("提交");
        jButton.setPreferredSize(new Dimension(20,20));
        jButton.setBackground(Color.cyan);
        flowWindow.add(jButton);
    }
}

示例(居中从左到右排列,组件间隙5px):
在这里插入图片描述

  • BoarderLayout()边框布局
    布局为东西南北中
public class BoardWindow extends JFrame implements Serializable {
    private static final long serialVersionUID = 629223360093875012L;
    BoardWindow(String s){
        setTitle(s);
        setLayout(new BorderLayout());
        setVisible(true);
        setDefaultCloseOperation(2);
        setBounds(100,100,500,400);
    }

    public static void main(String[] args) {
        BoardWindow w=new BoardWindow("边框式布局");
        w.add(new JButton("上"),BorderLayout.NORTH);
        w.add(new JButton("下"),BorderLayout.SOUTH);
        w.add(new JButton("左"),BorderLayout.WEST);
        w.add(new JButton("右"),BorderLayout.EAST);
        JButton jButton=new JButton("中");
        jButton.setBackground(Color.cyan);
        w.add(jButton,BorderLayout.CENTER);
    }
}

示例:
在这里插入图片描述

  • CardLayout()卡牌布局
    扑克牌式层叠
public class CardWindow extends JFrame implements Serializable {
    private static final long serialVersionUID = 722813939516557893L;
    CardWindow(){
        setLayout(new CardLayout());
        setVisible(true);
        setDefaultCloseOperation(2);
        setBounds(100,100,500,400);
    }
    public static void main(String[] args) {
        CardWindow window=new CardWindow();
        JButton button=new JButton("表层");
        window.add(button);
        window.add(new JTextField("底层",20));
    }
}

示例:
在这里插入图片描述

  • GridLayout()网格布局
    一格一格的布局
public class GridWindow extends JFrame implements Serializable {
    private static final long serialVersionUID = -1838053931324203836L;
    GridWindow(){
        setDefaultCloseOperation(2);
        setLayout(new GridLayout(12,12));
        setVisible(true);
        setBounds(100,100,500,400);
    }

    public static void main(String[] args) {
        GridWindow gridWindow=new GridWindow();
        Label label[][]=new Label[12][12];
        for (int i=0;i<12;i++){
            for(int j=0;j<12;j++){
                label[i][j]=new Label();
                if((i+j)%2==0)label[i][j].setBackground(Color.white);
                else label[i][j].setBackground(Color.BLACK);
                gridWindow.add(label[i][j]);
            }
        }
    }
}

示例:
在这里插入图片描述

  • nullLayout()空布局

在空布局中可以任意调整组件位置

public class NullWindow extends JFrame implements Serializable {
    private static final long serialVersionUID = 4781049511712375358L;
    NullWindow(){
        setLayout(null);
        setVisible(true);
        setDefaultCloseOperation(2);
        setBounds(100,100,500,400);
    }

    public static void main(String[] args) {
        NullWindow window=new NullWindow();
        JTextField textField=new JTextField("文本域");
        textField.setBounds(30,30,90,40);
        JButton jButton=new JButton("确认");
        jButton.setBounds(100,100,60,30);
        window.add(textField);
        window.add(jButton);
    }
}

在这里插入图片描述

  • BoxLayout()盒子布局
    盒子嵌套内布局
public class BoxWindow extends JFrame implements Serializable {
    private static final long serialVersionUID = -2234324123411861565L;
    BoxWindow(){
        setLayout(new FlowLayout());
        setVisible(true);
        setDefaultCloseOperation(2);
        setBounds(100,100,500,400);
    }
    public static void main(String[] args) {
        BoxWindow boxWindow=new BoxWindow();
        Box b1,b2,b3;
        b1=Box.createHorizontalBox();
        b2=Box.createVerticalBox();
        b3=Box.createVerticalBox();
        b2.add(new Label("name"));
        b2.add(new Label("password"));
        b3.add(new JTextField(10));
        b3.add(new JTextField(10));
        b1.add(b2);
        b1.add(Box.createHorizontalStrut(10));//行距
        b1.add(b3);
    }
}

3. 组件(Component)

Jbutton,JTextField,JLabel,Combox,JTable,JTree,JPasswordField,JRadioButton,JTextArea,JCheckBox

4. 面板(Panel)

JPanel(普通窗格)
JTabbedPanel(选项卡窗格)
JScrollPane(滚动窗格)
JSplitPane(c拆分窗格)
JLayeredPane(分层窗格)

4. 事件(Event)

ActionEvent
ItemEvent
DocumentEvent
MouseEvent
FocusEvent
KeyEvent
WindowEvent
EnnoymousEvent

Java swing实战训练:图书信息管理系统(桌面类应用程序)
这里为github链接地址:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值