如何将任何其他布局管理器用于 jscrollpane 而不是使用空布局

How to use any other layout manager for jscrollpane with it instead of using null layout

我使用空布局创建了这个视图:

这是我用来创建上面布局的代码

public class Page1311 extends JPanel {
//  private JTable table;
//  public JScrollPane pane=null;
    public JPanel panel=null;
    public JButton back=null;
    /**
     * Create the panel.
     */
    public Page1311() {
        setLayout(null);
        back = new JButton("back");
        back.setFont(new Font("Arial", Font.PLAIN, 20));
        back.setBounds(10,10, 150, 27);
        add(back);
        List<List<String>> list=new ArrayList<List<String>>();
        for(int j=0;j<=5;j++)
        {
            List<String> list1=new ArrayList<>();
            list1.add("Honda Showroom"+j);
            list1.add("Mandsaur");
            list1.add("25 Chakrawati Colony Railway Station Road");
            list1.add("Activa");
            list1.add("2017");
            list1.add("Honda");
            list.add(list1);
        }
        getLayout(list,this);
        back = new JButton("back");
        back.setFont(new Font("Arial", Font.PLAIN, 20));
        back.setBounds(10,10, 150, 27);
        add(back);
    }
    public static void getLayout(List<List<String>> list,JPanel pane)
    {
        int i=0;
        int x=100;
        int y=100;
        int height=20;
        int width=200;
        int size=list.size();
        JLabel[] lblSName=new JLabel[size];
        JLabel[] lblSAddress=new JLabel[size];
        JLabel[] lblSCity=new JLabel[size];
        JLabel[] lblVName=new JLabel[size];
        JLabel[] lblVVersion=new JLabel[size];
        JLabel[] lblVCompanies=new JLabel[size];
        JButton[] lblGo=new JButton[size];
        Iterator<List<String>> it=list.iterator();
        while(it.hasNext())
        {
            System.out.println(x+" "+y+" "+width+" "+height+" "+i);
            Iterator iit=it.next().iterator(); 
            lblSName[i]= new JLabel();
            lblSName[i].setText("Name:"+iit.next());
            lblSName[i].setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
            lblSName[i].setBounds(x,y,width,height);
            pane.add(lblSName[i]);
            lblSCity[i] = new JLabel();
            lblSCity[i].setText("City:"+iit.next());
            lblSCity[i].setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
            System.out.println((x+240)+" "+y+" "+width+" "+height+" "+i);
            lblSCity[i].setBounds(x+240,y,width,height);
            pane.add(lblSCity[i]);
            lblSAddress[i]= new JLabel();
            lblSAddress[i].setText("Address:"+iit.next());
            lblSAddress[i].setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
            System.out.println((x+470)+" "+y+" "+(width+256)+" "+height+" "+i);
            lblSAddress[i].setBounds(x+470,y, width+256, height);
            pane.add(lblSAddress[i]);
            lblVName[i]= new JLabel();
            lblVName[i].setText("Vehicle Name:"+iit.next());
            lblVName[i].setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
            System.out.println(x+" "+(y+35)+" "+width+" "+height+" "+i);
            lblVName[i].setBounds(x,y+35,width, height);
            pane.add(lblVName[i]);
            lblVVersion[i] = new JLabel();
            lblVVersion[i].setText("Vehicle Version:"+iit.next());
            lblVVersion[i].setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
            System.out.println((x+240)+" "+y+35+" "+width+" "+height+" "+i);
            lblVVersion[i].setBounds(x+240,y+35, width, height);
            pane.add(lblVVersion[i]);
            lblVCompanies[i]= new JLabel();
            lblVCompanies[i].setText("Vehicle Companies:"+iit.next());
            lblVCompanies[i].setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
            System.out.println((x+470)+" "+(y+35)+" "+(width+256)+" "+height+" "+i);
            lblVCompanies[i].setBounds(x+470,y+35, width+256,height);
            pane.add(lblVCompanies[i]);

            lblGo[i]= new JButton("Go ");
            lblGo[i].setFont(new Font("Monotype Corsiva", Font.ITALIC, 15));
            System.out.println(x+" "+(y+70)+" "+(width-130)+" "+height+" "+i);
            lblGo[i].setBounds(x,y+70,width-130, height);
            pane.add(lblGo[i]);
            i++;
            y=y+160;
            System.out.println("new height"+y);
        }
    }
public static void main(String[] args) {
    JFrame frame=new JFrame();
    frame.add(new Page1311());
    frame.setExtendedState(frame.MAXIMIZED_BOTH);
    frame.setLocation(0, 0);
    frame.setVisible(true);
}
}

