y495711146 2015-03-11 06:13 采纳率: 0%
浏览 3122

java 为什么Dialog中的Label不显示?

为什么 在Dialog中add 一个Lable,但运行的时候并没有显示? 求大神

import java.awt.Button;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;

public class MyFrame
{
private Frame frame ;
private Button butCount;
private Button butClear;
private TextField tf ;
private TextArea ta ;
private TextField answer;

public void init()
{
    //定义组建
    frame = new Frame("统计代码行数软件--by Owen");
    butCount = new Button("统计");
    butClear = new Button("结果清零");
    tf = new TextField("请输入要计算的文件夹路径" , 60);
    tf.selectAll();     //设置将初始的"请输入要计算的文件夹路径"全选
    ta = new TextArea(25 , 75);
    answer = new TextField(40);

    //向面板添加组建
    frame.add(tf);
    frame.add(butCount);
    frame.add(butClear);
    frame.add(ta);
    frame.add(answer);

    //设置面板
    frame.setVisible(true);
    frame.setBounds(300,100,630,500);
    frame.setLayout(new FlowLayout());

    //引入事件
    eventFrame();
    eventButton();
    eventText();
}

//窗体事件
public void eventFrame()
{
    //添加面板监听器
    frame.addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent e)
        {
            System.exit(0);
        }
    }); 
}

//按钮事件
public void eventButton()
{
    //添加butCount监听器
    butCount.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e)
        {
            action();

        }
    });

    //添加butClear监听器
    butClear.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e)
        {
            Print.count = 0;
            ta.setText("");
            tf.setText("");
            answer.setText("");
        }
    });
}

//文本事件
public void eventText()
{
    tf.addKeyListener(new KeyAdapter(){
        public void keyPressed(KeyEvent e)
        {
            if(e.getKeyCode() == KeyEvent.VK_ENTER)
                action();
        }
    });

}

//操作部分
private void action()
{
    String address = tf.getText();
    //健壮性检验
    File check = new File(address);
    if( !(check.exists() && check.isDirectory() ))
        popDialog(address);

    String[] files = Print.traDir(address);
    for(int i=0 ; i<files.length ; i++)
    {
        ta.append(files[i]);
    }
    ta.append("\r\n");
    ta.append("\r\n");
    ta.append("----------------------------------------------");
    ta.append("\r\n");
    ta.append("\r\n");

    int count = Print.count;

    answer.setText("结果:  "+count+"   行代码");
}

//弹出对话框------------------这里没有弹出------------------?????????????????????
private void popDialog(String address)
{
    //定义弹出窗口口
    Dialog wrongDialog = new Dialog(frame,"地址错误提示", false);
    Label wrongLabel = new Label();
    Button wrongButton = new Button("确定");


    wrongDialog.add(wrongLabel);
    wrongDialog.add(wrongButton);


    wrongDialog.setBounds(320 , 250 , 600 , 150);
    wrongDialog.setLayout(new FlowLayout());
    wrongDialog.setVisible(true);
    String info = "输入地址 \" "+address+" \"有误,请重新输入";
    wrongLabel.setText(info);

    popEvent(wrongDialog ,wrongLabel ,wrongButton);

}

//弹出对话框 --监听器
private void popEvent(Dialog wrongDialog ,Label wrongLabel ,Button wrongButton)
{
    wrongDialog.addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent e)
        {
            wrongDialog.setVisible(false);
        }
    }); 

    wrongButton.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e)
        {
            wrongDialog.setVisible(false);
        }
    }); 
}

}

  • 写回答

1条回答

  • microhex 2015-03-11 07:05
    关注

    为什么我的可以弹出啊 来了来了。。。。图片说明

    评论

报告相同问题?

悬赏问题

  • ¥15 全志H618ROM新增分区
  • ¥20 jupyter保存图像功能的实现
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况