首页 > 解决方案 > 如何以及在何处绘制 JPanel

问题描述

我正在编写一个小应用程序,其中我想要一个将窗口分成两个部分的 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 时它可以工作,但由于某种原因,我的窗口中都没有绘制任何 JPanel。为什么是这样?我是否将它们添加到错误的位置?我选择的布局有问题吗?

谢谢 :)

标签: javaswingjpanelawt

解决方案


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

JFrame默认情况下使用 a 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);
  }
}

推荐阅读