Java JScrollPane的底部被切断

Java JScrollPane的底部被切断,java,swing,user-interface,jscrollpane,jtextarea,Java,Swing,User Interface,Jscrollpane,Jtextarea,我正在尝试创建一个简单的电子邮件客户端,而身体的底部正在被切断。如果我添加了一个水平滚动条,它不会出现,垂直滚动条的底部也不会出现 这是我的密码: import java.awt.BorderLayout; import java.awt.Container; import java.awt.FlowLayout; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JLa

我正在尝试创建一个简单的电子邮件客户端,而身体的底部正在被切断。如果我添加了一个水平滚动条,它不会出现,垂直滚动条的底部也不会出现

这是我的密码:

   import java.awt.BorderLayout;
   import java.awt.Container;
   import java.awt.FlowLayout;
   import java.awt.Font;

   import javax.swing.JFrame;
   import javax.swing.JLabel;
   import javax.swing.JPanel;
   import javax.swing.JScrollBar;
   import javax.swing.JScrollPane;
   import javax.swing.JTextArea;
   import javax.swing.JTextField;
   import javax.swing.UIManager;


   @SuppressWarnings("serial")
   public class gui extends JFrame{

gui(String title, int x, int y){

    super(title);
    setSize(x,y);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setResizable(false);

}

public void addElements(){

    Font size30 = new Font(null, Font.PLAIN, 30);

    JPanel pnl = new JPanel();

    Container contentPane = getContentPane();

    //--- User Info ---//

    JPanel userInfo = new JPanel();

    JLabel userLabel = new JLabel("Username: ");
    JTextField userField = new JTextField(12);
    userInfo.add(userLabel);
    userInfo.add(userField);

    JLabel passLabel = new JLabel("Password: ");
    JTextField passField = new JTextField(10);
    userInfo.add(passLabel);
    userInfo.add(passField);

    JLabel serverLabel = new JLabel("Mail Server: ");
    JTextField serverField = new JTextField(10);
    userInfo.add(serverLabel);
    userInfo.add(serverField);

    JLabel portLabel = new JLabel("Server Port: ");
    JTextField portField = new JTextField(3);
    userInfo.add(portLabel);
    userInfo.add(portField);

    //--- To: CC: and Subject Fields ---//

    JPanel msgInfo = new JPanel();

    JLabel toLabel = new JLabel("To: ");
    JTextField toField = new JTextField(30);
    msgInfo.add(toLabel);
    msgInfo.add(toField);

    JLabel subLabel = new JLabel("Subject: ");
    JTextField subField = new JTextField(30);
    msgInfo.add(subLabel);
    msgInfo.add(subField);

    //--- Body ---//

    JPanel bodyPnl = new JPanel(new BorderLayout(10,10));

    JLabel bodyLabel = new JLabel("Body");
    bodyLabel.setFont(size30);

    JTextArea bodyField = new JTextArea(30,70);
    bodyField.setLineWrap(true);
    bodyField.setWrapStyleWord(true);

    JScrollPane bodyScroll = new JScrollPane(bodyField);

    bodyScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    bodyScroll.setBounds(getX(), getY(), bodyField.getWidth(), bodyField.getHeight());

    bodyPnl.add("South",bodyScroll);

    pnl.add(userInfo);
    pnl.add(msgInfo);
    pnl.add(bodyLabel);
    pnl.add(bodyScroll);

    contentPane.add("North", pnl);

    setVisible(true);

}

}

在我的主类中,我只是创建了一个新的gui,然后调用addElements()函数。

FlowLayout
没有很好地“包装”。考虑不同的布局,<代码> GridBagLayout <代码>,例如……/P> 此外,停止“尝试”在UI上强制设置大小,因为您无法控制影响大小的因素

相反,依赖布局管理器和API功能。例如,与其在您的框架上调用
setSize
,不如调用
pack
…我很快就会发布,但我花了这么长时间才找到那个调用…我挠头想知道为什么UI的布局不符合我的预期

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;

