面板已满时将滚动窗格添加到jpanel(java)

时间:2012-08-12 05:28:59

标签: java swing jpanel jscrollpane

我正在研究java swing应用程序,我正在JPanel中动态添加组件。我想在此面板上设置JScrollPane,并且只有在面板已满时我们才能看到此滚动窗格。

我怎么能这样做:

 package add_button;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;

public class MyExample 
{
    // Field members
    static JPanel panel = new JPanel();
    static Integer indexer = 1;
    static List<JLabel> listOfLabels = new ArrayList<JLabel>();
    static List<JTextField> listOfTextFields = new ArrayList<JTextField>();

    public static void main(String[] args)
    {       
        // Construct frame
        JFrame frame = new JFrame();
        frame.setLayout(new GridBagLayout());
        frame.setPreferredSize(new Dimension(990, 990));
        frame.setTitle("My Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Frame constraints
        GridBagConstraints frameConstraints = new GridBagConstraints();

        // Construct button
        JButton addButton = new JButton("Add");
        addButton.addActionListener(new ButtonListener());

        // Add button to frame
        frameConstraints.gridx = 0;
        frameConstraints.gridy = 0;
        frame.add(addButton, frameConstraints);

        // Construct panel
        panel.setPreferredSize(new Dimension(600, 600));
        panel.setLayout(new GridBagLayout());
        panel.setBorder(LineBorder.createBlackLineBorder());

        // Add panel to frame
        frameConstraints.gridx = 0;
        frameConstraints.gridy = 1;
        frameConstraints.weighty = 1;
        frame.add(panel, frameConstraints);

        // Pack frame
        frame.pack();

        // Make frame visible
        frame.setVisible(true);
    }

    static class ButtonListener implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent arg0) 
        {       
            // Clear panel
            panel.removeAll();

            // Create label and text field
            JTextField jTextField = new JTextField();
            jTextField.setSize(100, 200);
            listOfTextFields.add(jTextField);
            listOfLabels.add(new JLabel("Label " + indexer));

            // Create constraints
            GridBagConstraints textFieldConstraints = new GridBagConstraints();
            GridBagConstraints labelConstraints = new GridBagConstraints();

            // Add labels and text fields
            for(int i = 0; i < indexer; i++)
            {
                // Text field constraints
                textFieldConstraints.gridx = 1;
                textFieldConstraints.fill = GridBagConstraints.HORIZONTAL;
                textFieldConstraints.weightx = 0.5;
                textFieldConstraints.insets = new Insets(10, 10, 10, 10);
                textFieldConstraints.gridy = i;

                // Label constraints
                labelConstraints.gridx = 0;
                labelConstraints.gridy = i;
                labelConstraints.insets = new Insets(10, 10, 10, 10);

                // Add them to panel
                panel.add(listOfLabels.get(i), labelConstraints);
                panel.add(listOfTextFields.get(i), textFieldConstraints);
            }

            // Align components top-to-bottom
            GridBagConstraints c = new GridBagConstraints();
            c.gridx = 0;
            c.gridy = indexer;
            c.weighty = 1;
            panel.add(new JLabel(), c);

            // Increment indexer
            indexer++;
            panel.updateUI();
        }
    }
}

3 个答案:

答案 0 :(得分:4)

你去吧

    // Construct panel
    //panel.setPreferredSize(new Dimension(600, 600)); // No need for panel as it will get added to scrollpane
    panel.setLayout(new GridBagLayout());
    panel.setBorder(LineBorder.createBlackLineBorder());


    JScrollPane scrollPane = new JScrollPane(panel,   ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    scrollPane.setPreferredSize(new Dimension(600, 600));
    // Add panel to frame
    frameConstraints.gridx = 0;
    frameConstraints.gridy = 1;
    frameConstraints.weighty = 1;
    frame.add(scrollPane, frameConstraints); // add acrollpane to frame

我创建了JScrollPane,添加了panel作为其组件,然后将scrollPane添加到frame

此处enter image description here

答案 1 :(得分:1)

将面板添加到JScrollPane中,但是通过此约束器

创建ScrollPane

JScrollPane scrollPane = new JScrollPane(panel, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);

只有当面板尺寸变得大于父组件尺寸时,ScrollBars才会变得可见。

答案 2 :(得分:1)

在你的jscrollpane上,你需要设置垂直和水平条形图。例如

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;


public class windows_test {
    JFrame login = null;
    JFrame inner_frame = null;

    public windows_test() {
        login = new JFrame();
        login.setBounds(10, 10, 300, 300);
        login.setLayout(new BorderLayout());

        JPanel temp_panel = new JPanel();

        temp_panel.add(new JTextArea("asd fsj   adhf jsad kjfh sa dj kfh j sak ds fda f hsa kj d hf ks ad hf kjs ad h fk js ad h fjs da hf k j sahd kjfsh d jk fhs ad"));

        login.setVisible(true);
        JScrollPane scroll_pane = new JScrollPane(temp_panel);
        scroll_pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); //SETTING SCHEME FOR HORIZONTAL BAR
        scroll_pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        login.add(scroll_pane);
    }
}
希望它会对你有所帮助。如果您遇到任何问题,那么您可以问我会尝试解决它。​​