import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class MyJDialog extends JDialog {
public MyJDialog(MyFrame frame) {
super(frame, "第一个JDialog窗体", 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() {
Container container = getContentPane();
container.setLayout(null);
JButton bl = new JButton("弹出对话框");
bl.setBounds(10, 10, 100, 21);
bl.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new MyJDialog(MyFrame.this).setVisible(true);
}
});
container.add(bl);
JLabel jl = new JLabel("这是一个JFrame窗体");
jl.setHorizontalAlignment(SwingConstants.CENTER);
container.add(jl);
container.setBackground(Color.white);
setSize(200, 200);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setVisible(true);
}
}
这里添加的标签没有显示出来,为什么呢?
------解决方案--------------------
- Java code
container.setLayout(null); //把这句话删掉
------解决方案--------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class MyJDialog extends JDialog {
public MyJDialog(MyFrame frame) {
super(frame, "第一个JDialog窗体", true);
Container container = getContentPane();
container.add(new JLabel("这是一个对话框"));
setBounds(120, 120, 100, 100);
}
}
public class MyFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 8904641527445622030L;
public static void main(String args[]) {
new MyFrame();
}
public MyFrame() {
Container container = getContentPane();
container.setLayout(null);
JButton bl = new JButton("弹出对话框");
bl.setBounds(10, 10, 100, 21);
bl.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new MyJDialog(MyFrame.this).setVisible(true);
}
});
container.add(bl);
JLabel jl = new JLabel("这是一个JFrame窗体");
//jl.setHorizontalAlignment(SwingConstants.CENTER);
jl.setBounds(10,30,150,30);
container.add(jl);
container.setBackground(Color.white);
setSize(200, 200);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setVisible(true);
}
}
好了,楼主给分