import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyDialog extends JDialog
{
public MyDialog(){
super(new MyFrame(),"第一个Dialog窗体",true);
Container container=getContentPane();
container.add(new JLabel("这是一个对话框"));
setSize(100,100);
}
public static void main(String args[]){
new MyDialog();
}
}
class MyFrame extends JFrame
{
public MyFrame(){
Container container=getContentPane();
container.setLayout(null);
JLabel jl=new JLabel("这是一个JFrame窗体");
jl.setHorizontalAlignment(SwingConstants.CENTER);
container.add(jl);
setVisible(true);
setBackground(Color.white);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JButton bt=new JButton("弹出对话框");
bt.setBounds(10,10,100,21);
bt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
new MyDialog().setVisible(true);
}
});
container.add(bt);
setSize(500,500);
}}
请问各位前辈为什么这个标签JLabel jl=new JLabel("这是一个JFrame窗体");为什么不能在Myframe中显示?
调试很久都不可以,望指点,不胜感激
import java.awt.event.*;
import javax.swing.*;
public class MyDialog extends JDialog
{
public MyDialog(){
super(new MyFrame(),"第一个Dialog窗体",true);
Container container=getContentPane();
container.add(new JLabel("这是一个对话框"));
setSize(100,100);
}
public static void main(String args[]){
new MyDialog();
}
}
class MyFrame extends JFrame
{
public MyFrame(){
Container container=getContentPane();
container.setLayout(null);
JLabel jl=new JLabel("这是一个JFrame窗体");
jl.setHorizontalAlignment(SwingConstants.CENTER);
container.add(jl);
setVisible(true);
setBackground(Color.white);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JButton bt=new JButton("弹出对话框");
bt.setBounds(10,10,100,21);
bt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
new MyDialog().setVisible(true);
}
});
container.add(bt);
setSize(500,500);
}}
请问各位前辈为什么这个标签JLabel jl=new JLabel("这是一个JFrame窗体");为什么不能在Myframe中显示?
调试很久都不可以,望指点,不胜感激
解决方案 »
- 请大家帮忙看看!用swing的做机车周转图可行吗?大家帮忙说说
- 求Java班级管理信息系统
- 大家连接数据库用的数据库连接池是什么呢。谢谢
- JComboBox.addItem("name");<option value="csdn">name</option>
- 求Eclipse如何汉化
- 新手请教,我的applet没东西显示
- java如何巩固
- 紧急求救!!!!高手,高手,高手请进!
- ★公告:申请子版斑竹具体方法!!!
- 谁有JBuilder mobileset2.0 的license key?
- 新鲜出炉的“俄罗斯方块”,欢迎大家下载试玩!
- 又遇到时间转换问题2009-09-02T01:01:49.093750Z
import java.awt.event.*;
import javax.swing.*;
public class MyDialog extends JDialog
{
public MyDialog(){
super(new MyFrame(),"第一个Dialog窗体",true);
Container container=getContentPane();
container.add(new JLabel("这是一个对话框"));
setSize(100,100);
}
public static void main(String args[]){
new MyDialog();
}
}
class MyFrame extends JFrame
{
public MyFrame(){
Container container=getContentPane();
container.setLayout(null);
JLabel jl=new JLabel("这是一个JFrame窗体");
jl.setHorizontalAlignment(SwingConstants.CENTER);
jl.setBounds(60, 60, 150, 100);
container.add(jl);
setVisible(true);
setBackground(Color.white);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JButton bt=new JButton("弹出对话框");
bt.setBounds(10,10,100,21);
bt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
new MyDialog().setVisible(true);
}
});
container.add(bt);
setSize(500,500);
} }
给你加 了一行,你看看 对不。
把这个去了
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyDialog extends JDialog
{
public MyDialog(JFrame owner){
super(owner,"第一个Dialog窗体",true);
Container container=getContentPane();
container.add(new JLabel("这是一个对话框"));
setSize(100,100);
}
public static void main(String args[]){
new MyFrame();
}
}
class MyFrame extends JFrame
{
private JFrame thisframe = this;
public MyFrame(){
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container=getContentPane();
container.setBackground(Color.white);
container.setLayout(new FlowLayout()); //用null必须给每个控件定位,不然就重叠了
JLabel jl=new JLabel("这是一个JFrame窗体");
jl.setHorizontalAlignment(SwingConstants.CENTER);
container.add(jl);
JButton bt=new JButton("弹出对话框");
bt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
new MyDialog(thisframe).setVisible(true);
}
});
container.add(bt);
setSize(500,500);
setVisible(true);
}
}