簡體   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