2014-11-23 6 views
0

문제는 간단 해 보이지만 해결할 수없는 것 같습니다.Java - JMenuBar가 유리 창을 사용할 때 숨김

내 프레임 (또한 ContentPane)에 GlassPane을 사용하고 있습니다. 그래서 프레임에 JMenuBar를 추가하면 나타나지 않습니다. 다른 시간에 GlassPane을 사용할 경우 모든 것이 완벽하게 작동합니다. 몇 가지 조사를했는데 JMenuBar가 RootPane에 표시되고 GlassPane이 어떻게 든 그것을 숨기고 있다고 생각합니다.

glassPane을 사용하는 동안 JMenuBar를 가져올 수있는 방법이 있는지 알아야합니까?

감사

UPDATE : I (false)를 glassPane.setOpaque를 설정하고

UPDATE : 코드의

실제 라인이 훨씬 더하지만 문제는 여기에 상대적 것들입니다 .

public class Demo extends JFrame { 

///////////////////////////////////////////////////////////////////////// 
// JMenuBar 
private final JMenuBar mainMenuBar; 
    private final JMenu fileMenu; 
     private final JMenuItem exitFileMenu; 
///////////////////////////////////////////////////////////////////////// 
// CONTENT PANE & COMPONENTS 
private final JPanel contentPanel; 
    private final JPanel buttonPanel; 
     private final JButton button1; 

///////////////////////////////////////////////////////////////////////// 
// GLASSPANE AND COMPONENTS 
private final JPanel glassPanel; 
    private final JPanel buttonPanel2; 
    private final JButton button2; 

public Demo() { 
    super(); 

    this.mainMenuBar = new JMenuBar(); 
     this.fileMenu = new JMenu("File"); 
      this.exitFileMenu = new JMenuItem("EXIT"); 

    this.contentPanel = new JPanel(new BorderLayout()); 
     this.buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); 
      this.button1 = new JButton("Button 1"); 

    this.glassPanel = new JPanel(new BorderLayout()); 
     this.buttonPanel2 = new JPanel(new FlowLayout(FlowLayout.RIGHT)); 
      this.button2 = new JButton("Button 2"); 
} 

public void initGUI() { 
     this.fileMenu.add(this.exitFileMenu); 
    this.mainMenuBar.add(this.fileMenu); 

     this.buttonPanel.add(this.button1); 
    this.contentPanel.add(this.buttonPanel, BorderLayout.NORTH); 


     this.buttonPanel2.add(this.button2); 
    this.glassPanel.add(this.buttonPanel2, BorderLayout.NORTH); 

    super.setContentPane(this.contentPanel); 
    super.setGlassPane(this.glassPanel); 

    this.glassPanel.setOpaque(false); 
    this.glassPanel.setVisible(true); 

    super.setExtendedState(JFrame.MAXIMIZED_BOTH); 
    super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    super.setJMenuBar(mainMenuBar); 
    super.setVisible(true); 
} 

public static void main(String[] args) { 
    Demo obj = new Demo(); 
    obj.initGUI(); 
} 

}

+0

문제를 재현하는 코드를 제공하십시오. –

+0

@SergiyMedvynskyy 일부 코드 줄과 스크린 샷을 추가했습니다. – Abbas

+0

