2017-08-26 177 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標籤? –