public class Test extends JFrame {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                Test frame = new Test("Testing", 400, 400);
            }
        });
    }

    Test(String title, int x, int y) {

        super(title);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        addElements();
        pack();
        setVisible(true);
//        setResizable(false);

    }

    public void addElements() {

        Font size30 = new Font(null, Font.PLAIN, 30);

        //--- User Info ---//
        JPanel userInfo = new JPanel(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(2, 2, 4, 2);
        gbc.gridx = 0;
        gbc.gridy = 0;
        JLabel userLabel = new JLabel("Username: ");
        JTextField userField = new JTextField(12);
        userInfo.add(userLabel, gbc);
        gbc.gridx++;
        userInfo.add(userField, gbc);

        JLabel passLabel = new JLabel("Password: ");
        JTextField passField = new JTextField(10);
        gbc.gridx++;
        userInfo.add(passLabel, gbc);
        gbc.gridx++;
        userInfo.add(passField, gbc);

        JLabel serverLabel = new JLabel("Mail Server: ");
        JTextField serverField = new JTextField(10);
        gbc.gridx++;
        userInfo.add(serverLabel, gbc);
        gbc.gridx++;
        userInfo.add(serverField, gbc);

        JLabel portLabel = new JLabel("Server Port: ");
        JTextField portField = new JTextField(3);
        gbc.gridx++;
        userInfo.add(portLabel, gbc);
        gbc.gridx++;
        userInfo.add(portField, gbc);

        gbc = new GridBagConstraints();
        gbc.insets = new Insets(2, 2, 4, 2);
        gbc.gridx = 0;
        gbc.gridy = 0;
        //--- To: CC: and Subject Fields ---//
        JPanel msgInfo = new JPanel(new GridBagLayout());

        JLabel toLabel = new JLabel("To: ");
        JTextField toField = new JTextField(30);
        msgInfo.add(toLabel, gbc);
        gbc.gridx++;
        msgInfo.add(toField, gbc);

        JLabel subLabel = new JLabel("Subject: ");
        JTextField subField = new JTextField(30);
        gbc.gridx++;
        msgInfo.add(subLabel, gbc);
        gbc.gridx++;
        msgInfo.add(subField, gbc);

        //--- Body ---//
//        JPanel bodyPnl = new JPanel(new GridBagLayout());
//        gbc = new GridBagConstraints();
//        gbc.insets = new Insets(2, 2, 4, 2);
//        gbc.gridx = 0;
//        gbc.gridy = 0;

        JLabel bodyLabel = new JLabel("Body");
        bodyLabel.setHorizontalAlignment(JLabel.CENTER);
        bodyLabel.setFont(size30);

        JTextArea bodyField = new JTextArea(30, 70);
        bodyField.setLineWrap(true);
        bodyField.setWrapStyleWord(true);

        JScrollPane bodyScroll = new JScrollPane(bodyField);

        bodyScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
//        bodyScroll.setBounds(getX(), getY(), bodyField.getWidth(), bodyField.getHeight());

        setLayout(new GridBagLayout());
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;

        add(userInfo, gbc);
        gbc.gridy++;
        add(msgInfo, gbc);
        gbc.gridy++;
        gbc.insets = new Insets(10, 10, 4, 10);
        add(bodyLabel, gbc);
        gbc.gridy++;
        gbc.insets = new Insets(4, 10, 10, 10);
        gbc.weighty = 1;
        gbc.fill = GridBagConstraints.BOTH;
        add(bodyScroll, gbc);

    }

}


FlowLayout
不能很好地“包装”。考虑不同的布局,<代码> GridBagLayout <代码>,例如……/P> 此外,停止“尝试”在UI上强制设置大小,因为您无法控制影响大小的因素

相反,依赖布局管理器和API功能。例如,与其在您的框架上调用
setSize
,不如调用
pack
…我很快就会发布,但我花了这么长时间才找到那个调用…我挠头想知道为什么UI的布局不符合我的预期

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;

