import java.awt.BorderLayout;import java.awt.Color;import java.awt.Dimension;import java.awt.Graphics;import java.awt.Insets;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JScrollPane;public class Test extends JFrame { public Test() { setLayout(new BorderLayout()); JScrollPane scroll = new JScrollPane(new FootballField()); add(scroll, BorderLayout.CENTER); setSize(500, 500); setDefaultCloseOperation(EXIT_ON_CLOSE); //for anything to have a size you need to make it visible setVisible(true); Insets frameMargin = getInsets(); Insets scrollMargin = scroll.getInsets(); int frameWidth = 490 + frameMargin.left + frameMargin.right + scrollMargin.left + scrollMargin.right + scroll.getVerticalScrollBar().getWidth(); //resize the frame so the entire football field can be seen setSize(frameWidth, 500); } public static void main(String... args) { Test t = new Test(); }}class FootballField extends JPanel { Dimension size = new Dimension(490, 980); public void paintComponent(Graphics g) { g.setColor(Color.BLACK); g.fillRect(0, 0, 490, 980); g.setColor(Color.GREEN); g.fillRect(70, 70, 350, 840); } //override getPreferredSize so any container will know what size this component is public Dimension getPreferredSize() { return size; }}