随笔 - 172  文章 - 0  评论 - 0  阅读 - 11939

Java GUI编程(二)Swing

一,窗口

 

二,弹窗

复制代码
public class DialogDemo extends JFrame {
    public DialogDemo(){
        this.setVisible(true);
        this.setSize(700,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        //JFrame放东西
        Container container = this.getContentPane();
        //绝对布局
        this.setLayout(null);
        //按钮
        JButton jButton = new JButton("点击弹出一个对话框");
        jButton.setBounds(30,30,200,50);
        container.add(jButton);
        //点击这个按钮的时候,弹出一个弹窗
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new MyDialogDemo();
            }
        });
    }
    public static void main(String[] args) {
        new DialogDemo();
    }
}
class MyDialogDemo extends JDialog{
    public MyDialogDemo(){
        this.setVisible(true);
        this.setBounds(100,100,500,500);
        //this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);默认就有关不用再写闭事件,

        Container container=this.getContentPane();
        container.add(new Label("快起来学习"));
    }
}
复制代码

 

三,标签

new JLabel("文字");

图标Icon

复制代码
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 jLabel = new JLabel("icontest",iconDemo,SwingConstants.CENTER);
        Container container = this.getContentPane();
        container.add(jLabel);
        this.setVisible(true);
        this.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;
    }
}
复制代码

 

四,面板

五,按钮

六,列表

七,文本框

 

posted on   键盘敲烂的朱  阅读(31)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示