GUI基础编程

GUI基础编程 Day 05

swing

窗口 面板
import javax.swing.*;
import java.awt.*;

public class JFrameDemo01 {
    //init();  初始化
    public void init(){
        //顶级窗口
        JFrame jFrame = new JFrame("这是一个JFrame窗口!");
        jFrame.setVisible(true);
        jFrame.setBounds(200,200,500,500);
        jFrame.setBackground(Color.cyan);

        //设置文字  Jlabel
        JLabel jLabel = new JLabel("欢迎来到GUI基础!");

        jFrame.add(jLabel);

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

    public static void main(String[] args) {
        //建立一个窗口
        new JFrameDemo01().init();
    }
}

优化 标签居中

import javax.swing.*;
import java.awt.*;

public class JFrameDemo02 {
    public static void main(String[] args) {
        new MyJFrame02().init();
    }
}
class MyJFrame02 extends JFrame{
    public void init(){
        this.setVisible(true);
        this.setBounds(200,200,600,600);

        JLabel jLabel = new JLabel("居中");
        this.add(jLabel);
        
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        //让文本标签居中    设置水平对齐
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);

        //获得一个容器
        Container container = this.getContentPane();
        container.setBackground(Color.YELLOW);
    }
}
弹窗

注意:弹窗自带关闭事件,可以不写

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//主窗口
public class DialogDemo extends JFrame {
    public DialogDemo(){
        this.setVisible(true);
        this.setBounds(100,100,500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        //JFrame  放东西  容器
        Container container = this.getContentPane();
        //绝对布局
        container.setLayout(null);

        //按钮
        JButton button = new JButton("点击出现一个窗口!");  //创建一个对象
        button.setBounds(50,50,200,50);

        //点击按钮的时候出现一个弹窗   弹窗默认就有关闭事件
        button.addActionListener(new ActionListener() {   //监听事件
            @Override
            public void actionPerformed(ActionEvent e) {
                new MyDialog();
            }
        });
        container.add(button);
    }

    public static void main(String[] args) {
        new DialogDemo();
    }
}
class MyDialog extends JDialog{
    public MyDialog() {
        this.setVisible(true);
        this.setBounds(100,100,200,200);


        Container container = this.getContentPane();
        container.setLayout(null);

        container.add(new Label("第一个弹窗!"));
    }
}

问题:弹窗中的文字不显示!
修改:

class MyDialog extends JDialog{
    public MyDialog() {
        this.setVisible(true);
        this.setBounds(100,100,200,200);


        Container container = this.getContentPane();
        //container.setLayout(null);

        //container.add(new JLabel("第一个弹窗!"));
        JLabel label = new JLabel("第一个弹窗!");
        //1.居中
        //add(label);
        //label.setHorizontalAlignment(SwingConstants.CENTER);
        
        //2.
        container.add(label);
        label.setHorizontalAlignment(SwingConstants.CENTER);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

? Adore ?

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值