请看下面的代码
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()
,但它让一切变得更糟!