将面板添加到tabbedpane时,JScrollPane无法正常工作

时间:2014-06-13 14:00:44

标签: java swing jpanel jscrollpane jtabbedpane

我已经使用GroupLayout创建了一个JPanel。我已将其作为选项卡添加到选项卡式窗格中,如下所示。

panel= new panel(this);
JScrollPane scrollPane = new JScrollPane();
 scrollPane.setViewportView(panel);
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        scrollPane.setPreferredSize(new Dimension(screenSize.width,screenSize.height));
        tabbedPane.addTab("panel", null, scrollPane, null);
        setLayout(groupLayout);

我可以看到滚动条,它在我的系统中滚动得很好,在屏幕尺寸大于/小于此滚动条的其他系统中,滚动条没有出现。

小组I已创建为

import java.awt.Color;

public class panel extends JPanel {

    private static final long serialVersionUID = 1L;

    private JTextField date = new JTextField();
    private JTextArea reason = new JTextArea();
    private JRadioButton yes = new JRadioButton("Yes");
    private JRadioButton no = new JRadioButton("No");
    private final JLabel by = new JLabel("Followup By");
    private final JComboBox fo = new JComboBox();
    private final JPanel panel_1 = new JPanel();
    private JButton save;
    private final JPanel panel = new JPanel();
    private final JScrollPane scrollPane = new JScrollPane();
    private final JTextArea textArea = new JTextArea();
    /**
     * Create the panel.
     */
    public panel() {
        setBackground(Color.WHITE);

        ButtonGroup ButtonGroup = new ButtonGroup();

        GroupLayout groupLayout = new GroupLayout(this);
        groupLayout.setHorizontalGroup(
            groupLayout.createParallelGroup(Alignment.LEADING)
                .addGroup(groupLayout.createSequentialGroup()
                    .addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 777, GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(61, Short.MAX_VALUE))
        );
        groupLayout.setVerticalGroup(
            groupLayout.createParallelGroup(Alignment.TRAILING)
                .addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(panel_1, GroupLayout.DEFAULT_SIZE, 414, Short.MAX_VALUE)
                    .addGap(142))
        );
        panel_1.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Fo", TitledBorder.LEADING, TitledBorder.TOP, null, SystemColor.textHighlight));
        panel_1.setLayout(null);

         JLabel lblIsAFURequired = new JLabel("");
         JLabel lblProposedDateOfFU = new JLabel("");
         JLabel reason = new JLabel("");

        lblIsAFURequired.setBounds(10, 169, 132, 14);
        panel_1.add(lblIsAFURequired);
        ButtonGroup.add(yes);
        yes.setBounds(155, 165, 54, 23);
        panel_1.add(yes);
        ButtonGroup.add(no);
                no.setBounds(211, 165, 60, 23);
                panel_1.add(no);

                no.setSelected(true);
                lblProposedDateOfFU.setBounds(10, 194, 156, 14);
                panel_1.add(lblProposedDateOfFU);
                date.setBounds(161, 191, 86, 20);
                panel_1.add(date);

                date.setColumns(10);
                date.setEnabled(false);
                date.setEditable(false);
                by.setBounds(10, 219, 86, 14);
                panel_1.add(by);
                fo.setEnabled(false);
                fo.setBounds(161, 216, 67, 20);
                panel_1.add(fo);
                fo.setModel(new DefaultComboBoxModel(new String[] {"Doctor A", "Doctor B"}));
                reason.setBounds(10, 244, 132, 14);
                panel_1.add(reason);
        /*      reason.setEditable(false);
                reason.setEnabled(false)*/;
                reason.setBounds(10, 266, 735, 59);
                panel_1.add(reason);
                reason.setBackground(UIManager.getColor("Button.background"));
                reason.setBorder(new LineBorder(new Color(0, 0, 0)));

                reason.setLineWrap(true);
                reason.setWrapStyleWord(true);

        save = new JButton("Save ");
        save.setBounds(310, 363, 186, 25);
        panel_1.add(save);
        save.setForeground(new Color(0, 0, 128));
        save.setBackground(new Color(245, 245, 220));
        panel.setBounds(10, 25, 748, 133);
        panel_1.add(panel);
        panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Consultation Summary", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(51, 153, 255)));
        GroupLayout gl_panel = new GroupLayout(panel);
        gl_panel.setHorizontalGroup(
            gl_panel.createParallelGroup(Alignment.LEADING)
                .addGap(0, 692, Short.MAX_VALUE)
                .addGroup(gl_panel.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 660, Short.MAX_VALUE)
                    .addContainerGap())
        );
        gl_panel.setVerticalGroup(
            gl_panel.createParallelGroup(Alignment.LEADING)
                .addGap(0, 133, Short.MAX_VALUE)
                .addGroup(gl_panel.createSequentialGroup()
                    .addGap(2)
                    .addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE)
                    .addContainerGap())
        );
        textArea.setWrapStyleWord(true);
        textArea.setLineWrap(true);
        textArea.setBorder(null);
        textArea.setBackground(Color.WHITE);

        scrollPane.setViewportView(textArea);
        panel.setLayout(gl_panel);

                no.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        if(no.isSelected()){
                            date.setEnabled(false);
                            date.setEditable(false);
                            date.setText("");
                            fo.setEditable(false);
                            fo.setEnabled(false);
                            reason.setEnabled(false);
                            reason.setEnabled(false);
                            reason.setBackground(SystemColor.menu);
                        }
                    }
                });

        yes.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(yes.isSelected()){
                    date.setEnabled(true);
                    date.setEditable(true);
                    fo.setEditable(true);
                    fo.setEnabled(true);
                    reason.setEnabled(true);
                    reason.setEnabled(true);
                    reason.setBackground(SystemColor.white);
                                }
            }
        });
        setLayout(groupLayout);

    }

}

任何帮助都会很棒

2 个答案:

答案 0 :(得分:2)

您的布局代码太混乱了。有些代码似乎使用布局管理器而某些代码则没有。 Swing旨在与布局管理器一起使用。

  1. 如果您希望滚动窗格占据整个屏幕,则只需将滚动窗格添加到框架即可。默认情况下,JFrame使用BorderLayout,滚动窗格将放在CENTER中,这意味着滚动窗格将自动调整大小以填充整个框架。

  2. 不要在代码中的任何位置使用setBounds()或setPreferredSize()。当面板的首选大小大于滚动窗格的大小时,滚动窗格的滚动条将自动出现。布局管理器将为您计算首选大小,但如果您使用上述任一方法,它将无法正常工作。

  3. 我还建议您不要使用IDE来布局组件。

答案 1 :(得分:1)

尝试

JScrollPane scrollPane = new JScrollPane(panel);

JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewport(panel);

JScrollPane scrollPane = new JScrollPane();
scrollPane.getViewport().add(panel);

而不是使用

scrollPane.setViewportView(panel);

了解更多How to Use Scroll Panes


有些观点:

  1. 请勿使用null布局,而应使用适当的布局,这就是它们的设计原因。

    了解更多How to Use Various Layout Managers

  2. 覆盖JComponent#getPreferredSize()而非使用setPreferredSize()

    了解更多Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?