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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11919941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:56:36  来源:igfitidea点击:

add scrollpane to jpanel when the panel is full (java)

javaswingjpaneljscrollpane

提问by Adel Bachene

I am working on java swing application and I am adding components dynamically in a JPanel. I want to set a JScrollPaneon this panel and only if the panel is full we can see this scrollpane.

我正在开发 java swing 应用程序,并且正在JPanel. 我想JScrollPane在这个面板上设置一个,只有当面板已满时,我们才能看到这个滚动窗格。

How can I do it on this :

我该怎么做:

 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();
        }
    }
}

回答by Harmeet Singh

Here you go

干得好

    // 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

I have created a JScrollPane, added panelas its component and then added scrollPaneto frame

我创建了一个JScrollPane,添加panel为它的组件,然后添加scrollPaneframe

Here enter image description here

这里 在此处输入图片说明

回答by Nestor

Add panel into JScrollPane, but create ScrollPane by this constractor

将面板添加到 JScrollPane 中,但通过此构造函数创建 ScrollPane

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

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

ScrollBars became visible only when panel size became bigger then parent component size.

仅当面板大小大于父组件大小时,滚动条才可见。

回答by Bharat Sharma

On you jscrollpane you need to set vertical and horizontal bar schemes. for example

在您的 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);
    }
}

hope it will help you. if you are facing any problem then you can ask i will try to solve it.

希望它会帮助你。如果你遇到任何问题,那么你可以问我会尽力解决它。

回答by Smeet Shah

frame = new JFrame();
frame.setBounds(0, 0, 820, 950);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setLayout(null);

final JScrollPane scrollPanel = new JScrollPane(
    panel,
    ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
    ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS
);
scrollPanel.setBounds(0, 0, 800, 900);
panel.setBounds(0, 0, 1920, 1080);
panel.setPreferredSize(new Dimension(1920, 1080));
frame.getContentPane().add(scrollPanel);

This code will work in general to add JScrollPane to JPanel. Adjust bounds of frame, panel and scrollpane according to your requirements but ensure that the bounds of JScrollPane are within the bounds of the frame otherwise the scrollpane will not be visible.

此代码通常用于将 JScrollPane 添加到 JPanel。根据您的要求调整框架、面板和滚动窗格的边界,但确保 JScrollPane 的边界在框架的边界内,否则滚动窗格将不可见。