@ AbbasA.Ali * "some lines of code"*는 실행 가능한 예제를 작성하지 않습니다. 문제를 재현 할 수 없다면 문제를 해결할 수 없습니다. 다른 모든 것은 일을 추측합니다. 문제를 나타내는 [실행 가능한 예제] (https://stackoverflow.com/help/mcve)를 제공하십시오. 이렇게하면 혼란이 적고 응답이 좋아집니다. – MadProgrammer

답변

0

그래, 내가 실수로 문제의 해결책을 찾았습니다. 사실 간단합니다. 'glassPane'내에 중첩 패널을 사용하는 경우 각 중첩 패널의 불투명도를 false로 설정하면됩니다. 그렇지 않은 경우 중첩 패널은 각 경계에 배경을 표시하고 기본 레이어를 겹칩니다.

여기는 Demo의 작동 코드입니다.

public class Demo extends JFrame { 

///////////////////////////////////////////////////////////////////////// 
// JMenuBar 
private final JMenuBar mainMenuBar; 
    private final JMenu fileMenu; 
     private final JMenuItem exitFileMenu; 
///////////////////////////////////////////////////////////////////////// 
// CONTENT PANE & COMPONENTS 
private final JPanel contentPanel; 
    private final JPanel buttonPanel; 
     private final JButton button1; 

///////////////////////////////////////////////////////////////////////// 
// GLASSPANE AND COMPONENTS 
private final JPanel glassPanel; 
    private final JPanel buttonPanel2; 
    private final JButton button2; 

public Demo() { 
    super(); 

    this.mainMenuBar = new JMenuBar(); 
     this.fileMenu = new JMenu("File"); 
      this.exitFileMenu = new JMenuItem("EXIT"); 

    this.contentPanel = new JPanel(new BorderLayout()); 
     this.buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); 
      this.button1 = new JButton("Button 1"); 

    this.glassPanel = new JPanel(new BorderLayout()); 
     this.buttonPanel2 = new JPanel(new FlowLayout(FlowLayout.RIGHT)); 
      this.button2 = new JButton("Button 2"); 
} 

public void initGUI() { 
     this.fileMenu.add(this.exitFileMenu); 
    this.mainMenuBar.add(this.fileMenu); 

     this.buttonPanel.add(this.button1); 
    this.contentPanel.add(this.buttonPanel, BorderLayout.NORTH); 


     this.buttonPanel2.add(this.button2); 
     this.buttonPanel2.setOpaque(false); 
    this.glassPanel.add(this.buttonPanel2, BorderLayout.NORTH); 

    super.setContentPane(this.contentPanel); 
    super.setGlassPane(this.glassPanel); 

    this.glassPanel.setOpaque(false); 
    this.glassPanel.setVisible(true); 

    super.setExtendedState(JFrame.MAXIMIZED_BOTH); 
    super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    super.setJMenuBar(mainMenuBar); 
    super.setVisible(true); 
} 

public static void main(String[] args) { 
    Demo obj = new Demo(); 
    obj.initGUI(); 
} 

}

이제 당신이 그렇지 않으면의 GlassPane가 불투명 남아 'setGlassPane(JPanel)'를 호출 한 후에의 glassPane의 불투명도를 설정할 항상 기억한다. (위의 방법을 호출하기 전이나 후에 설정 한 중첩 패널)

0

당신은 BorderLayout을 사용하고 있고 glassPanel에 대한 옵션 BorderLayout.NORTH (mainPanel 및 notificationPanel 자체 제작 JPanel의에서 연장 클래스는). 그것은 북쪽의 전체 공간을 차지하며 전체 메뉴와 겹칩니다. 그러므로 너는 더 이상 아무것도 보지 못한다. 예를 들어, 패널 생성을 다음과 같이 변경하십시오.

this.glassPanel = new JPanel(); 

그러면 패널이 버튼에만 맞도록 크기가 조정되고 메뉴가 표시됩니다. 일부 레이아웃을 가지고 놀고 어느 레이아웃이 적합한 지 볼 수 있습니다. 그러나 유리판은 항상 모든 것의 꼭대기에 있다는 것을 기억하십시오. 작은 쪽 참고 : 'buttonPanel2'을 사용하지 않고 'glassPanel'에 직접 단추를 추가하면 작은 "경계선"을 제거 할 수 있습니다. 그렇지 않으면 크기를 조절하여 버튼에 완벽하게 맞출 수 있습니다. 둘 다 가능하지만 버튼 하나의 구성 요소 만 갖고 싶으면 직접 추가 할 것입니다.

+0

그래, 제안대로 노력했지만 어떤 레이아웃을 사용하지 않고 button2를 패널에 직접 추가했지만 contentpanel이 전혀 표시되지 않습니다. 이 문제를 어떻게 극복했는지 지정하십시오. – Abbas

+0

방금 ​​한 줄 (this.glassPanel = 새 JPanel())을 변경 한 다음 추가 패널에 먼저 추가하는 대신 줄을 this.glassPanel.add (this.button2)로 변경했습니다. 나머지는 같습니다. 결과는 두 개의 버튼과 메뉴 바를 보여주었습니다. – BluesSolo

+0

그래서 내가 무엇이 잘못되었는지 알아 냈습니다. 추가 된 중첩 패널을 사용하는 경우 각 패널의 불투명도를 false로 설정해야합니다. 그렇지 않으면 기본적으로 true로 설정됩니다. 그래, 도와 줘서 고마워! – Abbas

관련 문제