Java 滚动条不是';在JLabel中显示的图像大小巨大。
我使用Jlabel来显示图像,但我现在对滚动条有问题。滚动条不会出现。让我展示一下我的代码:Java 滚动条不是';在JLabel中显示的图像大小巨大。,java,swing,jscrollpane,jlabel,Java,Swing,Jscrollpane,Jlabel,我使用Jlabel来显示图像,但我现在对滚动条有问题。滚动条不会出现。让我展示一下我的代码: public AVFrame(String title, ArrayList<String> imageList) throws HeadlessException { super(title); this.imageList = imageList; AddRootPanel(imageList); //Config frame setDe
public AVFrame(String title, ArrayList<String> imageList) throws HeadlessException {
super(title);
this.imageList = imageList;
AddRootPanel(imageList);
//Config frame
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(FRAME_WIDTH, FRAME_HEIGHT);
addWindowListener(new WindowAdapter() {
/**
* Invoked when a window is in the process of being closed.
* The close operation can be overridden at this point.
*/
@Override
public void windowClosing(WindowEvent e) {
rp.stopListening();
System.exit(0);
}
});
//pack();
//setResizable(false);
setLocationRelativeTo(null); // center the window
setVisible(true);
}
/**
* Method add root panel into frame
*/
private void AddRootPanel(ArrayList<String> imageList) {
rp = new RootPanel(imageList);
Container container = getContentPane();
container.add(rp);
}
**
* Creates a new <code>JPanel</code> with a double buffer
* and a flow layout.
*/
public RootPanel(ArrayList imageList) {
this.imageList = imageList;
listener = new AVListener(this);
controller = new Controller();
controller.addListener(listener);
addImageLabel();
showImage((File) this.imageList.get(0));
}
private void addImageLabel() {
imgLabel = new JLabel();
JScrollPane scrollPane = new JScrollPane(imgLabel);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
add(scrollPane);
}
public void showImage(File imagePath) {
try {
bufferedImage = ImageIO.read(imagePath);
} catch (IOException e) {
e.printStackTrace();
}
ImageIcon icon = new ImageIcon(bufferedImage);
imgLabel.setIcon(icon);
如果有人需要我的代码,请参阅:
另外,我的全球想法是创建应用程序,可以调整大小,旋转图像。如果有人,请提供如何改进我的代码的建议或提示。我会很感激的 直接将
JScrollPane
添加到JFrame的内容窗格中,而不是先将JScrollPane
添加到JPanel
中
这样做
private void AddRootPanel(ArrayList<String> imageList) {
rp = new RootPanel(imageList);
Container container = getContentPane();
container.add(rp.getScrollpane());
}
// Not require to extend JPanel for RootPanel class if it contains only single component
class RootPanel{
private JScrollPane scrollpane;
...
private void addImageLabel() {
imgLabel = new JLabel();
scrollPane= new JScrollPane(imgLabel);
...
}
public JScrollPane getScrollpane(){
return scrollPane;
}
}
private void AddRootPanel(ArrayList imageList){
rp=新根面板(imageList);
Container=getContentPane();
container.add(rp.getScrollpane());
}
//如果根面板类只包含单个组件,则不需要为其扩展JPanel
类根面板{
私有JScrollPane滚动窗格;
...
私有void addImageLabel(){
imgLabel=新的JLabel();
scrollPane=新的JScrollPane(imgLabel);
...
}
公共JScrollPane getScrollpane(){
返回滚动窗格;
}
}
为了更快地获得更好的帮助,请发布一个(最简单的完整且可验证的示例)。我会记住这一点。非常感谢。