.isVisible()方法不适用于JFrame

时间:2016-04-27 19:24:56

标签: java jframe

我已经创建了一个GUI,我在下面提到了我需要的两种方法。正如您在单击按钮时所看到的,菜单的可见性设置为false。我想在其他程序上做一些事情,但只有当点击按钮时,框架已经消失。出于演示目的,我让我的程序不断吐出数百万行“GUI Visible”,直到Menu被设置为不可见,它应该更改为吐出“GUI隐藏”。

public class Menu extends javax.swing.JFrame {
public Menu() {
    initComponents();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    try {
        this.WriteToText();
    } catch (Exception ex) {
        Logger.getLogger(Menu.class.getName()).log(Level.SEVERE, null, ex);
    }
}                                        

 public void WriteToText() throws Exception
 {

     FileWriter fw = new FileWriter("playerdetails.txt");
 BufferedWriter bw = new BufferedWriter(fw);
 bw.write(playername.getText());
     System.out.println("Printed");
     clicked = "1";
     this.setVisible(false); //HERE

这是我运行菜单的程序。它看起来好像是“hm.isVisible();”我使用的方法不起作用。当我运行它时,程序说GUI无论是否隐藏都是隐藏的。通过使用S.o.pl,我确定该方法总是返回false值。我怎样才能让它返回正确的值?

package monster.defense;
import java.io.*;
public class MonsterDefense{
public static void main(String args[]) throws Exception {

    Menu hm = new Menu(); 
    hm.setvisible(true);
    BufferedReader pdetails = new BufferedReader(new FileReader("playerdetails.txt"));
    BufferedReader inKb = new BufferedReader(new InputStreamReader (System.in));

    int yum = 1;
    while(yum == 1)
    {
        if(hm.isVisible()==true) //HERE
        {
            System.out.println("GUI visible");
        }
        else
        {
            System.out.println("GUI hiding");
        }
    }
    String cat = inKb.readLine();
    System.out.println("here"+hm.getPName());

  }
}

3 个答案:

答案 0 :(得分:0)

在Main类中创建一个自定义布尔值。如果你不确定isVisible()

class Main{

boolean opened = false;

public void setOpen(boolean open){
this.opened = open;
}

public Boolean isOpened(){
return opened;
}

}
你在main方法中的逻辑中的

现在可以检查

if(hm.isOpened()){
}

打开框架时调用方法

hm.setOpen(true); along with the `setVisible(true);`

答案 1 :(得分:0)

检查tutorial on Swing Component listeners并将您的逻辑移至componentHidden()componentShow()侦听器方法。

编辑:正如@MadProgrammer所说,WindowListener如果你想截取窗口关闭/打开事件,那将是合适的。它还has a tutorial。 您可能想要检查windowActivated() / windowDeactivated()方法。

答案 2 :(得分:0)

所以,一旦我让你的代码运行起来,它对我运行就好了。

然而,这是错误的方法,而不是使用while循环,这可能会阻止事件调度线程,使得无法关闭窗口(或与您的UI交互),你应该是使用模态对话框,例如......

import java.awt.EventQueue;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                Menu menu = new Menu();
                JOptionPane.showMessageDialog(null, menu, "PlayerName", JOptionPane.PLAIN_MESSAGE);

                String playerName = menu.getPlayerName();

                // Now you can write it to the file if you need

            }
        });
    }

    public class Menu extends JPanel {

        private JTextField playerName;

        public Menu() {
            setLayout(new GridBagLayout());
            playerName = new JTextField(10);
            add(new JLabel("Player name: "));
            add(playerName);
        }

        public String getPlayerName() {
            return playerName.getText();
        }
    }

}

有关详细信息,请查看How to Make Dialogs