import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class myjdialog extends JDialog {
public myjdialog(){
super(new myframe(),"小窗体标题",true);
Container a=getContentPane();
a.add(new JLabel("小窗体内容"));
setSize(100,100);
}
public static void main(String[] args) {
new myjdialog();
}
}
class myframe extends JFrame{
public myframe(){
Container b=getContentPane();
b.setLayout(null);
JLabel c=new JLabel("在哪显示?");
c.setHorizontalAlignment(SwingConstants.CENTER);
b.add(c);
JButton d=new JButton("按钮");
d.setBounds(10,10,100,21);
d.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){new myjdialog().setVisible(true);}
});
b.add(d);
}
}
import java.awt.event.*;
import javax.swing.*;public class myjdialog extends JDialog {
public myjdialog(){
super(new myframe(),"小窗体标题",true);
Container a=getContentPane();
a.add(new JLabel("小窗体内容"));
setSize(100,100);
}
public static void main(String[] args) {
new myjdialog();
}
}
class myframe extends JFrame{
public myframe(){
Container b=getContentPane();
b.setLayout(null);
JLabel c=new JLabel("在哪显示?");
c.setHorizontalAlignment(SwingConstants.CENTER);
b.add(c);
JButton d=new JButton("按钮");
d.setBounds(10,10,100,21);
d.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){new myjdialog().setVisible(true);}
});
b.add(d);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new myframe().setVisible(true);
}
});
}
把下面的代码public static void main(String[] args) {
new myjdialog();
}
}替换成public static void main(String[] args) {
new myframe().setVisible(true);
}
窗体显示需要调用setVisible(true);来更新窗体的可视属性,默认是false不显示。
楼主的代码有很多小毛病,你的意图是点击按钮弹出对话框,可是主函数中你却直接new了个对话框,主函数中应该是让JFrame窗体显示,
应该new的是myframe并调用myframe的setVisible(true);才能让JFrame窗体显示,才会有按钮让你点。但是myjdialog构造函数中又new了个myframe,这个myframe毫无意义。JDialog没有JFrame也能new,也能显示。 要么不要myframe参数,要么在new myjdialog时传个this进去。新new个myframe是毫无意义的代码。