public class Test extends JFrame {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                Test frame = new Test("Testing", 400, 400);
            }
        });
    }

    Test(String title, int x, int y) {

        super(title);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        addElements();
        pack();
        setVisible(true);
//        setResizable(false);

    }

    public void addElements() {

        Font size30 = new Font(null, Font.PLAIN, 30);

        //--- User Info ---//
        JPanel userInfo = new JPanel(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(2, 2, 4, 2);
        gbc.gridx = 0;
        gbc.gridy = 0;
        JLabel userLabel = new JLabel("Username: ");
        JTextField userField = new JTextField(12);
        userInfo.add(userLabel, gbc);
        gbc.gridx++;
        userInfo.add(userField, gbc);

        JLabel passLabel = new JLabel("Password: ");
        JTextField passField = new JTextField(10);
        gbc.gridx++;
        userInfo.add(passLabel, gbc);
        gbc.gridx++;
        userInfo.add(passField, gbc);

        JLabel serverLabel = new JLabel("Mail Server: ");
        JTextField serverField = new JTextField(10);
        gbc.gridx++;
        userInfo.add(serverLabel, gbc);
        gbc.gridx++;
        userInfo.add(serverField, gbc);

        JLabel portLabel = new JLabel("Server Port: ");
        JTextField portField = new JTextField(3);
        gbc.gridx++;
        userInfo.add(portLabel, gbc);
        gbc.gridx++;
        userInfo.add(portField, gbc);

        gbc = new GridBagConstraints();
        gbc.insets = new Insets(2, 2, 4, 2);
        gbc.gridx = 0;
        gbc.gridy = 0;
        //--- To: CC: and Subject Fields ---//
        JPanel msgInfo = new JPanel(new GridBagLayout());

        JLabel toLabel = new JLabel("To: ");
        JTextField toField = new JTextField(30);
        msgInfo.add(toLabel, gbc);
        gbc.gridx++;
        msgInfo.add(toField, gbc);

        JLabel subLabel = new JLabel("Subject: ");
        JTextField subField = new JTextField(30);
        gbc.gridx++;
        msgInfo.add(subLabel, gbc);
        gbc.gridx++;
        msgInfo.add(subField, gbc);

        //--- Body ---//
//        JPanel bodyPnl = new JPanel(new GridBagLayout());
//        gbc = new GridBagConstraints();
//        gbc.insets = new Insets(2, 2, 4, 2);
//        gbc.gridx = 0;
//        gbc.gridy = 0;

        JLabel bodyLabel = new JLabel("Body");
        bodyLabel.setHorizontalAlignment(JLabel.CENTER);
        bodyLabel.setFont(size30);

        JTextArea bodyField = new JTextArea(30, 70);
        bodyField.setLineWrap(true);
        bodyField.setWrapStyleWord(true);

        JScrollPane bodyScroll = new JScrollPane(bodyField);

        bodyScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
//        bodyScroll.setBounds(getX(), getY(), bodyField.getWidth(), bodyField.getHeight());

        setLayout(new GridBagLayout());
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;

        add(userInfo, gbc);
        gbc.gridy++;
        add(msgInfo, gbc);
        gbc.gridy++;
        gbc.insets = new Insets(10, 10, 4, 10);
        add(bodyLabel, gbc);
        gbc.gridy++;
        gbc.insets = new Insets(4, 10, 10, 10);
        gbc.weighty = 1;
        gbc.fill = GridBagConstraints.BOTH;
        add(bodyScroll, gbc);

    }

}


FlowLayout
不能很好地“包装”。考虑不同的布局,<代码> GridBagLayout <代码>,例如……/P> 此外,停止“尝试”在UI上强制设置大小,因为您无法控制影响大小的因素

相反,依赖布局管理器和API功能。例如,与其在您的框架上调用
setSize
,不如调用
pack
…我很快就会发布,但我花了这么长时间才找到那个调用…我挠头想知道为什么UI的布局不符合我的预期

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;

