3

我的 gui 布局有两个问题。我使用 JTabbedPane 来容纳两个 JPanel,每个面板都有一个按钮和文本区域的选择,每个面板都使用 GridBagLayout 进行布局。在我的一个面板中,我有一个使用 JTextArea 的 JScrollPane。当我将任何内容附加到此文本区域然后单击 gui 使其不再具有焦点时,或者如果我更改选项卡,所有文本字段和文本的大小都会更改为尽可能小。

为了进一步说明我的问题,这里是我在附加到文本区域后单击 gui 之前和之后的图片: 在我点击 gui 之前

在我点击 gui 之后

这是我用来将 JTextArea 添加到面板的代码:

table = new JTextArea();
    table.setEditable(false);
    JScrollPane sp = new JScrollPane(table);
    sp.setSize(40, 10);
    c.insets = new Insets(10,10,10,10);
    c.gridx = 1;
    c.gridwidth = 4;
    c.gridy = 7;
    c.gridheight = 7;
    this.add(sp, c);

这是我用来将文本区域添加到面板的代码:

title = new JTextField(10);
    author = new JTextField(10);
    dueDate = new JTextField(10);
    setDate = new JTextField(10);
    setWeighting = new JTextField(10);

    c.gridx = 2;
    c.gridy = 1;
    this.add(title, c);//add title field
    c.gridx = 2;
    c.gridy = 2;
    this.add(author, c);//add author field
    c.gridx = 2;
    c.gridy = 3;
    this.add(dueDate, c);//add dueDate field
    c.gridx = 2;
    c.gridy = 4;
    this.add(setDate, c);//add setDate field
    c.gridx = 2;
    c.gridy = 5;
    this.add(setWeighting, c);//add set Weighting field
4

2 回答 2

3

我能够像这样部分重现您的问题:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Foo002 {

   private static final int ROWS = 5;

   private static void createAndShowGui() {
      JPanel assignmentsPanel = new JPanel(new GridBagLayout());
      final JTextArea textarea = new JTextArea(ROWS, 20);

      GridBagConstraints c = new GridBagConstraints();
      int insetGap = 2;
      c.insets = new Insets(insetGap, insetGap, insetGap, insetGap);
      c.fill = GridBagConstraints.HORIZONTAL;
      c.gridwidth = 1;
      c.gridheight = 1;
      c.weightx = 1.0;
      c.weighty = 1.0;
      String[] labels = { "title", "author", "date due", "date set",
            "set weighting" };
      int row = 0;
      for (int i = 0; i < labels.length; i++) {
         JLabel label = new JLabel(labels[i], SwingConstants.CENTER);
         c.gridx = 0;
         c.gridy = i;
         assignmentsPanel.add(label, c);
         c.gridx = 1;
         JTextField textfield = new JTextField(10);
         assignmentsPanel.add(textfield, c);

         label.setPreferredSize(textfield.getPreferredSize());
         row++;
      }
      c.gridx = 0;
      c.gridy = row;
      c.fill = GridBagConstraints.HORIZONTAL;
      Action myAction = new AbstractAction("Fill Area") {

         @Override
         public void actionPerformed(ActionEvent arg0) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < 10; i++) {
               sb.append("foo bar bif baz spam\n");
            }
            textarea.setText(sb.toString());
         }
      };
      assignmentsPanel.add(new JButton(myAction), c);
      c.gridx = 1;
      assignmentsPanel.add(new JButton("Button 2"), c);
      row++;

      c.gridx = 0;
      c.gridy = row;
      c.gridwidth = 2;
      c.gridheight = ROWS;

      JScrollPane scrollpane = new JScrollPane(textarea);

      assignmentsPanel.add(scrollpane, c);

      JTabbedPane tabbedPanel = new JTabbedPane();
      tabbedPanel.add("Assignments", assignmentsPanel);
      tabbedPanel.add("Modules", new JPanel());

      JOptionPane.showMessageDialog(null, tabbedPanel, "Foo",
            JOptionPane.PLAIN_MESSAGE);

   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

在向 JTextArea 添加文本之前和之后,它看起来像:
在此处输入图像描述 在此处输入图像描述

点击标签后:
在此处输入图像描述

但是可以通过给 JScrollPane 一个垂直滚动条来修复它:

  // JScrollPane scrollpane = new JScrollPane(textarea);
  JScrollPane scrollpane = new JScrollPane(textarea,
        JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
        JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

运行时看起来像:
在此处输入图像描述

在 Mac OS 上看起来也不错:

在此处输入图像描述

于 2012-04-08T17:11:43.790 回答
1

您可以尝试避免使用 Gridbag 并改用嵌套的 JPanel。我冒昧地创建了一个简短的示例:

public class LilrooPanel extends JPanel
{
    private static final int GAP = 5;

    public static void main(String[] args){
        JFrame main = new JFrame("Dims");
        JTabbedPane tabbed = new JTabbedPane(); 
        JPanel myPanel = new LilrooPanel();
        tabbed.add("Assignments", myPanel);
        tabbed.add("Modules", new JPanel());
        main.setContentPane(tabbed);
        main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        main.setSize(400, 400);
        main.setLocationRelativeTo(null);
        main.setVisible(true);
    }

    public LilrooPanel(){
        super(new BorderLayout(0, GAP));
        setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
        Box north = new Box(BoxLayout.Y_AXIS);

        north.add(new BorderPanel("Assignment Title", new JTextField()));
        north.add(Box.createRigidArea(new Dimension(0, GAP)));
        north.add(new BorderPanel("Author", new JTextField()));
        north.add(Box.createRigidArea(new Dimension(0, GAP)));
        north.add(new BorderPanel("Date Due", new JTextField()));
        north.add(Box.createRigidArea(new Dimension(0, GAP)));
        north.add(new BorderPanel("Date Set", new JTextField()));
        north.add(Box.createRigidArea(new Dimension(0, GAP)));
        north.add(new BorderPanel("Set Weighting", new JTextField()));
        north.add(Box.createRigidArea(new Dimension(0, GAP)));

        JPanel buttonsPanel = new JPanel();
        buttonsPanel.add(new JButton("Add Assignment"));
        buttonsPanel.add(new JButton("Remove Assignment"));
        north.add(buttonsPanel);

        add(north, BorderLayout.NORTH);

        add(new JScrollPane(new JTable(new Object[][]{}, new Object[]{"ModTitle", "ModId", "Assignments"})));
    }

    private static class BorderPanel extends JPanel
    {
        private static final Dimension LABELS_WIDTH = new Dimension(100, 0);

        public BorderPanel(String label, JComponent right){
            super(new BorderLayout(GAP, 0));
            JLabel jLabel = new JLabel(label);
            jLabel.setPreferredSize(LABELS_WIDTH);
            add(jLabel, BorderLayout.WEST);
            add(right);
        }
    }
}
于 2012-04-08T18:27:17.627 回答