2014-03-29 27 views
-1

嗨,我試圖讓我有一個JLabel我洗牌 像我想設置的圖像大小適合對JLabel如何在jlabel上設置圖像大小修正?

這裏Java桌面應用程序whee是我的代碼

public class ImageShuffle extends JPanel { 

    private List<Icon> list = new ArrayList<Icon>(); 
    private JLabel label = new JLabel(); 
    private Timer timer = new Timer(1000, new ActionListener() { 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      update(); 
     } 
    }); 

    public ImageShuffle() { 
     this.setLayout(new GridLayout(1, 0)); 
      list.add(new ImageIcon("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\e.jpg")); 
    list.add(new ImageIcon("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\d.jpg")); 
    list.add(new ImageIcon("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\yellow.png")); 
     list.add(new ImageIcon("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\f.jpg")); 
      list.add(new ImageIcon("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\l.jpg")); 
     label.setIcon(UIManager.getIcon("OptionPane.informationIcon")); 

     timer.start(); 
    } 

    private void update() { 
     Collections.shuffle(list); 
     label.setIcon(list.get(0)); 
    } 

    private void display() { 
     JFrame f = new JFrame("ImageShuffle"); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.add(this); 
     f.add(label); 
     f.pack(); 
     f.setLocationRelativeTo(null); 
     f.setVisible(true); 
    } 

我怎麼能實現提前這個

感謝

+0

你的圖片尺寸不同嗎? –

+0

是圖像大小不同 – user3456343

+1

請看[this](http://stackoverflow.com/a/18011430/2587435)和[this](http://stackoverflow.com/a/14553003/2587435) –

回答

-1
private void display() { 
    JFrame f = new JFrame("ImageShuffle"); 
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    f.add(this); 
    f.add(label); 
    f.pack(); 
    f.setLocationRelativeTo(null); 
    f.setSize(x,y) 
    f.setVisible(true); 

X是你的寬度和你的身高!

如果你寫例如

f.setSize(800,600); 

應該設置在插入它像我一樣的大小!

如果你想設置一個JLabel的大小是這樣的:

panelName.setSize(x,y); 
panelName.setVisible(true); 
+2

x和y會設置幀的大小? – user3456343

相關問題