public class Test extends JFrame {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                Test frame = new Test("Testing", 400, 400);
            }
        });
    }

    Test(String title, int x, int y) {

        super(title);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        addElements();
        pack();
        setVisible(true);
//        setResizable(false);

    }

    public void addElements() {

        Font size30 = new Font(null, Font.PLAIN, 30);

        //--- User Info ---//
        JPanel userInfo = new JPanel(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(2, 2, 4, 2);
        gbc.gridx = 0;
        gbc.gridy = 0;
        JLabel userLabel = new JLabel("Username: ");
        JTextField userField = new JTextField(12);
        userInfo.add(userLabel, gbc);
        gbc.gridx++;
        userInfo.add(userField, gbc);

        JLabel passLabel = new JLabel("Password: ");
        JTextField passField = new JTextField(10);
        gbc.gridx++;
        userInfo.add(passLabel, gbc);
        gbc.gridx++;
        userInfo.add(passField, gbc);

        JLabel serverLabel = new JLabel("Mail Server: ");
        JTextField serverField = new JTextField(10);
        gbc.gridx++;
        userInfo.add(serverLabel, gbc);
        gbc.gridx++;
        userInfo.add(serverField, gbc);

        JLabel portLabel = new JLabel("Server Port: ");
        JTextField portField = new JTextField(3);
        gbc.gridx++;
        userInfo.add(portLabel, gbc);
        gbc.gridx++;
        userInfo.add(portField, gbc);

        gbc = new GridBagConstraints();
        gbc.insets = new Insets(2, 2, 4, 2);
        gbc.gridx = 0;
        gbc.gridy = 0;
        //--- To: CC: and Subject Fields ---//
        JPanel msgInfo = new JPanel(new GridBagLayout());

        JLabel toLabel = new JLabel("To: ");
        JTextField toField = new JTextField(30);
        msgInfo.add(toLabel, gbc);
        gbc.gridx++;
        msgInfo.add(toField, gbc);

        JLabel subLabel = new JLabel("Subject: ");
        JTextField subField = new JTextField(30);
        gbc.gridx++;
        msgInfo.add(subLabel, gbc);
        gbc.gridx++;
        msgInfo.add(subField, gbc);

        //--- Body ---//
//        JPanel bodyPnl = new JPanel(new GridBagLayout());
//        gbc = new GridBagConstraints();
//        gbc.insets = new Insets(2, 2, 4, 2);
//        gbc.gridx = 0;
//        gbc.gridy = 0;

        JLabel bodyLabel = new JLabel("Body");
        bodyLabel.setHorizontalAlignment(JLabel.CENTER);
        bodyLabel.setFont(size30);

        JTextArea bodyField = new JTextArea(30, 70);
        bodyField.setLineWrap(true);
        bodyField.setWrapStyleWord(true);

        JScrollPane bodyScroll = new JScrollPane(bodyField);

        bodyScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
//        bodyScroll.setBounds(getX(), getY(), bodyField.getWidth(), bodyField.getHeight());

        setLayout(new GridBagLayout());
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;

        add(userInfo, gbc);
        gbc.gridy++;
        add(msgInfo, gbc);
        gbc.gridy++;
        gbc.insets = new Insets(10, 10, 4, 10);
        add(bodyLabel, gbc);
        gbc.gridy++;
        gbc.insets = new Insets(4, 10, 10, 10);
        gbc.weighty = 1;
        gbc.fill = GridBagConstraints.BOTH;
        add(bodyScroll, gbc);

    }

}


FlowLayout
不能很好地“包装”。考虑不同的布局,<代码> GridBagLayout <代码>,例如……/P> 此外,停止“尝试”在UI上强制设置大小,因为您无法控制影响大小的因素

相反,依赖布局管理器和API功能。例如,与其在您的框架上调用
setSize
,不如调用
pack
…我很快就会发布,但我花了这么长时间才找到那个调用…我挠头想知道为什么UI的布局不符合我的预期

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;

