2017-08-03 11 views
1

私のJFrameの左上隅でgridbaglayoutを開始しようとしています。私は問題を解決するために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

私は助けてくれるとうれしいです)同じ問題を抱えている他の人々が解決策を見つけるのを助けるために、この回答を受け入れることを検討してください。乾杯 –

関連する問題