import java.awt.*;
import java.awt.event.*;
class Buttontest extends Frame implements ActionListener
{
Panel p = new Panel();
Button btnred = new Button("红色");
Button btnyellow = new Button("黄色");
Button btnblue = new Button("蓝色");
Color c = Color.white;
public Buttontest(String title)
{
super(title);
setLayout(new FlowLayout());
p.add(btnred);
p.add(btnyellow);
p.add(btnblue);
btnred.addActionListener(this);
btnyellow.addActionListener(this);
btnblue.addActionListener(this);
p.setVisible(true);
add(p);
}
public static void main(String args[])
{
Buttontest b = new Buttontest("三个小按钮!");
b.setSize(300,200);
b.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource().equals(btnred))
{
c = Color.red;
}
if(e.getSource().equals(btnyellow))
{
c = Color.yellow;
}
else if(e.getSource().equals(btnblue))
{
c = Color.blue;
}
repaint();
}
public void paint(Graphics g)
{
setBackground(c);
}
}在panel里面的颜色始终不能变化 请教一下怎么办呢?
import java.awt.event.*;
class Buttontest extends Frame implements ActionListener
{
Panel p = new Panel();
Button btnred = new Button("红色");
Button btnyellow = new Button("黄色");
Button btnblue = new Button("蓝色");
Color c = Color.white;
public Buttontest(String title)
{
super(title);
setLayout(new FlowLayout());
p.add(btnred);
p.add(btnyellow);
p.add(btnblue);
btnred.addActionListener(this);
btnyellow.addActionListener(this);
btnblue.addActionListener(this);
p.setVisible(true);
add(p);
}
public static void main(String args[])
{
Buttontest b = new Buttontest("三个小按钮!");
b.setSize(300,200);
b.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource().equals(btnred))
{
c = Color.red;
}
if(e.getSource().equals(btnyellow))
{
c = Color.yellow;
}
else if(e.getSource().equals(btnblue))
{
c = Color.blue;
}
repaint();
}
public void paint(Graphics g)
{
setBackground(c);
}
}在panel里面的颜色始终不能变化 请教一下怎么办呢?
import java.awt.event.*;
import javax.swing.*;public class ButtonTest
{
public static void main(String[] args)
{
ButtonFrame frame = new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}class ButtonFrame extends JFrame
{
public ButtonFrame()
{
setTitle("ButtonTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); // add panel to frame ButtonPanel panel = new ButtonPanel();
add(panel);
} public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 200;
}class ButtonPanel extends JPanel
{
public ButtonPanel()
{
// create buttons JButton yellowButton = new JButton("BLACK");
JButton blueButton = new JButton("Blue");
JButton redButton = new JButton("Red"); // add buttons to panel add(yellowButton);
add(blueButton);
add(redButton); // create button actions ColorAction yellowAction = new ColorAction(Color.BLACK);
ColorAction blueAction = new ColorAction(Color.BLUE);
ColorAction redAction = new ColorAction(Color.RED); // associate actions with buttons yellowButton.addActionListener(yellowAction);
blueButton.addActionListener(blueAction);
redButton.addActionListener(redAction);
} private class ColorAction implements ActionListener
{
public ColorAction(Color c)
{
backgroundColor = c;
} public void actionPerformed(ActionEvent event)
{
setBackground(backgroundColor);
} private Color backgroundColor;
}
}楼主试试这个代码
package je.test;
//通过按钮操作背景色
import java.awt.*;
import java.awt.event.*;public class ButtonTest extends Frame implements ActionListener
{
Panel p;
Button red,blue,yellow;
Color c = Color.white;
public ButtonTest()
{
super("setBackground");
red = new Button("Red");
red.addActionListener(this); blue = new Button("Blue");
blue.addActionListener(this);
yellow = new Button("Yellow");
yellow.addActionListener(this);
p = new Panel();
p.add(red);
p.add(blue);
p.add(yellow);
add(p);
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0);
}
});
setSize(300,200);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource().equals(red))
{
c = Color.red;
}
else if(e.getSource().equals(blue))
{
c=Color.blue;
}
else if(e.getSource().equals(yellow))
{
c=Color.yellow;
}
this.repaint();
this.setState(1);
this.setState(0);
}
public void paint(Graphics g)
{
System.out.println(c.toString());
this.setBackground(c);
}
public static void main(String[] args)
{
new ButtonTest();
}
}
能说一下代码中最核心的地方 改变在那里么?
{
setBackground(c);
p.setBackground(c);
}
不懂就不要装懂