简体   繁体   中英

How to put JPanel in JScrollpane?

i want to put JPanel in JScrollpane i wrote this code but it didn't work so i need your help

    centerPanel_scroll = new JScrollPane();
    centerPanel_scroll.setBounds(261, 71, 750, 698);
    center_panel = new JPanel();
    center_panel.setLayout(null);
    center_panel.setBackground(Color.cyan);
    centerPanel_scroll.setViewportView(center_panel);
    main_panel.add(centerPanel_scroll);

Your center_panel is in the JScrollPane. The viewport doesn't respect size/bounds but rather preferred size, so if you must, set the preferred size via setPreferredSize(...) , or even better, have the component extend getPreferredSize() .

I have no idea what you're trying to achieve via setBounds position at 261, 71. That's position within the viewport so it makes no sense whatsoever.

Also you're potentially messing yourself up with your use of the null layout which JScrollPanes can have a great deal of trouble with . Just don't set bounds/sizes or use null layouts and you'll find life a lot easier.

eg,

centerPanel_scroll = new JScrollPane();
// centerPanel_scroll.setBounds(261, 71, 750, 698);
center_panel.setPreferredSize(new Dimension(750, 698));
center_panel = new JPanel();
// center_panel.setLayout(null);
center_panel.setPreferredSize(new Dimension(900, 800));
center_panel.setBackground(Color.cyan);
centerPanel_scroll.setViewportView(center_panel);
main_panel.add(centerPanel_scroll);

eg,

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.*;

public class ScrollEg extends JPanel {
    public ScrollEg() {
        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setViewportView(new InnerPanel());

        setLayout(new BorderLayout());
        add(scrollPane, BorderLayout.CENTER);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(650, 500);
    }

    private static class InnerPanel extends JPanel {
        private static final int PREF_W = 1000;
        private static final Color COLOR_1 = Color.RED;
        private static final Color COLOR_2 = Color.BLUE;

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(PREF_W, PREF_W);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            g2.setPaint(new GradientPaint(0, 0, COLOR_1, 100, 100, COLOR_2, true));
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.fillOval(0, 0, PREF_W, PREF_W);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            ScrollEg mainPanel = new ScrollEg();
            JFrame frame = new JFrame("ScrollEg");
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.add(mainPanel);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        });
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM