【问题标题】:Card Layout doesn't change the panel after working the process完成该过程后,卡片布局不会更改面板
【发布时间】:2017-08-26 00:44:33
【问题描述】:

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

代码如下:

原班

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:我没有费心添加 cmets,因为我想这是不言自明的。请放心,如果您确实需要我添加 cmets,我会立即添加它们。

【问题讨论】:

  • 请查看编辑以回答。如有任何问题,请询问。
  • 非常好用,非常感谢您的帮助。

标签: java swing jpanel cardlayout


【解决方案1】:

批判性地思考:你认为这有什么作用:new Frames(); 在 ActionListener 中?

答案:它创建了一个 NEW(重音“new”这个词)Frames 对象。新的,就像完全新的一样,与原始的不同且无关。更改此 Frames 对象的视图不会影响当前显示的 Frames 对象,因此解决问题的方法是*获取对实际显示对象的引用并调用更改卡片的方法。

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

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,并且在override中调用super的方法——阅读这个Lesson: Performing Custom Painting的教程

【讨论】:

  • 非常感谢它为我解释了很多事情。快速注意我保持空布局的原因是因为我希望按钮位于特定位置,因为我将使用另一个游戏的背景图像,因此我必须根据提供给我的图形图像对齐按钮如果有不同的方法,你也可以把我链接到那个吗? PS:你帮了我很多次,我欠你的太多了!!!谢谢
  • 一个简单的问题,因为我正在制作一个停车游戏,你建议我用汽车做什么,如果我使用油漆组件会好还是你建议我使用经典的 JLabel 作为车,然后移动 j 标签?
猜你喜欢
  • 2013-01-27
  • 2011-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-24
  • 1970-01-01
  • 2017-07-10
  • 1970-01-01
相关资源
最近更新 更多