时我的JFrame内容不见了

时我的JFrame内容不见了

本文介绍了调用setVisible(false)后,调用set Visible(true)时我的JFrame内容不见了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个绘图程序(用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();

谢谢您的建议,麦克斯

推荐答案

  1. 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

如果要向已经可见的容器中添加或删除某些东西,则必须调用

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

这篇关于调用setVisible(false)后,调用set Visible(true)时我的JFrame内容不见了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 02:24