gpt4 book ai didi

Java GUI 使用 setVisible()

转载 作者:行者123 更新时间:2023-12-01 12:48:16 24 4
gpt4 key购买 nike

我有一个从 JFrame 扩展的类,如下所示:

public class TicTacToeSubMenu extends JFrame implements ActionListener { }

在同一个文件中,我有另一个用于客户端连接的类,如下所示:

class ListenThread extends Thread { }

问题是,在 ListenThread 类中,我试图退出当前帧并打开一个新帧,到目前为止,新帧已按需要打开,但当前帧不会退出。

我使用了这两个命令,但都不起作用:

1. TicTacToeSubMenu.this.setVisible(false);

2. setVisible(false);

如何从同一文件的另一个类中退出当前帧?

一个更大的例子:

从以前的窗口框架我这样做:

            TicTacToeSubMenu win = new TicTacToeSubMenu(word, false);
win.setTitle("Tic Tac Toe");
win.pack();
win.setLocation(600, 200);
setVisible(false);
win.show();

然后在一个新文件和新框架中:

public class TicTacToeSubMenu extends JFrame implements ActionListener {
variables.....

public TicTacToeSubMenu(String username, boolean playing) {
new TicTacToeSubMenu();
}


public TicTacToeSubMenu() {
connectUser();
}

public void connectUser() {
clientThread = new ConnectThread();
clientThread.start();

}

class ConnectThread extends Thread {
InputStream input;
OutputStream output;
ObjectOutputStream oos;
Socket s;

public void sendText(String text) {
try {
oos.writeObject(text);

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void run() {
try {
s = new Socket(HOST, PORT);
output = s.getOutputStream();
oos = new ObjectOutputStream(output);

isOnline = true;
isConnected = true;

new ListenThread(s).start();

} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

class ListenThread extends Thread {

Socket s;
InputStream input;
ObjectInputStream ois;

public ListenThread(Socket s) {
this.s = s;
try {
input = s.getInputStream();
ois = new ObjectInputStream(input);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public void run() {
while (isConnected) {
try {
final String inputMessage = (String) ois.readObject();
System.out.println(inputMessage);
String user;
user = getWord(inputMessage, 0);
String opponent;
opponent = getWord(inputMessage, 1);
String message = getWord(inputMessage, 2);


if (!user.equalsIgnoreCase(un)
&& opponent.equalsIgnoreCase(un)
{

TicTacToeMultiPlayer window = new TicTacToeMultiPlayer(
un, user, t2, playing, turn);
window.setTitle("Tic Tac Toe");
window.setLocation(400, 100);
window.pack();
TicTacToeSubMenu.this.setVisible(false);
window.show();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
}

}

最佳答案

如果您想“隐藏”JPanel,请使用代码“this.dispose();”而不是将可见性设置为 false。

关于Java GUI 使用 setVisible(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24472661/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com