weixin_44372600 2019-11-21 21:35 采纳率: 0%
浏览 220

新人用awt做图形化界面遇到点问题,求大佬给解决解决

这个代码没问题但就是结果和我想得不一样,第二张图片我必须得点击一下关闭键才会出现标签和文本框,为什么啊?求大佬解决啊啊啊啊!!!!!!

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

public class Demo01 extends JDialog {
public Demo01(JFrame jFrame){
super(jFrame,"ss",true);
this.setBounds(100,200,100,200);
this.setLayout(new FlowLayout());
this.setBackground(Color.white);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setVisible(true);

this.add(new Label("zhanghu"));
this.add(new TextField(""));
}

public static void main(String[] args) {
        JFrame jFrame=new JFrame("en");
        jFrame.setBounds(100,100,100,100);
        jFrame.setLayout(new FlowLayout());
        jFrame.setBackground(Color.white);
        jFrame.add(new Label("hh"));
        jFrame.add(new TextField(" "));
        Button button=new Button("dianji");
        jFrame.add(button);
        jFrame.setVisible(true);
        jFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        button.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                Demo01 demo01=new Demo01(jFrame);
                demo01.setVisible(true);
            }
        });
    }
}

图片说明

  • 写回答

1条回答 默认 最新

  • 程序员刘极渺 2024-03-21 19:19
    关注
        public Demo01(JFrame jFrame){
            super(jFrame,"ss",true);
            this.setBounds(100,200,100,200);
            this.setLayout(new FlowLayout());
            this.setBackground(Color.white);
            this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
            
    //     这里显示对话框的时候,你的label和textField还没有添加到Frame中,所以看不到
    //        this.setVisible(true);
            
            this.add(new Label("zhanghu"));
            this.add(new TextField(""));
        }
    
    
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