2017-05-16 7 views
1

여기 내 코드가있다. 문제는 액션 리스너의 맨 아래에 있습니다. 모든 버튼 객체가 인스턴스화되었다는 것을 알 수 있습니다. 메서드 외부에 단추 개체를 만들려고했습니다. 나는 정말로 해결책을 찾을 수 없다. 당신은 약 scope 읽고 싶은조치 이벤트 메소드에서 Jbutton 오브젝트를 찾을 수 없습니다. 나는 캐릭터 선택 타입을 만들려고 노력 중이며 작동하는 해결책을 찾을 수 없다.

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

public class Select implements ActionListener { 
boolean shipSelect1=false; 
boolean shipSelect2=false; 
boolean shipSelect3=false; 
boolean shipSelect4=false; 
boolean shipSelect5=false; 
public static void main(String[] args) { 
    SwingUtilities.invokeLater(new Runnable() { 
     @Override 
     public void run() { 
      new Select().createGui(); 
     } 
     }); 

} 

public void createGui() { 
    JFrame frame = new JFrame("Java Stocks"); 
    frame.setSize(700, 700); 
    frame.setVisible(true); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    JPanel panel = new JPanel(new GridBagLayout()); 
    frame.add(panel); 
    frame.getContentPane().add(panel, BorderLayout.WEST); 

    GridBagConstraints c = new GridBagConstraints(); 

    JButton button1 = new JButton("Dorito"); 
    c.gridx = 0; 
    c.gridy = 0; 
    c.insets = new Insets(40, 40, 40, 40); 
    panel.add(button1, c); 
    button1.addActionListener(this); 
    button1.setToolTipText("Your gonna fly a dorito in space son."); 

    JButton button2 = new JButton("Otirod"); 
    c.gridx = 0; 
    c.gridy = 1; 
    panel.add(button2, c); 
    button2.addActionListener(this); 
    button2.setToolTipText("(?rosnopS elbissoP).nos ecaps ni ortirod"); 

    JButton button3 = new JButton("Ship"); 
    c.gridx = 0; 
    c.gridy = 2; 
    panel.add(button3, c); 
    button3.addActionListener(this); 
    button3.setToolTipText("Basic Ship"); 

    JButton button4 = new JButton("pihS"); 
    c.gridx = 0; 
    c.gridy = 3; 
    panel.add(button4, c); 
    button4.addActionListener(this); 
    button4.setToolTipText("pihS cisaB"); 

    JButton button5 = new JButton("Good Ship"); 
    c.gridx = 0; 
    c.gridy = 4; 
    panel.add(button5, c); 
    button5.addActionListener(this); 
    button5.setToolTipText("The ship is the best ship. Your not gonna"); 
} 

    public void actionPerformed(ActionEvent e) { 
     Object source = e.getSource(); 
    if(source == button1) 
    { 
     shipSelect1=true; 

    } else if(source == button2) 
    { 
     shipSelect2=true; 
    } 
    else if(source == button3) 
    { 
     shipSelect3=true; 
    } 
    else if(source == button4) 
    { 
     shipSelect4=true; 
    } 
    else if(source == button4) 
    { 
     shipSelect5=true; 
    } 
    else{ 

    } 
    } 
} 

답변

1

도와주세요. 이 속성은 변수의 가시성을 정의합니다.

메소드에 정의 된 변수는 해당 메소드에서만 볼 수 있습니다. 따라서 버튼을 클래스 범위로 이동해야합니다.

의미

JButton button1 = new JButton("Dorito"); 

...

클래스의 몸에 가야한다! 모든 사람들에게 당신이 가지고있는 것과 비슷합니다.

boolean shipSelect1=false; 

참고 : 해당 메소드에서 해당 버튼에 대한 모든 init 호출을 계속 수행 할 수 있습니다. 당신은 그 그 방법의 몸 밖으로 선언을 이동해야합니다.

너머 : 진짜 답변 : 스윙 UI 응용 프로그램을 작성하여 Java를 배우려고하지 마십시오. 기본 사항으로 시작; here처럼 - 다른 것은 좌절감과 시간 낭비로 이어질 것입니다. 당신은 거의 크롤링 할 수 없지만 장애물 경주를하고 싶습니다.