混乱的GUI:很多空白

时间:2012-09-16 14:11:32

标签: java swing user-interface jtextarea gridbaglayout

请查看以下代码

package email;

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

public class SendEmail extends JDialog
{
    private JLabel to, cc, bcc, subject, account;
    private JTextField toTxt, ccTxt, bccTxt, subjectTxt;
    private JTextArea messageTxt;
    private JButton send;

    private JComboBox accountBox;

    private JScrollPane scroll;

    private GridBagLayout gbl;
    private GridBagConstraints gbc;

    public SendEmail()
    {
        //Declaring instance variables
        to = new JLabel("To: ");
        cc = new JLabel("CC: ");
        bcc = new JLabel("BCC: ");
        subject = new JLabel("Subject: ");
        account = new JLabel("Select an Account: ");

        toTxt = new JTextField(20);
        ccTxt = new JTextField(20);
        bccTxt = new JTextField(20);
        subjectTxt = new JTextField(20);

        messageTxt = new JTextArea(500,500);
        scroll = new JScrollPane(messageTxt);


        accountBox = new JComboBox();
        accountBox.addItem("Yahoo");
        accountBox.addItem("GMail");
        accountBox.addItem("MSN");
        //accountBox.addItem("Yahoo");
        //accountBox.addItem("Yahoo");



        //Creating thr GUI
        gbl = new GridBagLayout();
        gbc = new GridBagConstraints();

        this.setLayout(gbl);


        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.BOTH;
        this.add(account,gbc);

        gbc.gridx = 2;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.BOTH;
        this.add(accountBox,gbc);


        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.BOTH;
        this.add(to,gbc);

        gbc.gridx = 2;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.BOTH;
        this.add(toTxt,gbc);

        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.fill = GridBagConstraints.BOTH;
        this.add(bcc,gbc);

        gbc.gridx = 2;
        gbc.gridy = 3;
        gbc.fill = GridBagConstraints.BOTH;
        this.add(bccTxt,gbc);

        gbc.gridx = 1;
        gbc.gridy = 4;
        gbc.fill = GridBagConstraints.BOTH;
        this.add(cc,gbc);

        gbc.gridx = 2;
        gbc.gridy = 4;
        gbc.fill = GridBagConstraints.BOTH;
        this.add(ccTxt,gbc);

        gbc.gridx = 1;
        gbc.gridy = 5;
        gbc.fill = GridBagConstraints.BOTH;
        this.add(subject,gbc);

        gbc.gridx = 2;
        gbc.gridy = 5;
        gbc.fill = GridBagConstraints.BOTH;
        this.add(subjectTxt,gbc);

        gbc.gridx = 1;
        gbc.gridy = 6;
        gbc.fill = GridBagConstraints.BOTH;
        this.add(scroll,gbc);


        this.setSize(new Dimension(200,500));
        this.setVisible(true);


    }
}

在这里,GUI看起来非常糟糕。我需要为所有领域留出足够的空间。在JTextArea中,您可以看到它甚至都不可见。我需要它的高度为2列;似乎所有这些问题都在发生,因为它试图将大JTextArea设置为一列。为了您的信息,这是一个“撰写电子邮件”GUI,因此您可以清楚我要问的内容。我使用this.pack(),但它使一切都变得最糟糕!

1 个答案:

答案 0 :(得分:4)

正如我在评论中提到的那样:

  

请勿致电setSize()。取而代之的是pack() GUI,让组件的自然首选大小和布局决定了GUI的大小。如果pack()使事情变得更糟,那么你应该从事情的这一方面解决问题,而不是通过调用setSize()

例如,我会

  • 使JTextArea成为合理的大小,而不是500行乘500列!
  • 不要设置尺寸
  • 请致电pack()
  • 不要忘记使用weightx和weighty(添加到代码中)和insets(尚未添加)。
  • 避免使用魔法数字。
  • 考虑嵌套更容易使用布局并最大限度地减少GridBagLayout的使用。
  • 为JScrollPane提供额外的网格宽度,以允许它填满底部。例如:

