gpt4 book ai didi

java - JLabel图像图标: stop auto centering in the frame

转载 作者:行者123 更新时间:2023-12-03 02:52:07 25 4
gpt4 key购买 nike

我有这个:

import javax.swing.*;
import java.awt.Canvas;
import java.awt.image.BufferedImage;

public class test extends Canvas{

public static JFrame frame;
public static int WIDTH = 800;
public static int HEIGHT = 600;
public static BufferedImage img;
public static int[] pixels;
public static boolean running=true;

public static void main(String[] a){

img = new BufferedImage(WIDTH, HEIGHT,BufferedImage.TYPE_INT_RGB);
frame = new JFrame("WINDOWw");
frame.add(new JLabel(new ImageIcon(img)));
frame.pack();
frame.setVisible(true);
frame.setSize(WIDTH, HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

如何阻止黑色图像在框架上自动居中?

最佳答案

它似乎是唯一的组件,它填充了框架。如果它是唯一的组件,请使用文本对齐或 FlowLayout

TestLabelPlacement - FlowLayout

import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.image.BufferedImage;

public class TestLabelPlacement {

public static JFrame frame;
public static int WIDTH = 200;
public static int HEIGHT = 150;
public static BufferedImage img;

public static void main(String[] a){

img = new BufferedImage(WIDTH, HEIGHT,BufferedImage.TYPE_INT_RGB);
frame = new JFrame("Window");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setLayout(new FlowLayout(FlowLayout.LEADING));
frame.add(new JLabel(new ImageIcon(img)));
frame.pack();
frame.setVisible(true);
// WRONG! That is the size of the image, not the frame!
//frame.setSize(WIDTH, HEIGHT);
}
}

关于java - JLabel图像图标: stop auto centering in the frame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11260809/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com