如何让 GridBagLayout 定位到左上角
How to get GridBagLayout to position top left corner
我正在尝试让 gridbaglayout 从我的 JFrame 的左上角开始。我试图在 JFrame 中使用面板来解决问题。它不起作用,它给我的结果和以前一样。我得到了我想放在左上角的面板,它从中间开始,一直在左边。
GUI problem picture
这是我的 class 和代码。
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();
}
在您的构造函数中,设置 mGridBagConstaints.weighty = 0.5;
然后面板只占用帧可用 space 的一半。
我正在尝试让 gridbaglayout 从我的 JFrame 的左上角开始。我试图在 JFrame 中使用面板来解决问题。它不起作用,它给我的结果和以前一样。我得到了我想放在左上角的面板,它从中间开始,一直在左边。 GUI problem picture 这是我的 class 和代码。
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();
}
在您的构造函数中,设置 mGridBagConstaints.weighty = 0.5;
然后面板只占用帧可用 space 的一半。