导航

JDialog窗体

Posted on 2016-05-22 15:16  4565892  阅读(289)  评论(0)    收藏  举报

class MyJDialog extends JDialog{
    public MyJDialog(JFrame frame){
    super(frame,"第一个Dialog窗体",true);
    Container container=getContentPane();
    container.add(new JLabel("这是一个对话框"));
    setBounds(120, 120, 100, 100);
    }    
}
public class MyFrame extends JFrame{
    public static void main(String[] args) {
        new MyFrame();
    }
    public MyFrame()
    {
        JFrame jf=new JFrame("JFrame窗体");
        Container container=jf.getContentPane();
        container.setLayout(null);
        JLabel ji=new JLabel("这是一个JFrame窗体");
        //将标签的文字至于标签中间
        ji.setHorizontalAlignment(SwingConstants.CENTER);
        container.add(ji);
        JButton bl=new JButton("弹出对话框");
        bl.setBackground(Color.blue);
        bl.setBounds(10, 10, 100, 21);
        bl.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                new MyJDialog(MyFrame.this).setVisible(true);
                
            }
        });
        container.add(bl);
        jf.setVisible(true);
        jf.setSize(200,150);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        
        
    }

}