调用 setVisible(false) 后,我的 JFrame 内容在调用 set Visible(true) 时消失了
我正在设计一个应在其中绘制文本的绘图程序(用 Java 编写).由于我正在使用 kinect 执行此操作,因此我想使用我已经找到的 onscreenKeyboard.这个键盘基本上就是一个JFrame,里面有JComponents,我不想讲太多……
I'm designing a drawing program (in Java) in which text should be drawn. Since I'm doing this with kinect I'd like to use an onscreenKeyboard which I've already found. This keyboard is basically a JFrame witch JComponents in it, I dont't want to go too much in detail ...
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 出现里面没有组件...
In start_vk I could call setVisible(true) and it'd works but I would love to actually call this later only when I need it ... Now I call at some place the setVisible(true) and only the JFrame appears with no Components in it ...
我是否应该在单独的线程中调用它,因为 start_vk 的构造函数是使用 SwingUtilities.invokeLater() 完成的?还是有其他建议?
Should I call this in an apart Thread because the constructor for start_vk is done with SwingUtilities.invokeLater()? Or any other suggestion?
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 中的代码:
The code in 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();
感谢您的任何建议,马克斯
Thanks for any suggestions, Max
推荐答案
setVisible(true);
必须是public MainFrame() {
应该是什么 this.keyboard = new start_vk();
???,也许是为了使用 Swing Timer
来调用某些东西并处理
what should be this.keyboard = new start_vk();
???, maybe to use Swing Timer
for invoke something and dealyed
调用 setVisible(false) 后,我的 JFrame 内容在调用 set Visible(true) 时消失了
After calling setVisible(false) my JFrame contents are gone when calling set Visible(true)
如果您在已经可见的容器中添加或删除某些内容,您必须调用
if you add or remove something from/to already visible container you have to call
nearestContainer.revalidate();
// for JFrame/JDialog/JWindow and upto Java7 is there only validate();
nearestContainer.reapaint()
.4. 为了获得更好的帮助,请尽快发布 SSCCE
. 4. for better help sooner post an SSCCE
相关文章