如何以及在何处绘制JPanels

时间:2018-11-27 21:17:31

标签: java swing jpanel awt

我正在编写一个小应用程序,我想要一个GUI,它将窗口分为两个部分,每个部分都将由JPanel控制。我想在左侧添加一个输入字段和一组按钮。我有代码:

public ModalModeller() {
    super("Modal Modeller"); 
    //setLayout(new GridBagLayout());
    setSize(1000, 500);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    font = new Font("Sans-serif", Font.PLAIN, 20);
    setUpTextInputField();
    setUpButtons();
    setUpPanels();
    setVisible(true);
}
public void setUpTextInputField() {
    inputField = new JTextField(30);
    inputField.addActionListener( // anonymous inner class
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    String s = inputField.getText();
                    ExprStr inputExpression = new ExprStr(s);
                    System.out.println(s);
                }
            });
    //add(inputField);
}
public void setUpButtons() {
    buttonPanel = new JPanel();
    buttonPanel.setLayout(new GridBagLayout());
    GridBagConstraints buttonConstraints = new GridBagConstraints();
    buttonConstraints.gridx = 0;
    buttonConstraints.gridy = GridBagConstraints.RELATIVE;
    buttonConstraints.anchor = GridBagConstraints.WEST;

    reflexB = new JRadioButton();
    reflexB.addActionListener(this);
    reflexB.setFont(font);

    transB = new JRadioButton();
    transB.addActionListener(this);
    transB.setFont(font);

    symmB = new JRadioButton();
    symmB.addActionListener(this);
    symmB.setFont(font);

    hereditaryB = new JRadioButton();
    hereditaryB.addActionListener(this);
    hereditaryB.setFont(font);

    buttonPanel.add(reflexB);
    buttonPanel.add(transB);
    buttonPanel.add(symmB);
    buttonPanel.add(hereditaryB);


}
public void setUpPanels() {
    leftPanel = new JPanel();
    rightPanel = new JPanel();
    leftPanel.setLayout(new GridBagLayout());
    rightPanel.setLayout(new GridBagLayout());

    leftPanel.setBackground(Color.black);


    GridBagConstraints leftConstraints = new GridBagConstraints();
    GridBagConstraints rightConstraints = new GridBagConstraints();

    leftConstraints.fill = GridBagConstraints.BOTH;
    leftConstraints.gridx = 0;
    leftConstraints.gridy = 0;
    leftPanel.add(inputField, leftConstraints);

    leftConstraints.fill = GridBagConstraints.BOTH;
    leftConstraints.gridx = 1;
    leftConstraints.gridy = 0;
    leftPanel.add(buttonPanel, leftConstraints);
    add(leftPanel);
    add(rightPanel);
}

我知道当我不在JPanel中绘制inputTextField时,它可以工作,但是由于某种原因,在我的窗口中都没有绘制两个JPanels。为什么是这样?我将它们添加到错误的位置了吗?我选择的布局有问题吗?

谢谢:)

2 个答案:

答案 0 :(得分:1)

好的,一连串的问题...

JFrame默认情况下使用BorderLayout,您似乎从未更改过它,这意味着当您先添加leftPanel然后再添加rightPanel时,右边的面板才是赢家并显示出来。

接下来,您在rightPanel上看不到任何内容的原因是因为您实际上没有在其中添加任何内容

leftConstraints.fill = GridBagConstraints.BOTH;
leftConstraints.gridx = 0;
leftConstraints.gridy = 0;
leftPanel.add(inputField, leftConstraints);

leftConstraints.fill = GridBagConstraints.BOTH;
leftConstraints.gridx = 1;
leftConstraints.gridy = 0;
leftPanel.add(buttonPanel, leftConstraints);

这两个块都将组件添加到leftPanel中。

在第二个步骤中,我认为您想做的事情更像是...

rightConstraints.fill = GridBagConstraints.BOTH;
rightConstraints.gridx = 1;
rightConstraints.gridy = 0;
rightPanel.add(buttonPanel, leftConstraints);

我纠正了所有这些问题后,您的代码就会正常工作

