组件无法使用GridBagLayout正确显示

时间:2019-09-07 15:08:20

标签: java swing layout-manager gridbaglayout

我正在学习Java swing,这让我感到非常困惑。退出按钮不显示。但是,如果我将ID VALUE1 VALUE2 VALUE3 1 google [ab,c] Good 2 First [ab,c1] Good1 3 NaN [ab,c] Good 的代码部分移到按钮的两个部分之后,它将正确显示。那为什么呢?

textArea

1 个答案:

答案 0 :(得分:2)

简单:添加JScrollPane之后,您忘记了重置c.gridheight = 1;。如果不这样做,发送按钮将覆盖退出按钮。

private void launchFrame() {
    panel = new JPanel(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();

    c.fill = GridBagConstraints.HORIZONTAL;  // ** This is also worthwhile **

    textArea = new JTextArea(10, 50);
    scrollPane = new JScrollPane(textArea);
    c.gridx = 0;
    c.gridy = 0;
    c.gridheight = 3;
    panel.add(scrollPane, c);

    btnSend = new JButton("Send");
    c.gridx = 1;
    c.gridy = 0;
    c.gridheight = 1;  // ********* ADD THIS *********
    c.anchor = GridBagConstraints.NORTH;
    panel.add(btnSend, c);

    btnQuit = new JButton("Quit");
    c.gridx = 1;
    c.gridy = 1;
    c.anchor = GridBagConstraints.NORTH;
    panel.add(btnQuit, c);

}