English 中文(简体)
java: 如何调整框架内装图像的大小
原标题:java: how to resize a frame containg 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);
问题回答

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.

当您调用 pack () 时, 您会将配置和定位委托给您框架中组件的布局管理器 。 布局管理器根据其首选/ minim/ 最大大小和限制, 将组件的位置和大小根据其首选/ minim/ 最大大小和限制来决定 。 见关于布局管理器 < a href= > 的所有信息 http://docs. oracle. com/javase/tumental/ uiswing/ layout/ using. html" rel=“ no follow” >这里

我建议依赖布局管理者 因为它在不同平台上效果更好 你的代码会更干净

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




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签