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 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?