如何用java設計一個按button來改變frame顏色?

如何寫用java程式設計一個 用button來改變frame顏色?

import java.awt.*;

import java.awt.event.*;

public class main extends KeyAdapter implements ActionListener, ItemListener

{

public static void main(String[] args)

{

Frame f1 = new Frame("Hw");

f1.setSize(400,400);

f1.setVisible(true);

f1.setBackground(Color.white);

f1.addWindowListener(new WindowEventHandler());

Button b1 = new Button("Blue");

b1.setSize(100,100);

b1.setActionCommand("b1");

b1.addActionListener(this);

Button b2 = new Button("Red");

Button b3 = new Button("Green");

Button b4 = new Button("Yellow");

Button b5 = new Button("Exit");

f1.setLayout(new FlowLayout());

f1.add();

f1.add();

f1.add();

f1.add();

f1.add();

class WindowEventHandler extends WindowAdapter

{

public void windowActivated(WindowEvent e)

{

f1.setTitle("Hw");

}

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

public void windowIconified(WindowEvent e)

{

f1.setTitle("Iconified");

}

}

public void actionPerformed(ActionEvent e)

{

Button b = (Button)e.get.source();

if(cmd.equals("Blue"))

{

f1.setBackground(Color.Blue);

}

}

}

}

這是我寫到目前為止的code錯誤滿多的 但我看不太懂錯哪

麻煩高手指點!!!!!

4 Answers

Rating
  • 9 years ago
    Favorite Answer

    我用你的程式碼改

    有點亂見諒

    import java.awt.*;

    import java.awt.event.*;

    public class main extends KeyAdapter implements ActionListener

    {

    Frame f1 = new Frame("Hw");

    public main()

    {

    f1.setSize(400,400);

    f1.setVisible(true);

    f1.setBackground(Color.white);

    f1.addWindowListener(new WindowEventHandler());

    Button b1 = new Button("Blue");

    Button b2 = new Button("Red");

    Button b3 = new Button("Green");

    Button b4 = new Button("Yellow");

    Button b5 = new Button("Exit");

    b1.setSize(100,100);

    f1.setLayout(new FlowLayout());

    f1.add(b1); b1.addActionListener(this);

    f1.add(b2);b2.addActionListener(this);

    f1.add(b3);b3.addActionListener(this);

    f1.add(b4);b4.addActionListener(this);

    f1.add(b5); b5.addActionListener(this);

    }

    public static void main(String[] args)

    {

    new main();

    }

    public void actionPerformed(ActionEvent e)

    {

    if(e.getActionCommand() == "Blue")

    {

    f1.setBackground(Color.blue);

    }

    if(e.getActionCommand() == "Red")

    {

    f1.setBackground(Color.red);

    }

    if(e.getActionCommand() == "Green")

    {

    f1.setBackground(Color.green);

    }

    if(e.getActionCommand() == "Yellow")

    {

    f1.setBackground(Color.yellow);

    }

    if(e.getActionCommand() == "Exit")

    {

    System.exit(0);

    }

    }

    }

    class WindowEventHandler extends WindowAdapter

    {

    public void windowActivated(WindowEvent e)

    {

    new Frame().setTitle("Hw");

    }

    public void windowClosing(WindowEvent e)

    {

    System.exit(0);

    }

    public void windowIconified(WindowEvent e)

    {

    new Frame().setTitle("Iconified");

    }

    }

  • 5 years ago

    體育 遊戲 -- 世界各國籃球(NBA)、棒球(MLB)、網球、冰球、足球等遊戲

    TS777.CC

  • 5 years ago

    他跟你有一樣的問題

    TS777。CC

  • 5 years ago

    亞州合法36588系統直營現金網~●誠徵經銷商、會員●

    請洽客服 官網 : AA8888.NET

Still have questions? Get your answers by asking now.