JPanel 位置问题
JPanel location issues
我必须创建一个 GUI,我真的很想为其设置背景。
为此,我在使用 paintComponent 方法的地方创建了一个名为 "Backround" 的 class。我给了它我想在后台设置的文件,它起作用了。
这是我的背景 class :
public class Background extends JPanel
{
public void paintComponent(Graphics g)
{
try {
Image img = ImageIO.read(new File("./fond.jpg"));
g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
但是,一旦图像出现在背景中,我框架上的面板就不再位于同一位置,我真的不知道如何解决这个问题,而且仍然没有找到任何相关主题这个。
这是我在描述 GUI 时引用的 class :
this.setContentPane(new Background());
this.setTitle("Arena");
this.setSize(800, 500);
this.setLocationRelativeTo(null);
//this.setLayout(new FlowLayout(FlowLayout.CENTER));
//this.setLayout(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
// ******************************************** PANEL 1 ******************************************
Panel P1 = new Panel();
this.add("NORTH", P1);
P1.setLayout(new FlowLayout());
P1.add(new Label ("Joueur 1"));
P1.add(new Label ("Action"));
P1.add(new Label ("Joueur 2"));
//P1.setVisible(true);
// ********************************************* PANEL 2 ******************************************
Panel P2 = new Panel();
P2.setLayout(new FlowLayout());
P2.add(Liste1);
// Boutons
Button B1 = new Button("FIGHT");
P2.add(B1);
Button B2 = new Button("HEAL");
P2.add(B2);
P2.add(Liste2);
this.add("WEST", P2);
// P2.setVisible(true);
此外,当我删除对 Background 构造函数的调用时,面板会返回到它们的初始位置。
我希望你能帮助我或重定向我!
谢谢!
安托万·斯伯特
- 确保在执行任何客户绘画之前致电
super.paintComponent
- 不要在paint方法中加载资源(如图片),这会影响程序的性能
JPanel
默认使用 FlowLayout
,但在将 Background
面板应用为 contentPane
后,您永远不会更改布局。调用 setContentPane
后,使用 setLayout(new BorderLayout())
或在 类 构造函数中应用布局(同时加载图像)
- 确保在建立基本 UI
之后最后调用 setVisible
我必须创建一个 GUI,我真的很想为其设置背景。 为此,我在使用 paintComponent 方法的地方创建了一个名为 "Backround" 的 class。我给了它我想在后台设置的文件,它起作用了。
这是我的背景 class :
public class Background extends JPanel
{
public void paintComponent(Graphics g)
{
try {
Image img = ImageIO.read(new File("./fond.jpg"));
g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
但是,一旦图像出现在背景中,我框架上的面板就不再位于同一位置,我真的不知道如何解决这个问题,而且仍然没有找到任何相关主题这个。
这是我在描述 GUI 时引用的 class :
this.setContentPane(new Background());
this.setTitle("Arena");
this.setSize(800, 500);
this.setLocationRelativeTo(null);
//this.setLayout(new FlowLayout(FlowLayout.CENTER));
//this.setLayout(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
// ******************************************** PANEL 1 ******************************************
Panel P1 = new Panel();
this.add("NORTH", P1);
P1.setLayout(new FlowLayout());
P1.add(new Label ("Joueur 1"));
P1.add(new Label ("Action"));
P1.add(new Label ("Joueur 2"));
//P1.setVisible(true);
// ********************************************* PANEL 2 ******************************************
Panel P2 = new Panel();
P2.setLayout(new FlowLayout());
P2.add(Liste1);
// Boutons
Button B1 = new Button("FIGHT");
P2.add(B1);
Button B2 = new Button("HEAL");
P2.add(B2);
P2.add(Liste2);
this.add("WEST", P2);
// P2.setVisible(true);
此外,当我删除对 Background 构造函数的调用时,面板会返回到它们的初始位置。
我希望你能帮助我或重定向我!
谢谢!
安托万·斯伯特
- 确保在执行任何客户绘画之前致电
super.paintComponent
- 不要在paint方法中加载资源(如图片),这会影响程序的性能
JPanel
默认使用FlowLayout
,但在将Background
面板应用为contentPane
后,您永远不会更改布局。调用setContentPane
后,使用setLayout(new BorderLayout())
或在 类 构造函数中应用布局(同时加载图像)- 确保在建立基本 UI 之后最后调用
setVisible