我有一个从JFrame扩展的类:
public class TicTacToeSubMenu extends JFrame implements ActionListener { }
并且在同一个文件中我有另一个客户端连接类,如下所示:
class ListenThread extends Thread { }
问题是,在ListenThread类中,我试图退出当前帧并打开一个新帧,到目前为止,新帧按需要打开,但当前帧不会退出。
我使用了两个命令,但都没有工作:
1. TicTacToeSubMenu.this.setVisible(false);
2. setVisible(false);
如何从同一个文件的另一个类中退出当前帧?
更大的例子:
从之前的widnow框架我这样做:
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();
}
}
}
}
}
答案 0 :(得分:1)
如果您想“隐藏”JPanel,请使用代码“this.dispose();”而不是将可见性设置为false。