简体   繁体   中英

Adding JScrollpane where JPanel is larger then JFrame

In my project if the user resizes the frame or has a lower screen resolution then the JPanel containing all the information will be larger than the JFrame.

By way of example code:

import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class scroll{

    public static void main(String[] args) {
        JFrame frame = new JFrame("Example");
        frame.setSize(500, 500);
        frame.setLayout(null);

        JPanel panel = new JPanel();
        panel.setBackground(Color.BLACK);
        panel.setBounds(5, 5, 450, 600);

        frame.add(panel);

        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}

Can someone kindly explain how to add a JScrollPane for this, have look on here and the wider web but cannot seem to find an answer or tutorial for this.

I hope this will give your answer:

import java.awt.*;

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

public class panel{

    public static void main(String[] args) {
        JFrame frame = new JFrame("Example");
        frame.setSize(500, 500);

        JPanel panel = new JPanel();
        panel.setBackground(Color.BLACK);
        panel.setPreferredSize(new Dimension(450,600));

        JScrollPane scrollPane = new JScrollPane(panel);

        frame.setContentPane(scrollPane);

        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

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