0

首先,我很清楚 Layout Managers 的存在,我只是不想在这种情况下使用它。

我正在为我的应用程序编写一个简单的主菜单,但无论我做什么,Image ( JLabel) 总是将自身设置在屏幕的左上角。我已经尝试使用这两种方法(setLocation, setBounds),但没有任何区别。

我确定这是一个愚蠢的错误,但我似乎无法弄清楚。

这是我的代码:

import javax.swing.*;

public class Main extends JFrame{

    protected ImageIcon createImageIcon(String path,
                                        String description) {
        java.net.URL imgURL = getClass().getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL, description);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }

    Main() {
        ImageIcon image1=createImageIcon("/monopoly.jpg","");
        JLabel image1l=new JLabel(image1);
        image1l.setLocation(200,200);
        image1l.setBounds(330, 300, 140, 60);
        add(image1l);
    }

    public static void main(String[] args) {
        Main f=new Main();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
        f.pack();
        f.setTitle("Monopoly");
        f.setSize(800,800);
        f.setLocationRelativeTo(null);
        f.setLayout(null);
    }
}
4

0 回答 0