public class Test extends JFrame {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                Test frame = new Test("Testing", 400, 400);
            }
        });
    }

    Test(String title, int x, int y) {

        super(title);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        addElements();
        pack();
        setVisible(true);
//        setResizable(false);

    }

    public void addElements() {

        Font size30 = new Font(null, Font.PLAIN, 30);

        //--- User Info ---//
        JPanel userInfo = new JPanel(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(2, 2, 4, 2);
        gbc.gridx = 0;
        gbc.gridy = 0;
        JLabel userLabel = new JLabel("Username: ");
        JTextField userField = new JTextField(12);
        userInfo.add(userLabel, gbc);
        gbc.gridx++;
        userInfo.add(userField, gbc);

        JLabel passLabel = new JLabel("Password: ");
        JTextField passField = new JTextField(10);
        gbc.gridx++;
        userInfo.add(passLabel, gbc);
        gbc.gridx++;
        userInfo.add(passField, gbc);

        JLabel serverLabel = new JLabel("Mail Server: ");
        JTextField serverField = new JTextField(10);
        gbc.gridx++;
        userInfo.add(serverLabel, gbc);
        gbc.gridx++;
        userInfo.add(serverField, gbc);

        JLabel portLabel = new JLabel("Server Port: ");
        JTextField portField = new JTextField(3);
        gbc.gridx++;
        userInfo.add(portLabel, gbc);
        gbc.gridx++;
        userInfo.add(portField, gbc);

        gbc = new GridBagConstraints();
        gbc.insets = new Insets(2, 2, 4, 2);
        gbc.gridx = 0;
        gbc.gridy = 0;
        //--- To: CC: and Subject Fields ---//
        JPanel msgInfo = new JPanel(new GridBagLayout());

        JLabel toLabel = new JLabel("To: ");
        JTextField toField = new JTextField(30);
        msgInfo.add(toLabel, gbc);
        gbc.gridx++;
        msgInfo.add(toField, gbc);

        JLabel subLabel = new JLabel("Subject: ");
        JTextField subField = new JTextField(30);
        gbc.gridx++;
        msgInfo.add(subLabel, gbc);
        gbc.gridx++;
        msgInfo.add(subField, gbc);

        //--- Body ---//
//        JPanel bodyPnl = new JPanel(new GridBagLayout());
//        gbc = new GridBagConstraints();
//        gbc.insets = new Insets(2, 2, 4, 2);
//        gbc.gridx = 0;
//        gbc.gridy = 0;

        JLabel bodyLabel = new JLabel("Body");
        bodyLabel.setHorizontalAlignment(JLabel.CENTER);
        bodyLabel.setFont(size30);

        JTextArea bodyField = new JTextArea(30, 70);
        bodyField.setLineWrap(true);
        bodyField.setWrapStyleWord(true);

        JScrollPane bodyScroll = new JScrollPane(bodyField);

        bodyScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
//        bodyScroll.setBounds(getX(), getY(), bodyField.getWidth(), bodyField.getHeight());

        setLayout(new GridBagLayout());
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;

        add(userInfo, gbc);
        gbc.gridy++;
        add(msgInfo, gbc);
        gbc.gridy++;
        gbc.insets = new Insets(10, 10, 4, 10);
        add(bodyLabel, gbc);
        gbc.gridy++;
        gbc.insets = new Insets(4, 10, 10, 10);
        gbc.weighty = 1;
        gbc.fill = GridBagConstraints.BOTH;
        add(bodyScroll, gbc);

    }

}


