问题
I'm creating GUI for my university project and I 'm trying to understand how JScrollPane
works.
I have successfully written simple program which shows picture in a scrollable way:
public class ScrollPaneTest{
public static void main(String[] args){
JFrame testFrame = new JFrame("ramka testowa");
testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel picture = new JLabel(new ImageIcon("JavaSwingCompoentsList.PNG"));
JScrollPane scrollPane = new JScrollPane(picture);
testFrame.add(scrollPane, BorderLayout.CENTER);
testFrame.setSize(400, 400);
testFrame.setVisible(true);
}
}
Although, in my final GUI, I would like to apply JScrollPane
only to a part of it, e.g. single JPanel
. To test this idea I have written following code, which unfortunately does not work:
public class ScrollPaneTest{
public static void main(String[] args){
JFrame testFrame = new JFrame("ramka testowa");
testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel picture = new JLabel(new ImageIcon("JavaSwingCompoentsList.PNG"));
JScrollPane scrollPane = new JScrollPane(picture);
JPanel insidePanel = new JPanel();
insidePanel.add(scrollPane);
testFrame.add(insidePanel, BorderLayout.CENTER);
testFrame.setSize(400, 400);
testFrame.setVisible(true);
}
}
I have read numerous tutorials, as well as Stack and CodeRanch articles and I still fail to grasp the idea of how the JScrollPane
work. I suspect, that my mistake has something to do with specifying the dimension of JPanel
-to-scroll, but every single approach I have tried gave me no scrollbars or no picture at all.
If you could show me the right solution to this problem and most importantly, show me where I was wrong I would be very grateful.
回答1:
- Initialize
JPanel
, notJScrollPane
with picture To add the
JPanel
toJScrollPane
, do:scrollPane.setViewportView (panel)
Add the
JScrollPane
, notJPanel
to theJFrame
回答2:
The simplest way is to create your JPanel and specify it when creating the JScrollPane:
JPanel myPanel = ...;
JScrollPane scroller = new JScrollPane( myPanel );
Then just add the scroller to your GUI (instead of adding myPanel ).
回答3:
The problem seems to come from the default FlowLayout
of the inner panel. Change it to another layout (I use BordeLayout
) and it should work. Having said that, I cannot explain why flow layout fails!
import java.awt.BorderLayout;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class ScrollPaneTest {
public static void main(String[] args) {
JFrame testFrame = new JFrame("ramka testowa");
testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel picture = new JLabel(new ImageIcon(
new BufferedImage(370, 1200, BufferedImage.TYPE_INT_RGB)));
JScrollPane scrollPane = new JScrollPane(picture);
JPanel insidePanel = new JPanel(new BorderLayout());
insidePanel.add(scrollPane);
testFrame.add(insidePanel, BorderLayout.CENTER);
insidePanel.add(new JLabel("Stay"), BorderLayout.LINE_START);
insidePanel.add(new JLabel("Stay"), BorderLayout.LINE_END);
insidePanel.add(new JLabel("Stay"), BorderLayout.PAGE_START);
insidePanel.add(new JLabel("Stay"), BorderLayout.PAGE_END);
testFrame.pack();
testFrame.setSize(400, 400);
// failing to do this will end the main & the app.
// doing it will cause the EDT to start.
testFrame.setVisible(true);
}
}
来源:https://stackoverflow.com/questions/41172472/how-to-make-jpanel-scrollable