2017-08-03 101 views
1

我想讓gridbaglayout在我的JFrame的左上角開始。我試圖使用JFrame內的面板來解決問題。它不起作用,它給了我和以前一樣的結果。我得到我想要放置在左上角的面板,它始於中心並始終離開。 GUI problem picture 這是我的課程和代碼。如何讓GridBagLayout定位左上角

public class GraphMatchApp extends JApplet implements ActionListener 
{ 
    private JFrame mFrame; 
    private JPanel mBusinessPanel; 
    private JPanel mItemsPanel; 
    private JTabbedPane mTabbedPane; 
    private JPanel mFramePanel; 
    GridBagConstraints mGridBagConstaints; 


    public GraphMatchApp() 
    { 
     prepairMainFrame(); 
     prepairBusinessPanel(); 
     mGridBagConstaints.fill = GridBagConstraints.HORIZONTAL; 
     mGridBagConstaints.weightx = 0.5; 
     mGridBagConstaints.gridx = 0; 
     mGridBagConstaints.gridy = 0; 
     mGridBagConstaints.anchor = GridBagConstraints.FIRST_LINE_START; 
     mFramePanel.add(mBusinessPanel,mGridBagConstaints); 
     mFrame.setVisible(true); 
    } 
    public void prepairMainFrame() 
    { 
     mFrame = new JFrame(); 
     mFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     mFrame.setTitle("Test"); 

     mFrame.setSize(400,200); 
     mFramePanel = new JPanel(); 
     mFramePanel.setLayout(new GridBagLayout()); 
     mFrame.add(mFramePanel, BorderLayout.LINE_START); 
     mGridBagConstaints = new GridBagConstraints(); 
    } 
    public void prepairBusinessPanel() 
    { 
     mBusinessPanel = new JPanel(); 
     Border border = BorderFactory.createEtchedBorder(); 
     border = BorderFactory.createTitledBorder(border,"What is your ?"); 
     mBusinessPanel.setBorder(border); 
     JRadioButton logisticsButton = new JRadioButton("Logistics"); 
     logisticsButton.setMnemonic(KeyEvent.VK_L); 
     logisticsButton.setActionCommand("SelectionLogistics"); 
     logisticsButton.setSelected(false); 

     JRadioButton healthCareButton = new JRadioButton("Health care"); 
     healthCareButton.setMnemonic(KeyEvent.VK_H); 
     healthCareButton.setActionCommand("SelectionHealthCare"); 
     healthCareButton.setSelected(false); 

     JRadioButton transportationButton = new JRadioButton("Transp"); 
     transportationButton.setMnemonic(KeyEvent.VK_T); 
     transportationButton.setActionCommand("SelectionTransportation"); 
     transportationButton.setSelected(false); 

     ButtonGroup group = new ButtonGroup(); 
     group.add(logisticsButton); 
     group.add(healthCareButton); 
     group.add(transportationButton); 

     logisticsButton.addActionListener(this); 
     healthCareButton.addActionListener(this); 
     transportationButton.addActionListener(this); 

     mBusinessPanel.add(logisticsButton); 
     mBusinessPanel.add(healthCareButton); 
     mBusinessPanel.add(transportationButton); 
    } 
    public static void main(String[] args) 
    { 
     new GraphMatchApp(); 
    } 

回答

2

在你的構造,設置mGridBagConstaints.weighty = 0.5;

面板,然後只需要一半的幀的可用空間。

+0

它的工作,問題解決了。感謝您的時間。 – Jerry

+0

很高興我能幫忙;)請考慮接受這個答案,以幫助其他有同樣問題的人找到解決方案。乾杯 –