问题是因为正在使用FlowLayout管理器。我已使用其他布局管理器解决了您的问题。
在发布解决方案之前,这里有一些您应该遵循的提示

  • 更改您的类名。应该是骆驼式的
  • 尝试调用pack()而不是setSize(),因为它会自动处理它。当我用pack()替换您的setSize()时,它显示了一个外观笨拙的GUI,这证明您的布局和添加元素是不正确的

        Font size30 = new Font(null, Font.PLAIN, 30);
    
        JPanel pnl = new JPanel();
        pnl.setLayout(new BoxLayout(pnl,BoxLayout.Y_AXIS));
    
        Container contentPane = getContentPane();
    
        //--- User Info ---//
    
        JPanel userInfo = new JPanel();
    
        JLabel userLabel = new JLabel("Username: ");
        JTextField userField = new JTextField(12);
        userInfo.add(userLabel);
        userInfo.add(userField);
    
        JLabel passLabel = new JLabel("Password: ");
        JTextField passField = new JTextField(10);
        userInfo.add(passLabel);
        userInfo.add(passField);
    
        JLabel serverLabel = new JLabel("Mail Server: ");
        JTextField serverField = new JTextField(10);
        userInfo.add(serverLabel);
        userInfo.add(serverField);
    
        JLabel portLabel = new JLabel("Server Port: ");
        JTextField portField = new JTextField(3);
        userInfo.add(portLabel);
        userInfo.add(portField);
    
        //--- To: CC: and Subject Fields ---//
    
        JPanel msgInfo = new JPanel();
    
        JLabel toLabel = new JLabel("To: ");
        JTextField toField = new JTextField(30);
        msgInfo.add(toLabel);
        msgInfo.add(toField);
    
        JLabel subLabel = new JLabel("Subject: ");
        JTextField subField = new JTextField(30);
        msgInfo.add(subLabel);
        msgInfo.add(subField);
    
        //--- Body ---//
    
        JLabel bodyLabel = new JLabel("Body");
        bodyLabel.setFont(size30);
    
        JTextArea bodyField = new JTextArea(30,70);
        bodyField.setLineWrap(true);
        bodyField.setWrapStyleWord(true);
    
        JScrollPane bodyScroll = new JScrollPane(bodyField);
    
        bodyScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        bodyScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    
    
        pnl.add(userInfo);
        pnl.add(msgInfo);
        pnl.add(bodyLabel);
        pnl.add(bodyScroll);
    
        contentPane.add(pnl);
    
        setVisible(true);
        pack();
    

  • 问题是因为正在使用FlowLayout管理器。我已使用其他布局管理器解决了您的问题。
    在发布解决方案之前,这里有一些您应该遵循的提示

  • 更改您的类名。应该是骆驼式的
  • 尝试调用pack()而不是setSize(),因为它会自动处理它。当我用pack()替换您的setSize()时,它显示了一个外观笨拙的GUI,这证明您的布局和添加元素是不正确的

        Font size30 = new Font(null, Font.PLAIN, 30);
    
        JPanel pnl = new JPanel();
        pnl.setLayout(new BoxLayout(pnl,BoxLayout.Y_AXIS));
    
        Container contentPane = getContentPane();
    
        //--- User Info ---//
    
        JPanel userInfo = new JPanel();
    
        JLabel userLabel = new JLabel("Username: ");
        JTextField userField = new JTextField(12);
        userInfo.add(userLabel);
        userInfo.add(userField);
    
        JLabel passLabel = new JLabel("Password: ");
        JTextField passField = new JTextField(10);
        userInfo.add(passLabel);
        userInfo.add(passField);
    
        JLabel serverLabel = new JLabel("Mail Server: ");
        JTextField serverField = new JTextField(10);
        userInfo.add(serverLabel);
        userInfo.add(serverField);
    
        JLabel portLabel = new JLabel("Server Port: ");
        JTextField portField = new JTextField(3);
        userInfo.add(portLabel);
        userInfo.add(portField);
    
        //--- To: CC: and Subject Fields ---//
    
        JPanel msgInfo = new JPanel();
    
        JLabel toLabel = new JLabel("To: ");
        JTextField toField = new JTextField(30);
        msgInfo.add(toLabel);
        msgInfo.add(toField);
    
        JLabel subLabel = new JLabel("Subject: ");
        JTextField subField = new JTextField(30);
        msgInfo.add(subLabel);
        msgInfo.add(subField);
    
        //--- Body ---//
    
        JLabel bodyLabel = new JLabel("Body");
        bodyLabel.setFont(size30);
    
        JTextArea bodyField = new JTextArea(30,70);
        bodyField.setLineWrap(true);
        bodyField.setWrapStyleWord(true);
    
        JScrollPane bodyScroll = new JScrollPane(bodyField);
    
        bodyScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        bodyScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    
    
        pnl.add(userInfo);
        pnl.add(msgInfo);
        pnl.add(bodyLabel);
        pnl.add(bodyScroll);
    
        contentPane.add(pnl);
    
        setVisible(true);
        pack();
    

  • 问题是因为正在使用FlowLayout管理器。我已使用其他布局管理器解决了您的问题。
    在发布解决方案之前,这里有一些您应该遵循的提示

  • 更改您的类名。应该是骆驼式的
  • 尝试调用pack()而不是setSize(),因为它会自动处理它。当我用pack()替换您的setSize()时,它显示了一个外观笨拙的GUI,这证明您的布局和添加元素是不正确的

        Font size30 = new Font(null, Font.PLAIN, 30);
    
        JPanel pnl = new JPanel();
        pnl.setLayout(new BoxLayout(pnl,BoxLayout.Y_AXIS));
    
        Container contentPane = getContentPane();
    
        //--- User Info ---//
    
        JPanel userInfo = new JPanel();
    
        JLabel userLabel = new JLabel("Username: ");
        JTextField userField = new JTextField(12);
        userInfo.add(userLabel);
        userInfo.add(userField);
    
        JLabel passLabel = new JLabel("Password: ");
        JTextField passField = new JTextField(10);
        userInfo.add(passLabel);
        userInfo.add(passField);
    
        JLabel serverLabel = new JLabel("Mail Server: ");
        JTextField serverField = new JTextField(10);
        userInfo.add(serverLabel);
        userInfo.add(serverField);
    
        JLabel portLabel = new JLabel("Server Port: ");
        JTextField portField = new JTextField(3);
        userInfo.add(portLabel);
        userInfo.add(portField);
    
        //--- To: CC: and Subject Fields ---//
    
        JPanel msgInfo = new JPanel();
    
        JLabel toLabel = new JLabel("To: ");
        JTextField toField = new JTextField(30);
        msgInfo.add(toLabel);
        msgInfo.add(toField);
    
        JLabel subLabel = new JLabel("Subject: ");
        JTextField subField = new JTextField(30);
        msgInfo.add(subLabel);
        msgInfo.add(subField);
    
        //--- Body ---//
    
        JLabel bodyLabel = new JLabel("Body");
        bodyLabel.setFont(size30);
    
        JTextArea bodyField = new JTextArea(30,70);
        bodyField.setLineWrap(true);
        bodyField.setWrapStyleWord(true);
    
        JScrollPane bodyScroll = new JScrollPane(bodyField);
    
        bodyScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        bodyScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    
    
        pnl.add(userInfo);
        pnl.add(msgInfo);
        pnl.add(bodyLabel);
        pnl.add(bodyScroll);
    
        contentPane.add(pnl);
    
        setVisible(true);
        pack();
    

  • 问题是因为正在使用FlowLayout管理器。我已使用其他布局管理器解决了您的问题。
    在发布解决方案之前,这里有一些您应该遵循的提示

  • 更改您的类名。应该是骆驼式的
  • 尝试调用pack()而不是setSize(),因为它会自动处理它。当我用pack()替换您的setSize()时,它显示了一个外观笨拙的GUI,这证明您的布局和添加元素是不正确的

        Font size30 = new Font(null, Font.PLAIN, 30);
    
        JPanel pnl = new JPanel();
        pnl.setLayout(new BoxLayout(pnl,BoxLayout.Y_AXIS));
    
        Container contentPane = getContentPane();
    
        //--- User Info ---//
    
        JPanel userInfo = new JPanel();
    
        JLabel userLabel = new JLabel("Username: ");
        JTextField userField = new JTextField(12);
        userInfo.add(userLabel);
        userInfo.add(userField);
    
        JLabel passLabel = new JLabel("Password: ");
        JTextField passField = new JTextField(10);
        userInfo.add(passLabel);
        userInfo.add(passField);
    
        JLabel serverLabel = new JLabel("Mail Server: ");
        JTextField serverField = new JTextField(10);
        userInfo.add(serverLabel);
        userInfo.add(serverField);
    
        JLabel portLabel = new JLabel("Server Port: ");
        JTextField portField = new JTextField(3);
        userInfo.add(portLabel);
        userInfo.add(portField);
    
        //--- To: CC: and Subject Fields ---//
    
        JPanel msgInfo = new JPanel();
    
        JLabel toLabel = new JLabel("To: ");
        JTextField toField = new JTextField(30);
        msgInfo.add(toLabel);
        msgInfo.add(toField);
    
        JLabel subLabel = new JLabel("Subject: ");
        JTextField subField = new JTextField(30);
        msgInfo.add(subLabel);
        msgInfo.add(subField);
    
        //--- Body ---//
    
        JLabel bodyLabel = new JLabel("Body");
        bodyLabel.setFont(size30);
    
        JTextArea bodyField = new JTextArea(30,70);
        bodyField.setLineWrap(true);
        bodyField.setWrapStyleWord(true);
    
        JScrollPane bodyScroll = new JScrollPane(bodyField);
    
        bodyScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        bodyScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    
    
        pnl.add(userInfo);
        pnl.add(msgInfo);
        pnl.add(bodyLabel);
        pnl.add(bodyScroll);
    
        contentPane.add(pnl);
    
        setVisible(true);
        pack();
    

  • <代码> PNL<代码>使用的是<代码> FlowLayout <代码>,它没有“打包”,我会考虑使用一个不同的布局管理器,因为您的文本框是空的,并且滚动条被设置为“按需要”,所以滚动条没有显示出来。尝试使用
    JScrollPane.VERTICAL\u SCROLLBAR\u ALWAYS
    @错误这不是问题所在,问题是滚动条底部被切断。在底部没有箭头,你可以按它使滚动条缓慢下降。正如程序员所说,这可能是由于布局管理器。这也可能是由于将JScrollPane设置为JTextArea的高度。当您将组件传递到JScrollPanes构造函数时,它应该自动补偿大小。不要设置边界(x,y,宽度,高度),试着只设置JScrollPane的位置(x,y)。另外,框架(添加组件后)也不是使用
    setSize(),我会考虑使用一个不同的布局管理器,因为你的文本空间是空的,并且滚动条被设置为“按需要”,所以滚动条没有显示出来。尝试使用
    JScrollPane.VERTICAL\u SCROLLBAR\u始终
    @错误这不是问题,问题是bott