2012-03-29 110 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