(이 질문이 제대로 수행되지 않은 경우 죄송합니다, 나는 새로운 해요. 그러나 적어도 나는 내 자신의 질문 요청하기 전에 많은 연구)새 구성 요소를 작성하고 여러 번 jpose를 삭제하는 방법은 무엇입니까?
안녕하세요. 나는 자바에서 블랙 잭 게임을 쓰고 있는데 꽤 거대한 회전을하고있다. 내 문제은 스윙 구성 요소의 여러 인스턴스를 처리하는 방법입니다. 호출 할 수 있습니다. 클래스 레벨이나 특정 메소드에서 컴포넌트 (예 : jpanels 및 jbutton)를 만드는 것이 더 낫지는 않습니다.
해당 메소드에서 해당 메소드를 작성하면 조치 리스너에서 볼 수 없지만 클래스 레벨로 작성하면 dispose()
을 호출하면 삭제됩니다.
class BlackjackGame extends JFrame implements ActionListener{
public void mainMenu(){
JPanel menuPane = new JPanel(new GridBagLayout()); //Init of main menu
GridBagConstraints c = new GridBagConstraints();
menuPane.setBackground(new Color(125,0,0));
menuPane.setBounds(620,220,175,250);
JLabel menuTitle = new JLabel("Welcome to Blackjack!");//Main menu-content
c.gridx = 1;
c.gridy = 0;
c.insets = new Insets(0,0,20,0);
menuPane.add(menuTitle, c);
JButton playButton = new JButton("Play!");
playButton.addActionListener(this);
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 3;
c.ipadx = 25;
c.ipady = 25;
c.insets = new Insets(0,0,0,0);
menuPane.add(playButton, c);
JButton exitButton = new JButton("Exit!");
exitButton.addActionListener(this);
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 3;
menuPane.add(exitButton, c);
JButton rulesButton = new JButton("Set rules.");
rulesButton.addActionListener(this);
c.gridx = 0;
c.gridy = 3;
c.gridwidth = 3;
menuPane.add(rulesButton, c);
this.add(menuPane,0);
}
//This is where I get problems
public void actionPerformed (ActionEvent event){
if(event.getSource() == playButton){
//I want the menuPane to disappear, and a transition into the game.
menuPane.dispose();
//Call method for the rest of the game.
}else if(event .getSource() etcetera etcetera){
etcetera etcetera
}
}
}
이렇게하면 actionListener가 내 구성 요소 (예 : playButton 또는 menuPane)를 찾을 수 없습니다. 하지만 클래스 수준 개체로를 도입 한 경우 :
class BlackjackGame extends JFrame implements ActionListener{
JPanel menuPane = new JPanel(new GridBagLayout());
JLabel menuTitle = new JLabel("Welcome to Blackjack!");
JButton playButton = new JButton("Play!");
JButton exitButton = new JButton("Exit!");
JButton rulesButton = new JButton("Set rules.");
public void mainMenu(){
//Rest of code
}
public void actionPerformed(ActionEvent event){
menuPane.dispose();
//Rest of code
}
}
... 그때 menuPane.dispose()
전화, 나는 다시 mainMenu()
를 호출 할 때 나는 그것을 다시 얻을 수있는 방법? 주 메뉴로 되돌아 가려면 모든 버튼뿐만 아니라 menuPane의 새 인스턴스를 만들어야하지만 클래스 수준이고 이미 처리되었으므로 취소 할 수 없습니다.
도와주세요. 감사합니다.
PS. 도움이된다면 atm 인 것처럼 전체 코드를 게시 할 수 있습니다.
편집 : Dan의 답은 실제로 정답 이었기 때문에 Daniel의 답변이 받아 들여졌으며 특정 프로그램에서 잘 작동했습니다. 고마워요, 메리 크리스마스! 내가 코드를 오해하지 않았다면
들어 난 당신이 여기 http://stackoverflow.com/questions/11273267/java-open-a-new-window-by-clicking-a-button – IAmBlake