繁体   English   中英

完成此过程后,“卡片布局”不会更改面板

[英]Card Layout doesn't change the panel after working the process

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

代码如下:

原班

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:我不介意添加评论,因为我认为这是不言自明的。 请放心,如果您确实需要我添加评论,我会立即添加。

批判性思考:您认为这样做是什么: new Frames(); 在ActionListener中?

答:它将创建一个NEW (在单词“ new”上带有重音)的Frames对象。 全新的,就像全新的,独特的,与原始的无关。 更改此Frames对象上的视图将不会对当前显示的Frames对象产生影响,因此解决此问题的方法是*获得对实际显示对象的引用,并调用更改其上卡片的方法。

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

Opening op=new Opening();

至:

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

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

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的方法-阅读本课:执行自定义绘画的教程

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM