múltiples 'vistas de imagen' en jframe

hi all i am trying to add multiple components to my jframe. but i cant seem to get it to work.

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);



}

my ImageView is a class that extends a JPanel which paints a image.

at this point only nashor is painted

Cualquier ayuda es muy apreciada.

preguntado el 25 de agosto de 12 a las 22:08

"but i cant seem to get it to work." may not tell us enough to allow us to help you well. Please tell us more such as what happens. -

I will say that as a general rule, you should avoid using null layouts that you are almost always better off using some of the layout managers. -

You want to add multiple images to your panel? -

Para obtener una mejor ayuda antes, publique un SSCCE. -

2 Respuestas

I recommend that you have your JPanel panel use a GridLayout, not a null layout, and that you not set your ImageView sizes but rather make sure that the class has a getPreferredSize() method override that makes sense, that returns a Dimension of the appropriate size. Then if you call pack() on your JFrame after adding components, the layout managers will take care of sizing things for you.

Respondido 25 ago 12, 23:08

Considere este programa:

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

Respondido el 16 de diciembre de 12 a las 15:12

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.