我正在设计一个绘图程序(用Java编写),其中应该绘制文本。因为我用kinect这样做,所以我想使用我已经找到的onscreenKeyboard。这个键盘基本上就是JFrame中的JComponents,我不想太详细了......
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();
感谢任何建议,Max
答案 0 :(得分:2)
setVisible(true);
必须是public MainFrame() {
应该是this.keyboard = new start_vk();
???,或许可以使用Swing Timer
来调用某些内容并进行分析
调用setVisible(false)后调用set Visible(true)时,我的JFrame内容消失了
如果您在已经可见的容器中添加或删除某些内容,则必须调用
nearestContainer.revalidate();
// for JFrame/JDialog/JWindow and upto Java7 is there only validate();
nearestContainer.reapaint()
。 4.发布SSCCE
后尽快提供更好的帮助答案 1 :(得分:1)
由于您的setVisible( true )
调用是第一次调用之一(在将任何组件添加到容器之前),您应该重新验证布局。
这些方法的javadoc中明确提到了这一点(例如Container#add
方法的复制粘贴):
此方法更改与布局相关的信息,因此使组件层次结构无效。如果已显示容器,则必须在此后验证层次结构,以显示添加的组件。