2017-05-17 3 views
0

내 프로그램에서 문제가 발생하면 코드를 처음 실행하면 완벽하게 실행되지만 새 개체를 만든 다음 창을 표시 한 원래 메서드를 다시 호출하면 창 올바른 제목으로 올바른 크기로 표시되지만 구성 요소를 표시하지 않습니다.새 개체를 만들 때 JFrame이 구성 요소를 표시하지 않음

정적 변수가없고 모든 필수 변수가 생성자에서 초기화되었는지 확인했습니다. 창을 표시

클래스 :

public class DifficultySelect implements ActionListener   
{ 
    private JPanel getInput; 
    private JFrame startFrame; 
    private JButton easy; 
    private JButton medium; 
    private JButton hard; 
    private JButton custom; 
    private JLabel labelRow; 
    private JLabel labelColumn; 
    private JLabel labelMines; 
    private JTextArea textRow; 
    private JTextArea textColumn; 
    private JTextArea textMines; 
    public ArrayList<Integer> getArray; 

    public DifficultySelect() 
    { 
     getInput = new JPanel(); 
     startFrame = new JFrame("Select Difficulty:"); 
     easy = new JButton(); 
     medium = new JButton(); 
     hard = new JButton(); 
     custom = new JButton(); 
     labelRow = new JLabel(); 
     labelColumn = new JLabel(); 
     labelMines = new JLabel(); 
     textRow = new JTextArea(5, 20); 
     textColumn = new JTextArea(5, 20); 
     textMines = new JTextArea(5, 20); 
     getArray = new ArrayList<Integer>(); 
    } 

    public void setDisplay() 
    { 
     getInput.setLayout(new BoxLayout(getInput, BoxLayout.PAGE_AXIS));  
     getInput.setVisible(true); 

     Dimension buttonSize = new Dimension(300,40); 
     easy.setText("Easy: 5x5 - 4 Mines"); 
     easy.setMaximumSize(buttonSize); 
     easy.addActionListener(this); 
     medium.setText("Medium: 10x10 - 20 Mines"); 
     medium.setMaximumSize(buttonSize); 
     medium.addActionListener(this); 
     hard.setText("Hard: 15 x 15 - 50 Mines"); 
     hard.setMaximumSize(buttonSize); 
     hard.addActionListener(this); 
     custom.setText("Custom: Enter Rows/Columns/Mines then Click"); 
     custom.setMaximumSize(buttonSize); 
     custom.addActionListener(this); 

     labelRow.setText("Enter Row Size: ");  
     labelColumn.setText("Enter Column Size: ");  
     labelMines.setText("Enter Amount of Mines:"); 

     textRow.setAlignmentX(0); 
     textColumn.setAlignmentX(0); 
     textMines.setAlignmentX(0); 

     getInput.add(easy); 
     getInput.add(medium); 
     getInput.add(hard); 
     getInput.add(custom); 
     getInput.add(labelRow); 
     getInput.add(textRow); 
     getInput.add(labelColumn); 
     getInput.add(textColumn); 
     getInput.add(labelMines); 
     getInput.add(textMines); 

     startFrame.add(getInput); 
     startFrame.setSize(310, 250); 
     startFrame.setResizable(false); 
     startFrame.setVisible(true); 
     startFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 

코드의 다음 블록은 내가 메인에서 호출 할 때 작동하여 GUI 클래스를 호출하고 방법을 보여줍니다,하지만 프로그램에 호출 할 때 작동하지 않습니다

private DifficultySelect d; 

    public StartGame() 
    { 

     d = new DifficultySelect(); 
     d.setDisplay(); 
    } 

이 작업을 수행하는 데 여러 가지 방법을 시도해 왔습니다. 현재 올바른지 만 공백의 흰색 화면이 나타나는 StartGame newGame = new StartGame();으로 개체를 호출하고 있습니다. 크기, 및있다 정확한 애스펙트가 생성된다는 것을 의미하는 올바른 JFrame 이름.

+0

[MCVE] 또는 [짧은, 자체 포함, 올바른 예] (http://www.sscce.org/)를 게시하는 것이 좋습니다. –

+0

JPanel 및/또는 JFrame을'repaint() '하려고 했습니까? –

+0

재 페인트 사용을 시도했지만 성공하지 못했습니다. – Pengoid

답변

1

, 당신 GUI 실행을 시도하십시오 :

public class StartGame { 

    public static void main(String[] args) { 
    DifficultySelect d = new DifficultySelect(); 
     d.setDisplay(); 

    } 
} 

보이는을 :

 if (d == null) { 
      d = new DifficultySelect(); 
      d.setVisible(true); 
     } else { 
      d.setVisible(true); 
     } 
: 다시 당신이 코드를 사용할 수있는 또 다른 장소에서이 폼을 표시하려는 enter image description here

새 인스턴스를 만들려면 다음 코드를 입력하십시오.

DifficultySelect new_d = new DifficultySelect(); 
    new_d.setDisplay(); 
+0

당신이 내게 정말로 말하고있는 것이 뭔지 모르겠군요, 그건 내 프로그램이 처음 실행되는 모습입니다. 문제가 시작될 프레임을 다시 만드는 메서드를 다시 호출해야합니다. – Pengoid

+0

그 뜻 이세요? –

+0

그 덕분에 문제가 해결되었습니다. 감사합니다. 나는 새로운 것을 만들지 않고 그것을 창조했다. – Pengoid