刚学Swing,点按钮弹不出窗口。

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class myFrame extends JFrame{
public void firstFrame(){
JFrame jf=new JFrame();
Container container=jf.getContentPane();

    JLabel jl=new JLabel("nothing exist");
    jl.setHorizontalAlignment(SwingConstants.CENTER);
    container.add(jl);

    jf.setSize(300,500);
    jf.setVisible(true);
    jf.setBackground(Color.blue);
    container.setLayout(null);

    JButton jb=new JButton("show a button");
    jb.setBounds(100,10,100,50);
    jb.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            new myDialog().firstDialog(jf);
        }
    });
    container.add(jb);

    jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}

}
class myDialog extends JDialog{
public void firstDialog(JFrame frame){
JDialog jd=new JDialog(frame,"my first dialog",true);
Container container=jd.getContentPane();
container.add(new JLabel("star chatting"));
container.setBounds(120, 120, 100, 100);
container.setVisible(true);
container.setBackground(Color.black);
}
}
public class Test{
public static void main(String[] args){
new myFrame().firstFrame();
}
}
图片说明

运行之后弹出这个界面 点了button按钮弹不出框
刚学swing 还没学监听这方面 addActionListener方法是抄书里写的
求解决 谢谢。

你的监听添加没有问题,但是你在监听时间触发之后弹出对话框,对话框是生成了,但是你没有显示出来,也就是说少了一行代码。图片说明

http://tieba.baidu.com/p/2192881456