我试图使我的JPanel可滚动,但是它不起作用。我制作了一个JPanel,向其中添加了组件,然后将我的JPanel添加到了JScrollPane中。这是您应该做的,对吧?我究竟做错了什么?

import java.awt.Dimension;
import java.awt.Font;
import java.awt.SystemColor;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ScrollPaneLayout;
import javax.swing.SwingConstants;


public class RegisterPane extends JPanel {
    private JTextField txtJohnDoe;
    private JTextField txtExampledomaincom;
    private JPasswordField passwordField;
    private JPasswordField passwordField_1;

    /**
     * Create the panel.
     */
    public RegisterPane() {
        setLayout(null);

        JPanel panel = new JPanel();
        panel.setBounds(6, 36, 300, 450);
        panel.setLayout(null);

        JLabel lblFirstName = new JLabel("First Name");
        lblFirstName.setBounds(28, 6, 92, 16);
        panel.add(lblFirstName);

        txtJohnDoe = new JTextField();
        txtJohnDoe.setText("John Doe");
        txtJohnDoe.setBounds(124, 0, 251, 28);
        panel.add(txtJohnDoe);
        txtJohnDoe.setColumns(10);

        txtExampledomaincom = new JTextField();
        txtExampledomaincom.setText("[email protected]");
        txtExampledomaincom.setColumns(10);
        txtExampledomaincom.setBounds(124, 40, 251, 28);
        panel.add(txtExampledomaincom);

        JLabel lblEmail = new JLabel("Email");
        lblEmail.setBounds(28, 46, 92, 16);
        panel.add(lblEmail);

        JLabel lblGender = new JLabel("Gender");
        lblGender.setBounds(28, 89, 55, 16);
        panel.add(lblGender);

        JRadioButton rdbtnMale = new JRadioButton("Male");
        rdbtnMale.setBounds(124, 85, 92, 23);
        panel.add(rdbtnMale);

        JRadioButton rdbtnFemale = new JRadioButton("Female");
        rdbtnFemale.setBounds(283, 85, 92, 23);
        panel.add(rdbtnFemale);

        JLabel lblPassword = new JLabel("Password");
        lblPassword.setBounds(28, 157, 55, 16);
        panel.add(lblPassword);

        JLabel passDirections = new JLabel();
        passDirections.setHorizontalAlignment(SwingConstants.CENTER);
        passDirections.setFont(new Font("Lucida Grande", Font.PLAIN, 10));
        passDirections.setBackground(SystemColor.window);
        passDirections.setText("Password's Must be at Least 6 characters long and contain 1  non letter character");
        passDirections.setBounds(29, 117, 346, 28);
        panel.add(passDirections);

        passwordField = new JPasswordField();
        passwordField.setBounds(124, 151, 251, 28);
        panel.add(passwordField);

        passwordField_1 = new JPasswordField();
        passwordField_1.setBounds(124, 195, 251, 28);
        panel.add(passwordField_1);

        JLabel lblRetypePassword = new JLabel("Password Again");
        lblRetypePassword.setBounds(28, 201, 92, 16);
        panel.add(lblRetypePassword);

        JCheckBox chckbxSubscribeToNews = new JCheckBox("Subscribe to News Letter");
        chckbxSubscribeToNews.setSelected(true);
        chckbxSubscribeToNews.setHorizontalAlignment(SwingConstants.CENTER);
        chckbxSubscribeToNews.setBounds(29, 244, 346, 23);
        panel.add(chckbxSubscribeToNews);

        JLabel lblNewLabel = new JLabel("New label");
        lblNewLabel.setBounds(28, 280, 55, 16);
        panel.add(lblNewLabel);

        JScrollPane scroll = new JScrollPane(panel);
        scroll.setSize(new Dimension(450,300));
        panel.setAutoscrolls(true);
        add(scroll);
    }
}


这是我的JFrame

import java.awt.BorderLayout;


public class MainFrame extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MainFrame frame = new MainFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public MainFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout(0, 0));
        setResizable(false);
        setContentPane(new RegisterPane());

            //RegisterPane isn't scrolling ^

    }

}


在此先感谢您的帮助!

最佳答案

我在您的代码中看到很多setLayout(null)

JViewport使用组件/视图的首选大小作为确定视图是否扩展到JScrollPane可见范围之外的基础,因为您已经发现可以忽略此功能,因此组件开始崩溃。

Swing专为工作布局经理而设计,它使开发可在多种平台和环境中工作的复杂用户界面变得更加容易

09-05 01:56