1

I have created one frame and displaying an image into frame. but I am unable set the size of a frame. As i have tried following code, but its giving me the part of image i have set, Not the whole image. Please help me to re-size the image.

JFrame frame = new JFrame("My Window");
frame.setSize(200,200);
int frameWidth = 200;
int frameHeight = 200;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
frame.setBounds((int) screenSize.getWidth() - frameWidth, 0, frameWidth, frameHeight);
frame.pack();
frame.setVisible(true);
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Anjali
  • 269
  • 2
  • 6
  • 13
  • Have you tried to use the .setSize() method with different parameters? – Ewald May 24 '12 at 06:02
  • I have tried the .setSize(height,width) method only. Is there any other method other than this? – Anjali May 24 '12 at 06:06
  • How about posting the entire application, so we can see how you add the image etc. We might be able to spot something that could help you? – Ewald May 24 '12 at 06:08
  • [This](http://stackoverflow.com/questions/2856480/resizing-a-imageicon-in-a-jbutton) answer might help you. – Rakesh May 24 '12 at 06:09
  • public void Capture() { ImageIcon icon = new ImageIcon("F:\\POSTERS\\Roses\\P100.jpg"); JLabel label = new JLabel(icon); label.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent me) { System.out.println("CLICKED"); } }); – Anjali May 24 '12 at 06:14
  • JFrame frame = new JFrame("My Window"); frame.setSize(200,200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(label); int frameWidth = 150; int frameHeight = 300; Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); frame.setBounds((int) screenSize.getWidth() - frameWidth, 0, frameWidth, frameHeight); frame.pack(); frame.setVisible(true); } capture mathod is called on button click. – Anjali May 24 '12 at 06:15

2 Answers2

3

You are mixing absolute positioning and usage of layout managers. When you call setSize() or setBounds(), you are using absolute layout, ie, you manage manually the size and location of your components.

When you call pack(), you delegate the sizing and positioning to the LayoutManagers of the component inside your frame. LayoutManager's position and size the components based on their preferred/minimum/maximum size and constraints. See all about LayoutManager's here.

I would recommend to rely on LayoutManagers because it works better across different platforms and your code will be cleaner.

Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
3
package test.t100.t004;

import java.awt.Image;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

public class ImageSizedGUI {

    ImageSizedGUI(Image image) {
        JLabel label = new JLabel(new ImageIcon(image));

        JFrame f = new JFrame("Image");
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        // either this, or..
        f.setContentPane(new JScrollPane(label));

        f.pack();
        // ..this, but could produce undesirable results for images 
        // that are larger than screen size!
        // f.setMinimumSize(f.getSize());
        f.setLocationByPlatform(true);
        f.setVisible(true);
    }

    public static void main(String[] args) throws Exception {
        String address = (args.length>0 ? args[0] :
                "http://pscode.org/media/stromlo2.jpg");
        URL url = new URL(address);
        final Image image = ImageIO.read(url);
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                new ImageSizedGUI(image);
            }
        });
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433