SendEmail.java:

import java.awt.*;
import java.util.HashMap;
import java.util.Map;
import javax.swing.*;

@SuppressWarnings("serial")
public class SendEmail extends JDialog {
   public final static String[] LABEL_TEXTS = { "Select an Account:", "To:",
         "BCC:", "CC:", "Subject:" };
   public final static String[] ACCOUNT_TEXTS = { "Yahoo", "GMail", "MSN" };
   private static final int TEXT_FIELD_LENGTH = 20;
   private static final int T_AREA_ROWS = 20;
   private static final int T_AREA_COLS = 50;
   private static final int INSET_GAP = 1;
   private static final int RIGHT_INSET_GAP = 15;
   private Map<String, JTextField> fieldMap = new HashMap<String, JTextField>();
   private JTextArea messageTxt;
   private JButton send;

   private JComboBox<String> accountBox;

   private JScrollPane scroll;

   public SendEmail(JFrame frame) {
      super(frame, "Dialog", true);
      messageTxt = new JTextArea(T_AREA_ROWS, T_AREA_COLS);
      scroll = new JScrollPane(messageTxt);
      accountBox = new JComboBox<String>(ACCOUNT_TEXTS);

      this.setLayout(new GridBagLayout());
      int ebGap = 5;
      ((JPanel) getContentPane()).setBorder(BorderFactory.createEmptyBorder(
            ebGap, ebGap, ebGap, ebGap));

      for (int i = 0; i < LABEL_TEXTS.length; i++) {
         JLabel label = new JLabel(LABEL_TEXTS[i]);
         addLabel(0, i, label);

         if (i == 0) {
            addField(1, i, accountBox);
         } else {
            JTextField tField = new JTextField(TEXT_FIELD_LENGTH);
            fieldMap.put(LABEL_TEXTS[i], tField);
            addField(1, i, tField);
         }
      }

      GridBagConstraints gbc = new GridBagConstraints();
      gbc.gridx = 0;
      gbc.gridy = LABEL_TEXTS.length;
      gbc.gridwidth = 2;
      gbc.weightx = 1.0;
      gbc.weighty = 1.0;
      gbc.fill = GridBagConstraints.BOTH;
      gbc.insets = new Insets(INSET_GAP, INSET_GAP, INSET_GAP, INSET_GAP);
      this.add(scroll, gbc);

      pack();
      this.setVisible(true);
   }

   private void addField(int x, int y, JComponent comp) {
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.gridx = x;
      gbc.gridy = y;
      gbc.weightx = 1.0;
      gbc.weighty = 0.0;
      gbc.fill = GridBagConstraints.HORIZONTAL;
      gbc.insets = new Insets(INSET_GAP, INSET_GAP, INSET_GAP, INSET_GAP);
      gbc.anchor = GridBagConstraints.EAST;

      this.add(comp, gbc);
   }

   private void addLabel(int x, int y, JComponent comp) {
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.gridx = x;
      gbc.gridy = y;
      gbc.weightx = 0.0;
      gbc.weighty = 0.0;
      gbc.fill = GridBagConstraints.BOTH;
      gbc.insets = new Insets(INSET_GAP, INSET_GAP, INSET_GAP, RIGHT_INSET_GAP);
      gbc.anchor = GridBagConstraints.EAST;

      this.add(comp, gbc);
   }

   public String getTextFieldText(String key) {
      JTextField tField = fieldMap.get(key);
      if (tField == null) {
         String text = "key, " + key + " not a valid argument for fieldMap";
         throw new IllegalArgumentException(text);
      }

      return tField.getText();
   }

   public String getAccountText() {
      return accountBox.getSelectedItem().toString();
   }

   public String getMessageTxt() {
      return messageTxt.getText();
   }

   public static void main(String[] args) {
      JFrame frame = new JFrame();
      frame.pack();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      new SendEmail(frame);
      frame.dispose();
   }
}

运行时看起来像:

enter image description here