gpt4 book ai didi

java - 卡片布局在处理过程后不会改变面板

转载 作者:行者123 更新时间:2023-12-02 02:38:23 25 4
gpt4 key购买 nike

所以我刚刚开始制作这个游戏,在制作游戏时遇到的第一个问题是在通过卡片布局更改面板时,当我执行该过程时,它确实显示它正在实例化对象,而且它也实例化了该对象。正在使面板显示,但在视觉上没有任何效果。

代码如下:

原始类

public class Parking_Mania {

public static void main(String []args)throws Exception
{
new GameFrame("Paking Mania");
}
}

游戏框架类

 public class GameFrame extends JFrame{

public GameFrame(String name)
{
this.setTitle(name);
this.setSize(640,510);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setVisible(true);

Frames frame=new Frames();
this.add(frame);
frame.panel.show(frame, "opening");

}
}

改变帧的面板

public class Frames extends JPanel{

CardLayout panel= new CardLayout();

public Frames()
{
this.setLayout(panel);
System.out.println("hello");
Opening op=new Opening();
nxtframe nf= new nxtframe();
this.add(op, "opening");
this.add(nf, "nt");
}

public void nxtf()
{

panel.show(this, "nt");
}
}

第一个面板

public class Opening extends JPanel{



JButton but=new JButton();

public Opening(){

this.setLayout(null);
this.setBackground(Color.BLACK);
add(but);
but.setText("next frame");
but.setBounds(0, 0, 110, 120);
but.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
Frames f=new Frames();
f.nxtf();
}
});
}

public void paint(Graphics g)
{
g.fillRect(110, 120, 110, 120);
}


}

第二个面板

public class nxtframe extends JPanel{

JButton but=new JButton();

public nxtframe(){

System.out.println("hello");
this.setLayout(null);
add(but);
but.setText("next frame");
but.setBounds(0, 0, 110, 120);
but.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{

}
});
}

public void paint(Graphics g)
{
g.setColor(Color.BLUE);
g.fillOval(110, 120, 110, 120);
}


}

PS:我没有费心添加评论,因为我认为这是不言自明的。请放心,如果您确实需要我添加评论,我会立即添加。

最佳答案

批判性地思考:你认为 ActionListener 中的 new Frames(); 会做什么?

答案:它创建一个NEW(强调"new"一词)Frames 对象。新的,就像全新的、独特的、与原来无关的。更改此 Frames 对象上的 View 不会对当前显示的 Frames 对象产生任何影响,因此问题的解决方案是*获取对实际显示对象的引用并调用更改其上的卡片的方法。

一种解决方案是将可视化的 Frames 引用传递到 Opening 中,例如通过构造函数参数,例如更改

Opening op=new Opening();

至:

Opening op = new Opening(this); // pass in the current Frames reference

然后在打开构造函数中获取该引用并使用它:

public class Opening extends JPanel{
private JButton but=new JButton();
private Frames f;

public Opening(final Frames f){
this.f = f;
this.setLayout(null); // !!!! no don't do this!!!
this.setBackground(Color.BLACK);
add(but);
but.setText("next frame");
but.setBounds(0, 0, 110, 120); // and don't do this!
but.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// !!Frames f=new Frames();
f.nxtf();
}
});
}

public void paint(Graphics g) {
g.fillRect(110, 120, 110, 120);
}
}

其他不相关的问题:

  • 不要使用空布局。使用它们会使您的 GUI 僵化、不灵活、难以维护,并且通常无法在其他平台上运行
  • 不要重写paint,而是重写paintComponent,并在重写中调用super的方法——阅读关于此的教程 Lesson: Performing Custom Painting

关于java - 卡片布局在处理过程后不会改变面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45890944/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com