如何在JFrame中调整png文件的位置?

时间:2018-10-12 19:11:41

标签: java jframe location png

我要调整位置(PNG文件)。

我该怎么办?我的png文件的位置现在位于面板的中心。

这是我的代码:

public static void main(String[] args) {

    JFrame frame = new JFrame("Hangman");
    frame.setSize(1100,600);
    frame.setVisible(true);
    frame.setResizable(true);
    frame.getContentPane().setBackground(Color.WHITE);

    ImageIcon img = new ImageIcon("hangman.png");
    JLabel lable = new JLabel(img);
    JScrollPane jsp = new JScrollPane(lable);
    frame.getContentPane().add(jsp);

}

1 个答案:

答案 0 :(得分:0)

您需要使用面板的布局。

JFrame frame = new JFrame("Hangman");
frame.setSize(1100,600);
frame.setVisible(true);
frame.setResizable(true);
frame.getContentPane().setBackground(Color.WHITE);

ImageIcon img = new ImageIcon("hangman.png");
JLabel lable = new JLabel(img);
JScrollPane jsp = new JScrollPane(lable);
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(jsp, BorderLayout.WEST);

有很多布局管理器(用于替换BorderLayout),并且以所需的方式获得东西比说HTML或XML困难得多。您可以阅读有关这些布局here的信息。

相关问题