2017-08-26 147 views
0

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

的代码如下:

原始类

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:我没有刻意添加评论,我想这是自我解释。请放心,如果您确实需要我添加我将立即添加的评论。

+0

请参阅编辑以回答。询问是否有任何问题。 –

+0

完美无缺,非常感谢您的帮助。 –

回答

2

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

答案:它会创建一个新的(强调单词“new”)框架对象。新的,如同全新的,独特的和与原始无关的。更改此Frames对象上的视图对当前显示的Frames对象没有任何影响,因此解决您的问题的方法是*获取对实际显示对象的引用,并调用更改其上的卡片的方法。

一种解决方案是通过可视化的框架参考到开口,说通过一个构造函数参数,则如改变

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

非常感谢我解释了很多东西。快速注意我保持null布局的原因是因为我想要按钮位于特定位置,因为我将使用来自其他游戏的背景图像,因此我必须根据提供给我的图形图像对齐我的按钮如果有不同的方式,请把我也链接到那里? PS:你很多次帮助我,我很欠你!谢谢 –

+0

一个简单的问题,因为我正在制作一个停车场游戏,你会建议我怎么处理这辆车,如果我使用油漆组件,或者你会建议我使用经典的JLabel作为汽车,然后移动j标签? –