2012-08-25 28 views
2

大家好,我想添加多個組件到我的jframe。但我似乎無法得到它的工作。jframe中的多個'imageviews'

private void initGUI() { 
    setAlwaysOnTop(true); 
    setUndecorated(true); 

    AWTUtilities.setWindowOpaque(this, false); 

    AWTUtilities.setWindowOpacity(this, 0.5f); 
    setLocation(ini.getButtonsX(), ini.getButtonsY()); 
    setSize(ini.getButtonsW(), ini.getButtonsH()); 

    setLayout(null); 

    JPanel panel = new JPanel(); 
    panel.setLayout(null); 
    ImageView baron = new ImageView("image/nashor.png", 50, 50); 
    baron.setBounds(50, 50, 50, 50); 
    ImageView test = new ImageView("image/dragon.png", 50, 50); 
    test.setBounds(50, 150, 50, 50); 

    panel.add(baron); 
    panel.add(test); 

    panel.setBounds(0, 0, ini.getButtonsW(), ini.getButtonsH()); 

    add(panel); 



} 

我的ImageView是一個擴展了繪製圖像的JPanel的類。

在這一點上只有nashor被繪

任何幫助是非常感謝。

+0

'「但我似乎無法得到它的工作。」'可能不告訴我們,足以讓我們來幫助你。請告訴我們更多,例如發生了什麼。 –

+2

我會說,作爲一般規則,您應該避免使用空佈局,使用某些佈局管理器幾乎總是會更好。 –

+0

你想添加多個圖像到你的面板? –

回答

4

我建議您讓JPanel面板使用GridLayout而不是null佈局,並且不要設置ImageView大小,而要確保該類有一個getPreferredSize()方法覆蓋,這有意義,它將返回Dimension適當的尺寸。然後,如果在添加組件後在JFrame上調用pack(),佈局管理器將負責爲您調整大小。

4

考慮此程序:

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import java.awt.*; 
import java.awt.event.*; 
import javax.imageio.ImageIO; 
import javax.swing.*; 

public class Init extends JFrame{ 
    JPanel view = new JPanel(); 
    JMenuBar mBar = new JMenuBar(); 
    JMenu menu = new JMenu("File"); 
    JMenuItem mItemOpen = new JMenuItem("Open"); 
    JMenuItem mItemExit = new JMenuItem("Exit"); 
    JFileChooser fc = new JFileChooser(); 
    JTextField txtPath = new JTextField(); 

    BufferedImage myPicture; 
    File filePath; 
    String path; 

    public Init(){ 
     mBar.add(menu); 
     menu.add(mItemOpen); 
     menu.addSeparator(); 
     menu.add(mItemExit); 
     setJMenuBar(mBar); 
     txtPath.setEditable(false); 
     mItemOpen.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent e){ 
       fc.showOpenDialog(null); 
       filePath = fc.getSelectedFile(); 
       path = filePath.getPath(); 
       txtPath.setText(path); 
       try { 
        //view.removeAll(); 
        myPicture = ImageIO.read(new File(path)); 
        JLabel picLabel = new JLabel(new ImageIcon(myPicture)); 
        view.add(picLabel); 
        revalidate(); 

       } catch (IOException ex) { 
        ex.printStackTrace(); 
       } 
      } 
     }); 
     setLayout(new BorderLayout()); 
     getContentPane().add(new JScrollPane(view),BorderLayout.CENTER); 
     getContentPane().add(txtPath,BorderLayout.SOUTH); 
     setTitle(".:My Picture Viewer:."); 
     setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     //pack(); 
     setSize(1024,768); 
     setVisible(true); 
    } 

    public static void main(String [] args){ 
     SwingUtilities.invokeLater(new Runnable(){ 
      public void run(){ 
       new Init(); 
      } 
     });   
    } 
} 

enter image description here