2012-03-29 107 views
2

这看起来像是一个新手问题,只是我一直试图围绕Swing框架将自己的头围绕在冗长的时间。Java Swing混合窗格

如果您提供一个图片dog.jpg,至少500 px square,以下代码应该在滚动窗格中显示图像。如果它显示什么,我可能不会把我的手放在绝望中。我错过了什么?

import java.awt.BorderLayout; 
import javax.swing.*; 

public class ScrollSample { 
    public static void main(String args[]) { 
    String title = (args.length == 0 ? "JScrollPane Sample" : args[0]); 
    new ScrollSample(title) ; 
    } 

    public ScrollSample (String title) { 
    JFrame frame = new JFrame(title); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    Icon icon = new ImageIcon("dog.jpg"); 
    JLabel dogLabel = new JLabel(icon); 
    dogLabel.setSize(500, 500) ; 

    JLayeredPane layeredPane = new JLayeredPane() ; 
    layeredPane.add(dogLabel, new Integer(0)) ; 

    JPanel jp = new JPanel() ; 
    jp.add(layeredPane) ; 
    jp.setSize(500, 500) ; 

    JScrollPane scrollPane = new JScrollPane(); 
    scrollPane.setViewportView(jp); 

    frame.getContentPane().add(scrollPane, BorderLayout.CENTER); 
    frame.setSize(300, 200); 
    frame.setVisible(true); 
    } 
} 

谢谢!

+0

像往常一样,我建议使用[windowbuilder亲(https://developers.google.com/java-dev-tools/wbpro/quick_start) – Kai 2012-03-29 14:19:58

+0

找不到任何明显的错误与您的代码。你有没有把dog.jpg放到你的项目根目录中? – 2012-03-29 14:22:18

回答

4

如果您正在绘制较大宽度和大小的组件,则必须设置JLayeredPane的首选大小。特别是因为您将其添加到具有默认布局的JPanel。 JLayeredPane默认情况下没有布局管理器 - 所以要么管理边界,要么将首选布局管理器添加到分层窗格。最简单的方法是:

JLayeredPane layeredPane = new JLayeredPane() ; 

添加

layeredPane.setPreferredSize(new Dimension(500,500)); 

然后将窗口最大化(或设置您的JFrame的大小600X600)的应用程序运行时。

阅读上:How to Use Layered Panes

+0

布局管理器是否需要中间JPanel jp的原因?花了我一段时间来弄清楚。 – 2012-03-29 21:09:42

4
  • 一个JPanel的默认布局是FlowLayout中。 FlowLayout显示 每个组件处于其首选大小并具有5像素边框。改为使用BorderLayout(或直接将分层窗格添加到滚动窗格)。
  • JLayeredPane的默认首选大小是(0,0)。为它设置一个 的首选尺寸。
4

Swing GUI应该在EDT上启动。留给用户练习。

import java.awt.*; 
import javax.swing.*; 
import java.net.URL; 

public class ScrollSample { 

    public static void main(String args[]) throws Exception { 
     final URL url = new URL("http://pscode.org/media/stromlo2.jpg"); 
     String title = (args.length == 0 ? "JScrollPane Sample" : args[0]); 
     new ScrollSample(title, url) ; 
    } 

    public ScrollSample (String title, URL url) { 
     JFrame frame = new JFrame(title); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     Icon icon = new ImageIcon(url); 
     JLabel dogLabel = new JLabel(icon); 
     dogLabel.setBounds(0,0,640,480); 

     JLayeredPane layeredPane = new JLayeredPane() ; 
     layeredPane.add(dogLabel, new Integer(0)) ; 
     layeredPane.setPreferredSize(new Dimension(500, 500)) ; 

     JPanel jp = new JPanel(new BorderLayout()) ; 
     jp.add(layeredPane) ; 

     JScrollPane scrollPane = new JScrollPane(jp); 

     frame.getContentPane().add(scrollPane, BorderLayout.CENTER); 
     frame.setSize(300, 200); 
     frame.setVisible(true); 
    } 
} 
+0

public static void main(String [] args)//为事件派发线程安排作业: //创建并显示此应用程序的GUI。新的ScrollSample(标题,字符串,字符串,字符串,字符串,字符串) url); } }); } – 2012-03-29 21:02:23