在jframe中有多个'imageviews'

时间:2012-08-25 21:50:02

标签: java image swing jframe panel

大家好我想在我的jframe中添加多个组件。但我似乎无法让它发挥作用。

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

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:4)

我建议您让JPanel面板使用GridLayout,而不是null布局,并且不要设置ImageView大小,而是确保类具有getPreferredSize()方法覆盖,这是有意义的,返回尺寸合适的尺寸。然后,如果在添加组件后在JFrame上调用pack(),布局管理器将负责为您调整大小。

答案 1 :(得分: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