boy*_*oyd 2 java swing jlabel jframe layout-manager
我有这个:
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如果它是唯一的组件.

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);
    }
}
| 归档时间: | 
 | 
| 查看次数: | 170 次 | 
| 最近记录: |