Java 调用setVisible(false)后,当调用setVisible(true)时,我的JFrame内容消失
我正在设计一个绘图程序(用Java)来绘制文本。因为我用kinect做这件事,所以我想用一个我已经找到的屏幕键盘。这个键盘基本上是一个JFrame,里面有很多组件,我不想太详细了Java 调用setVisible(false)后,当调用setVisible(true)时,我的JFrame内容消失,java,swing,jframe,Java,Swing,Jframe,我正在设计一个绘图程序(用Java)来绘制文本。因为我用kinect做这件事,所以我想用一个我已经找到的屏幕键盘。这个键盘基本上是一个JFrame,里面有很多组件,我不想太详细了 public MainFrame() { super("Draw"); setLayout(new BorderLayout()); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLoca
public MainFrame() {
super("Draw");
setLayout(new BorderLayout());
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null); //so basically some standard stuff
makeGUI();
this.keyboard = new start_vk(); //strange name for a class but I didn't make it
}
在start_vk中,我可以调用setVisible(true),它可以工作,但我希望以后只有在需要时才调用它。。。现在我在某个地方调用setVisible(true),只有JFrame出现,其中没有任何组件
因为start_vk的构造函数是用SwingUtilities.invokeLater()完成的,所以我应该在一个单独的线程中调用它吗?还是其他建议
public start_vk() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
myConf = defaultConf.setDefault(myConf);
myKeys = defaultConf.setKeyboard(myKeys);
readConf();
thisClass = new vk_gui();
thisClass.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
thisClass.setVisible(true); // I've set it to true here but would like to set it to false to be able to call it later ...
}
});
}
public void newText(){
thisClass.newText();
}
public boolean isVisible(){
return thisClass.isVisible();
}
public String getText(){
return thisClass.getText();
}
vk_gui中的代码:
public String getText(){
if (super.isVisible())
return null;
return jTextArea.getText();
}
public void newText(){
this.setVisible(true);
this.setAlwaysOnTop(true);
jTextArea.setText("");
}
我这样称呼它:
keyboard.newText();
while(keyboard.getText()==null){} // I know it's busy waiting and it's not good and all that ...
text = keyboard.getText();
谢谢你的建议,麦克斯
setVisible(true)
必须是公共大型机()中的最后一行代码{
this.keyboard=new start_vk();
?,也许可以使用Swing Timer
来调用某些东西并进行处理nearestContainer.revalidate();
// for JFrame/JDialog/JWindow and upto Java7 is there only validate();
nearestContainer.reapaint()
。
4.为了更好地帮助您尽快发布,因为您的
setVisible(true)
调用是第一个调用之一(在将任何组件添加到容器之前),您应该重新验证布局
这些方法的javadoc中明确提到了这一点(例如,方法的复制粘贴):
此方法会更改布局相关信息,从而使组件层次结构无效。如果容器已显示,则此后必须验证层次结构才能显示添加的组件
对于JDK1.6及以下版本,您可以使用
frame.getContentPane().revalidate()
,但对于JDK1.7及以上版本,您可以直接在frame上执行此操作,如frame.revalidate()
谢谢你的快速回答!1.好的,谢谢你的建议2.这个.keyboard以前是私有的start\u vk键盘;3.所以我必须为每个组件调用validate,或者只为框架中的JPanel调用validate?4.哦,好的,如果现在不行,我会的,抱歉0:-)2.不知道创建GUI,如果完成,那么为JFrame调用setVIsible,3.调用对容器进行验证,仅一次作为完成所有更改后的最后代码行进行验证…错误出现在while(keyboard.getText()==null){}…正在忙着等待线程而没有另一个线程…然后validate();丢失,非常感谢!!