但现在我发现我不能使用带有空布局的 jscrollpane manager.So 我想使用任何其他布局创建相同的布局 manager.Can 请任何人帮助解决这个问题或者你可以提供给我将 jscrollpane 与 null 布局管理器一起使用的任何其他方式。 提前致谢

JScrollPane 有自己的布局:ScrollPaneLayout。如果你想使用任何其他的,你不要在滚动窗格本身上设置它,你在里面放一个 JPanel 并在那个面板上设置布局。

如果您的布局使用空布局管理器,您需要告诉滚动窗格组件的大小

    JPanel pane = new JPanel(){
        Dimension d = new Dimension(400, 400);

        @Override
        public Dimension getMinimumSize(){
            return d;
        }
        @Override
        public Dimension getPreferredSize(){
            return d;
        }
        @Override
        public Dimension getMaximumSize(){
            return d;
        }

        @Override
        public void paintComponent(Graphics g){
            g.setColor(Color.RED);
            g.fill3DRect(25, 25, 350, 350, true);
        }
    };
    pane.setLayout(null);

    JFrame frame = new JFrame("check");
    frame.setSize(200, 200);
    frame.add(new JScrollPane(pane));
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

为此,我将窗格的大小设置为 400, 400。滚动窗格将遵循这一点。如果您注释掉覆盖的方法,您将看到滚动不再起作用。

OP 要应用此技术,他们只需更改将面板添加到 JFrame 的行。

frame.add(new JScrollPane(new Page1311()));

由于 Page1311 不使用布局管理器,因此他们需要覆盖 getMin/Max/Preferred。正如我在示例中所做的那样。这将用滚动窗格包装自定义 JPanel,并且内容将是可滚动的。

另一种不需要为 JScrollPane 设置任何布局管理器的方法是方法

JScrollPane.setViewportView(Component);

这是我的首选方式。没有即时的大小或布局处理。

如果要将多个组件放置到同一个JScrollPane,只需在其间放置一个JPanel即可。 JPanel 可以有您喜欢的布局。

我会为每个 'show room' 面板使用 GridBagLayout 的组合,然后使用单列 GridLayout 面板将展厅面板集合堆叠在一个容器中。第二个面板将进入滚动窗格。

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

public class ShowRoomLayout {

    private JComponent ui = null;

    ShowRoomLayout() {
        initUI();
    }

    private JPanel getShowRoomPanel(int num) {
        JPanel p = new JPanel(new GridBagLayout());
        p.setBorder(new TitledBorder("GridBagLayout"));

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.anchor = GridBagConstraints.WEST;

        p.add(new JLabel("Name:Honda Showroom" + num), gbc);

        gbc.gridx = 1;
        p.add(new JLabel("City:Mandsaur"), gbc);

        gbc.gridx = 2;
        p.add(new JLabel("Address:25 Chakrawati Colony Railway Station Road"), gbc);

        gbc.gridy = 1;
        gbc.gridx = 0;
        p.add(new JLabel("Vehicle Name:Activa"), gbc);

        gbc.gridx = 1;
        p.add(new JLabel("Vehicle Version:2017"), gbc);

        gbc.gridx = 2;
        p.add(new JLabel("Vehicle Companies:Honda"), gbc);

        gbc.gridy = 2;
        gbc.gridx = 0;
        p.add(new JButton("Go"), gbc);

        return p;
    }

    public void initUI() {
        if (ui != null) {
            return;
        }

        ui = new JPanel(new BorderLayout(4, 4));
        ui.setBorder(new EmptyBorder(4, 4, 4, 4));

        JPanel pList = new JPanel(new GridLayout(0, 1, 3, 3));
        pList.setBorder(new TitledBorder("GridLayout"));

        for (int ii = 1; ii < 21; ii++) {
            pList.add(getShowRoomPanel(ii));
        }

        JScrollPane scrollPane = new JScrollPane(pList,
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

        ui.add(scrollPane);
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                ShowRoomLayout o = new ShowRoomLayout();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                Dimension d = f.getSize();
                f.setSize(new Dimension(d.width, 400));
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}