모두가 플래그 변수를 사용하는 방법에 따라 달라집니다. 코드 샘플에서 전혀 플래그가 사용되지 않는 것을 보았습니다. 왜 그런 경우에 정적 변수를 사용하지 않으려는지 모르겠지만, 그 중요한 일이있는 경우 그러나, 해결책 중 하나가 MyFrame을 클래스의 공용 변수 (없음 정적 변수)로 플래그 변수를 사용하는 다음 주 기능에서에 접근 할 것 :
public class MyFrame extends JFrame
{
private Panel1 ;
private Panel2 ;
public boolean flag;
public MyFrame()
{
super("MyFrame");
this.flag = true;
P1 = new Panel1();
P2 = new Panel2();
setLayout(new BorderLayout());
add(P1,BorderLayout.CENTER);
add(P2,BorderLayout.EAST);
setSize(500,400);
setVisible(true);
}
public static void main(String[] args) {
MyFrame mF=new MyFrame();
// you can access your flag this way
mF.flag = false; // or use it the way you want !
mF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
그러나 경우 당신은 Panel2 클래스 내부에서 그 플래그를 사용하기를 원할 것입니다. 그러면 JFrame 타입을 인스턴스로 전달할 매개 변수로 받아들이는 Panel2 클래스에 새 생성자를 추가해야 할 것입니다 ...
,210
public class MyFrame extends JFrame
{
private Panel1 ;
private Panel2 ;
public boolean flag;
public MyFrame()
{
super("MyFrame");
P1 = new Panel1();
this.flag = true;
P2 = new Panel2(this); /* then you will be able to have a visibility
on the flag variable of your MyFrame instance.*/
setLayout(new BorderLayout());
add(P1,BorderLayout.CENTER);
add(P2,BorderLayout.EAST);
setSize(500,400);
setVisible(true);
}
public static void main(String[] args) {
MyFrame mF=new MyFrame();
// you can access your flag this way
mF.flag = false; // or use it the way you want !
mF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
당신은 또한 당신의 주요 기능에있는 동안 플래그 변수를 전달할 수있는 부울 변수를 받아 Panel2에 기능을 추가 할 수 있습니다
예, 가능합니다. 'main' 메쏘드 안에서'MyFrame'의 인스턴스를 만들고 거기에서부터 진행하십시오. –
그는 이미 그렇게하지 않았습니까? – raam86
불명확 한 질문. – raam86