求助:为什么会碰到添加的JComponent一开始无法显示,但窗口一缩放就显示出来了的问题?? 比如说,我在一个JFrame里添加一个JButton: add(new JButton("button"));然后运行,发现这个button显示不出来,但是一单击窗口的边框,或者缩放一下,就立刻显示出来了!请问这是为什么啊?? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 加了之后调用JFrame的.validate()方法 应该是动态添加的或在主窗口paint后添加的,所以没有画来,窗口缩放时调用了repaint所以出来了。在主窗口paint后再画的控件要用validate强制刷新。 谢谢了!可是还有个问题..我是在主frame外的一个对话框里向主frame里添加component的,不知道怎样让主frame强制刷新啊? 还是一样的。只要有主frame的引用调用JFrame.validate();就可以了。 对啊import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Test{ public static void main(String[] args) { MFrame frame = new MFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }}class MFrame extends JFrame{ JButton button; MDialog dialog; public MFrame() { setSize(200, 200); dialog = new MDialog(this); getContentPane().setLayout(new FlowLayout()); button = new JButton("show Dialog"); getContentPane().add(button); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { dialog.setVisible(true); } }); } class MDialog extends JDialog { JFrame owner; public MDialog(JFrame owner) { super(owner, "MDialog", false); setSize(100, 100); this.owner = owner; JButton Add = new JButton("Add"); Add.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { (MDialog.this).owner.getContentPane().add(new JButton("new")); (MDialog.this).owner.validate(); } }); getContentPane().setLayout(new FlowLayout()); getContentPane().add(Add); setVisible(false); } }} 谢谢LS各位,可是问题就在这里啊,我想要进行validate操作的那个dialog的owner是另一个dialog,那个dialog的owner才是我的主frame,所以我怎么也不知道该怎样引用frame! 谢谢,其实我已经尽可能地让算法和代码简单了,我的原理就是首先在JFrame里启动一项操作,这个操作弹出了一个对话框,然后又弹出了一个对话框进行确认,等到确认完成后才进行下一步操作,也就是我说的在frame里添加一些components。我是打算把validate()放在最后一步的确认按钮的actionListener里面,不知这样有无不妥? ......绕口令一样import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Test{ public static void main(String[] args) { MFrame frame = new MFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }}class MFrame extends JFrame{ JButton button; MDialog dialog; public MFrame() { setSize(200, 200); dialog = new MDialog(this); getContentPane().setLayout(new FlowLayout()); button = new JButton("show MDialog"); getContentPane().add(button); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { dialog.setVisible(true); } }); }}class MDialog extends JDialog{ JFrame owner; YDialog dialog; public MDialog(JFrame owner) { super(owner, "M", false); setSize(100, 100); this.owner = owner; dialog = new YDialog(this); JButton Add = new JButton("show YDialog"); Add.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { dialog.setVisible(true); } }); getContentPane().setLayout(new FlowLayout()); getContentPane().add(Add); setVisible(false); }}class YDialog extends JDialog{ MDialog owner; public YDialog(MDialog owner) { super(owner, "Y", false); setSize(100, 100); this.owner = owner; JButton Add = new JButton("Add"); Add.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { (YDialog.this).owner.owner.getContentPane().add(new JButton("new")); (YDialog.this).owner.owner.validate(); } }); getContentPane().setLayout(new FlowLayout()); getContentPane().add(Add); setVisible(false); }} 谢谢,其实这个方法我刚才就根据你在5楼的代码写过了,编译器报的错误说,上面(引用)加粗的那个owner "cannot be resolved as a type".. 我的原理就是首先在JFrame里启动一项操作,这个操作弹出了一个对话框,然后又弹出了一个对话框进行确认,等到确认完成后才进行下一步操作,也就是我说的在frame里添加一些components。(YDialog.this).owner.owner.validate(); 怎么都乱糟糟的啊好好好设计下了 求个模拟DISCUZ论坛登录,发帖和回复的小程序 求解 请问重载构造函数时,怎么调用原来的构造函数? 关于ultraedit 对Hibernate性能优化的认识 请教两个基础问题,谢谢 java读C写的文件 多边形问题。 包引用的問題,這是為什麼? 取session中的数据,为何偶尔出现ClassCastException JComboBox 实现过滤功能?如何实现? 内容分页的问题
在主窗口paint后再画的控件要用validate强制刷新。
还是一样的。只要有主frame的引用调用JFrame.validate();就可以了。
对啊
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class Test
{
public static void main(String[] args)
{
MFrame frame = new MFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}class MFrame extends JFrame
{
JButton button;
MDialog dialog; public MFrame()
{
setSize(200, 200);
dialog = new MDialog(this);
getContentPane().setLayout(new FlowLayout());
button = new JButton("show Dialog");
getContentPane().add(button);
button.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent e)
{
dialog.setVisible(true);
}
});
}
class MDialog extends JDialog
{
JFrame owner;
public MDialog(JFrame owner)
{
super(owner, "MDialog", false);
setSize(100, 100);
this.owner = owner;
JButton Add = new JButton("Add");
Add.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent e)
{
(MDialog.this).owner.getContentPane().add(new JButton("new"));
(MDialog.this).owner.validate();
}
});
getContentPane().setLayout(new FlowLayout());
getContentPane().add(Add);
setVisible(false);
}
}
}
谢谢,其实我已经尽可能地让算法和代码简单了,我的原理就是首先在JFrame里启动一项操作,这个操作弹出了一个对话框,然后又弹出了一个对话框进行确认,等到确认完成后才进行下一步操作,也就是我说的在frame里添加一些components。
我是打算把validate()放在最后一步的确认按钮的actionListener里面,不知这样有无不妥?
绕口令一样
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class Test
{
public static void main(String[] args)
{
MFrame frame = new MFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}class MFrame extends JFrame
{
JButton button;
MDialog dialog; public MFrame()
{
setSize(200, 200);
dialog = new MDialog(this);
getContentPane().setLayout(new FlowLayout());
button = new JButton("show MDialog");
getContentPane().add(button);
button.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent e)
{
dialog.setVisible(true);
}
});
}
}class MDialog extends JDialog
{
JFrame owner;
YDialog dialog;
public MDialog(JFrame owner)
{
super(owner, "M", false);
setSize(100, 100);
this.owner = owner;
dialog = new YDialog(this);
JButton Add = new JButton("show YDialog");
Add.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent e)
{
dialog.setVisible(true);
}
});
getContentPane().setLayout(new FlowLayout());
getContentPane().add(Add);
setVisible(false);
}
}class YDialog extends JDialog
{
MDialog owner;
public YDialog(MDialog owner)
{
super(owner, "Y", false);
setSize(100, 100);
this.owner = owner;
JButton Add = new JButton("Add");
Add.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent e)
{
(YDialog.this).owner.owner.getContentPane().add(new JButton("new"));
(YDialog.this).owner.owner.validate();
}
});
getContentPane().setLayout(new FlowLayout());
getContentPane().add(Add);
setVisible(false);
}
}
谢谢,其实这个方法我刚才就根据你在5楼的代码写过了,编译器报的错误说,上面(引用)加粗的那个owner "cannot be resolved as a type"..
(YDialog.this).owner.owner.validate();
怎么都乱糟糟的啊
好好好设计下了