可运行示例...

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class ModalModeller extends JFrame {

  public static void main(String[] args) {
    new ModalModeller();
  }

  private JTextField inputField;
  private JPanel buttonPanel;
  private JRadioButton reflexB;
  private JRadioButton transB;
  private JRadioButton symmB;
  private JRadioButton hereditaryB;
  private JPanel leftPanel;
  private JPanel rightPanel;

  public ModalModeller() {
    super("Modal Modeller");
    setLayout(new GridLayout(0, 2));
    setSize(1000, 500);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setUpTextInputField();
    setUpButtons();
    setUpPanels();

    setVisible(true);
  }

  public void setUpTextInputField() {
    inputField = new JTextField(30);
    inputField.addActionListener( // anonymous inner class
            new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        String s = inputField.getText();
//      ExprStr inputExpression = new ExprStr(s);
        System.out.println(s);
      }
    });
    //add(inputField);
  }

  public void setUpButtons() {
    buttonPanel = new JPanel();
    buttonPanel.setLayout(new GridBagLayout());
    GridBagConstraints buttonConstraints = new GridBagConstraints();
    buttonConstraints.gridx = 0;
    buttonConstraints.gridy = GridBagConstraints.RELATIVE;
    buttonConstraints.anchor = GridBagConstraints.WEST;

    reflexB = new JRadioButton();
//  reflexB.addActionListener(this);

    transB = new JRadioButton();
//  transB.addActionListener(this);

    symmB = new JRadioButton();
//  symmB.addActionListener(this);

    hereditaryB = new JRadioButton();
//  hereditaryB.addActionListener(this);

    buttonPanel.add(reflexB);
    buttonPanel.add(transB);
    buttonPanel.add(symmB);
    buttonPanel.add(hereditaryB);

  }

  public void setUpPanels() {
    leftPanel = new JPanel();
    rightPanel = new JPanel();
    leftPanel.setLayout(new GridBagLayout());
    rightPanel.setLayout(new GridBagLayout());

    leftPanel.setBackground(Color.black);

    GridBagConstraints leftConstraints = new GridBagConstraints();
    GridBagConstraints rightConstraints = new GridBagConstraints();

    leftConstraints.fill = GridBagConstraints.BOTH;
    leftConstraints.gridx = 0;
    leftConstraints.gridy = 0;
    leftPanel.add(inputField, leftConstraints);

    rightConstraints.fill = GridBagConstraints.BOTH;
    rightConstraints.gridx = 1;
    rightConstraints.gridy = 0;
    rightPanel.add(buttonPanel, leftConstraints);
    add(leftPanel);
    add(rightPanel);
  }
}

答案 1 :(得分:1)

  

但是由于某些原因,在我的窗口中都没有绘制两个JPanels

不确定为什么它们都不显示,但是我知道为什么左侧面板不显示。

据我所知,您正在使用JFrame。 JFrame的默认布局管理器是BorderLayout。您尝试使用以下方法将两个面板添加到框架中:

add(leftPanel);
add(rightPanel);

但是,当您将组件添加到BorderLayout而不指定约束时,该组件将被添加到CENTER。但是,只能将一个组件添加到CENTER,因此BorderLayout仅管理最后添加的组件。

  

我想要一个将窗口分为两个部分的gui

因此,我不是基于要创建的所有面板来构造代码,而是要基于要添加到框架中的面板来构造代码。

因此,构造函数中的代码将类似于:

add(createLeftPane(), BorderLayout.LINE_START);
add(createCenterPanel(), BorderLayout.CENTER);

因此,现在从较高的层次上来说,您知道在框架中添加了两个主面板。

然后,代码可能类似于:

private JPanel createLeftPanel()
{
    JPanel panel = new JPanel();
    panel.add( new JButton("Left Panel Button") );
    return panel;
}

您将为createCenterPanel()方法添加类似的代码。

然后,在显示两个顶级面板后,您可以使用任何要使用的布局管理器将实际组件添加到每个面板中。

表单设计应该是一种迭代方法。