当键盘不在焦点时,Swing组件会自行调整大小

时间:2012-04-08 16:14:53

标签: java swing

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

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

After I click off the gui

以下是我用于将JTextArea添加到Panel的代码:

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);

这是我用来向Panel添加文本区域的代码:

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

2 个答案:

答案 0 :(得分: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添加文本之前和之后,它看起来像:
enter image description here enter image description here

点击标签后:
enter image description here

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

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

运行时看起来像:
enter image description here

在Mac OS上看起来也不错:

enter image description here

答案 1 :(得分: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);
        }
    }
}