2017-09-03 13 views
0

homeWindowFrame이라는 JFrame을 만들고 크기를 (600, 500)으로 설정 한 다음 JFrame이라는 mainContainerPanel이라는 JPanel을 추가했습니다. JPanel에 새로운 크기를 설정했지만 작동하지 않습니다. JPanel 크기는 업데이트하는 대신 JFrame의 크기와 동일하게 유지됩니다. JFrame에서 JPanels에 크기를 어떻게 설정합니까? 미리 감사드립니다. 여기에 내 코드 : */JPanel이 컨테이너 크기를 상속받지 못하게하는 방법

JFrame homeWindowFrame = new JFrame("Home - Crime File Management System"); 

    if (isInvalidLogin) { 
     homeWindowFrame.setSize(600, 500); 

    } else { 
     homeWindowFrame.setSize(600, 400); 
    } 

    homeWindowFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    homeWindowFrame.setLocation((screenSize.width/2) - (homeWindowFrame.getWidth()/2), (screenSize.height/2) - (homeWindowFrame.getHeight()/2)); 


    /** 
    * Main panel construction 
    */ 
    JPanel mainContainerPanel; 

    if (isInvalidLogin) { 
     mainContainerPanel = new JPanel(new GridLayout(4, 2)); 

    } else { 
     mainContainerPanel = new JPanel(new GridLayout(3, 2)); 
    } 

    homeWindowFrame.add(mainContainerPanel); 
+1

1) ASCII 아트 나 간단한 그림을 제공 * 의도 * 최소 크기의 GUI의 레이아웃 및 경우보다 폭과 높이, 크기 조정 가능 - 여분의 공간을 어떻게 사용해야하는지 보여줄 수 있습니다. 2)'homeWindowFrame.setLocation (..'은'setLocationRelativeTo (null)'으로 대체 될 수 있지만 스크린 중앙에 무엇인가 놓으면 스플래시 화면처럼 보이게됩니다. 더 나은 것을 구현할 때까지'setLocationByPlatform (true) '를 사용하는 것이 좋습니다. 자세한 내용은 [이 답변] (https://stackoverflow.com/questions/7143287/how-to-best-position-swing-guis/7143398#7143398) –

+0

답장을 편집하십시오 (코드) –

답변

1

귀하의 코드는 어떻게 자바 스윙 레이아웃 매니저 일을 무시하고있는 것으로 보인다

/** * 메인 창 건설. JPanel을 JFrame에 추가 할 때, 기본 배치는 BorderLayout이며, 이는 패널을 프레임의 중앙에 배치하고 프레임을 채울 크기를 지정합니다. 다른 크기의 패널을 만들고 싶으면 preferredSize를 어떻게 든 설정해야하며 JPanel을 보유한 컨테이너는 다른 레이아웃 관리자를 사용해야합니다. GridBagLayout가 「디폴트」의 방법 (1 개의 컴퍼넌트를 추가, GridBagConstraints 없음)으로 사용되면 (자), JPanel가 원하는 경우는 중앙에 배치됩니다. 이러한 제안이 도움이되지 않는다면, 자신의 후부 MCVE을 만들어야합니다 (링크를 읽으십시오).

, MY MCVE :

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.GridBagLayout; 
import javax.swing.*; 

@SuppressWarnings("serial") 
public class DiffSizedPanel extends JPanel { 
    private static final int PANEL_W = 400; 
    private static final int PANEL_H = 300; 
    private static final int FRAME_W = 600; 
    private static final int FRAME_H = 500; 
    private static final Color BG_COLOR = Color.PINK; 

    public DiffSizedPanel() { 
     setBackground(BG_COLOR); 

     // set the JPanel the preferred size desired 
     setPreferredSize(new Dimension(PANEL_W, PANEL_H)); 
     setBorder(BorderFactory.createTitledBorder("This is the JPanel")); 
    } 

    private static void createAndShowGui() { 
     JFrame frame = new JFrame("Different Sized Panel"); 

     // set the JFrame the preferred size desired 
     frame.setPreferredSize(new Dimension(FRAME_W, FRAME_H)); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     // change the content pane's layout from default BorderLayout to GridBagLayout 
     frame.getContentPane().setLayout(new GridBagLayout()); 
     frame.getContentPane().add(new DiffSizedPanel()); // add the JPanel 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(() -> createAndShowGui()); 
    } 
} 
+0

고맙습니다. 작동합니다. GridBag 레이아웃 사용 –