I have a class that extends JPanel
, where I have overridden the paintComponent(Graphics g)
method. However, I can't see the rectangles or the scroll bars I have drawn.
In the main function I have the following code below:
MyClass mainPanel = new MyClass();
mainPanel.setPreferredSize(new Dimension(1000, 1000));
mainPanel.setLayout(new BorderLayout());
JPanel scrollPanel = new JPanel();
scrollPanel.setSize(new Dimension(2000, 2000));
JScrollPane scrollPane = new JScrollPane(scrollPanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); //Let all scrollPanel has scroll bars
scrollPane.setViewportView(scrollPanel);
scrollPane.setOpaque(true);
scrollPanel.revalidate();
mainPanel.add(scrollPane);
JFrame frame = new JFrame("Scrollable Panel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…