我要调整位置(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);
}
答案 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的信息。