为什么我的JScrollPane没有工作?

时间:2014-02-17 00:05:13

标签: java swing jframe jpanel jscrollpane

我正在尝试使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("Example@domain.com");
        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 ^

    }

}

先谢谢你的帮助!

1 个答案:

答案 0 :(得分:2)

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

JViewport使用组件/视图的首选大小作为确定视图是否扩展超出JScrollPane可见边界的基础,因为您已经认为适合忽略此功能,组件已经开始崩溃了。

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