我的GridBagLayout展示位置有什么问题?

时间:2018-01-09 18:37:27

标签: java gridbaglayout

我知道这里有很多GridBagLayout问题,但我似乎无法找到任何与我有相同问题的人。我试图在6x3网格中使用GridBagLayout组织一些按钮和面板,这样元素可以将任意行分成一半或三分之一(使用宽度为3和2)。似乎行实际上只被分成两半,但有没有人知道为什么这些元素似乎没有准确地遵循放置指令?我相信提供的代码应该是所有相关的。我提前为任何遗漏的信息或破坏的约定道歉,这是我第一次在这里提问。谢谢!

Area organized by GridBagLayout

// making generation panel
JPanel generationPanel = new JPanel();
generationPanel.setLayout(new GridBagLayout());
generationPanel.setBorder(BorderFactory.createTitledBorder("new world generation"));
generationPanel.setPreferredSize(new Dimension(600, 200));
generationPanel.setMaximumSize(new Dimension(600, 200));
controlPanel.add(generationPanel);

// adding generate button
JButton resetButton = new JButton("Generate");
resetButton.setActionCommand("reset");
resetButton.addActionListener(this);
generationPanel.add(resetButton, new GridBagConstraints(0, 0, 2, 1, 1, 0.45,
    10, 1, new Insets(5, 5, 5, 5), 0, 0));

// adding width and height inputs
widthInput = new JTextArea("" + width);
widthInput.setPreferredSize(new Dimension(40, 30));
JPanel widthHolder = new JPanel();
widthHolder.add(widthInput);
widthHolder.setBorder(BorderFactory.createTitledBorder("width"));
generationPanel.add(widthHolder, new GridBagConstraints(2, 0, 2, 1, 1, 0.45,
    10, 1, new Insets(5, 5, 5, 5), 0, 0));

heightInput = new JTextArea("" + height);
heightInput.setPreferredSize(new Dimension(40, 30));
JPanel heightHolder = new JPanel();
heightHolder.add(heightInput);
heightHolder.setBorder(BorderFactory.createTitledBorder("height"));
generationPanel.add(heightHolder, new GridBagConstraints(4, 0, 2, 1, 1, 0.45,
    10, 1, new Insets(5, 5, 5, 5), 0, 0));

// adding world type dropdown
worldType = new JComboBox<>(new String[]
        {"mountain island", "flat land", "continent", "archipelago", "last custom"});
worldType.setSelectedIndex(0);
//worldType.setActionCommand("reset");
//worldType.addActionListener(this);
generationPanel.add(new JLabel("world type:"), new GridBagConstraints(0, 1, 2, 1,
    0.333, 0.20, 10, 1, new Insets(5, 5, 5, 5), 0, 0));
generationPanel.add(worldType, new GridBagConstraints(2, 1, 4, 1, 0.666, 0.25,
    10, 1, new Insets(5, 5, 5, 5), 0, 0));

// adding new window buttons
JButton openSelection = new JButton("Open Selection");
openSelection.setActionCommand("openSelection");
openSelection.addActionListener(this);
generationPanel.add(openSelection, new GridBagConstraints(0, 2, 3, 1, 0.5, 0.30,
    10, 1, new Insets(5, 5, 5, 5), 0, 0));

JButton newWindow = new JButton("New Window");
newWindow.setActionCommand("newWindow");
newWindow.addActionListener(this);
generationPanel.add(newWindow, new GridBagConstraints(3, 2, 3, 1, 0.5, 0.30,
    10, 1, new Insets(5, 5, 5, 5), 0, 0));

0 个答案:

没有答案