네트워크로 연결된 게임 클라이언트를 작성 중이며 단추를 클릭 할 때 프레임 간을 변경할 때 문제가 있습니다.JFrame이 제대로로드되지 않았습니다.
클라이언트의 각 페이지를 다른 프레임으로 작성했으며 클라이언트의 홈 페이지에서 메뉴 단추를 클릭하면 해당 프레임이 표시되어야합니다. 다음
는내가했던 코드 ..
public class homePage extends JFrame{
public homePage() {
initComponents();
}
private void initComponents(){
// the frame and butttons are here....
GameListBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
this.setVisible(false); // to hide the current home page
new GameList().start(); // to start new frame called GameList
// basically the same thing as following code
// GameList gl = new GameList();
// gl.setVisible (true);
// gl.run();
}
}
}
public class GameList extends JFrame{
public GameList(){
//Everything about the frame is here
}
// this method is for connection
public void run(){
try{
// open socket and initialize input streams, output streams here
while (true){
//process data to and from server
}
}// end of try
} // end of run
// this method is to display GameList frame and it's connection
public static void start(){
GameList frame = new GameList();
frame.setVisible(true);
frame.run();
}
}
이 다음 클래스는 단지 GameList 프레임을 실행하고 main 메소드의 연결입니다입니다
public static void main(String[] args) {
new GameList().start();
// basically the same thing as following code
// GameList gl = new GameList();
// gl.setVisible (true);
// gl.run();
}
내 GameList 프레임은 main 메소드에서 실행했을 때 제대로 작동합니다. GUI가 표시되고 연결이 설정되어 데이터 전송에 성공한 것입니다. 기본적으로하고 싶은 것은 메인 메소드에서 호출 할 수있는 홈페이지의 ActionListener에서 새로운 GameList(). start() 메소드를 호출하고 GameList를 표시하고 홈페이지를 숨기는 것입니다.
첫 번째 코드에서 보았 듯이이 작업을 수행하면 GameList의 GUI가로드되지 않고 (검정색으로 바뀜) 연결이 설정되고 데이터 전송에 성공합니다. GUI는 연결이 종료 된 경우에만 표시됩니다. 그 이유는 GameList의 run() 메서드 내부에 while 루프가있는 것 같군요 ??
하지만 GameList 클래스의 main에서 실행하면 똑같은 기능이 매력처럼 작동합니다. 아무도 내가 gui가로드되지 않은 이유를 말해 줄 수 있습니까?
죄송합니다. 내 질문에 바보가 들리지만 도움을 주시면 대단히 감사하겠습니다.
가능한 한 여러 프레임을 사용하지 마십시오. 사용자가 혼란스러워 화면에 혼란을 일으킬 수 있습니다. 가능한 경우,'CardLayout' 또는'JDesktopPane' 또는'JTabbedPane'을 대신 사용하십시오 – MadProgrammer
예.당신이 몇 개의 게임을 던져서 얼마나 많은 게임을 던질 지 상상해보십시오. 너무 많이하지 않아도됩니다. –
[여러 JFrame 사용, 좋음/나쁨 사용법] (http://stackoverflow.com/a/9554657/418556) –