日期:2014-05-20 浏览次数:20681 次
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class thanks extends JApplet {
JButton b1;
int count = 0;
Checkbox setcolor, setcount, red, blue, green;
CheckboxGroup colorgroup;
public void init() {
JPanel panel = (JPanel) getContentPane();
panel.setLayout(new FlowLayout());
b1 = new JButton("press");
panel.add(b1);// add
setcolor = new Checkbox("set color");
setcount = new Checkbox("set count");
panel.add(setcolor);// add
panel.add(setcount);// add
colorgroup=new CheckboxGroup();
red=new Checkbox("red", colorgroup, true);
blue=new Checkbox("blue", colorgroup, false);
green=new Checkbox("green", colorgroup, false);
panel.add(red);
panel.add(blue);
panel.add(green);
if(setcolor.getState()){
red.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
if(e.getStateChange()==ItemEvent.SELECTED)
b1.setForeground(Color.red);
}
});
blue.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
if(e.getStateChange()==ItemEvent.SELECTED)
b1.setForeground(Color.blue);
}
});
green.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
if(e.getStateChange()==ItemEvent.SELECTED)
b1.setForeground(Color.green);
}
});
}
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
doButtonAction();
}
});
}
private void doButtonAction() {
int i = count++;// 从运行开始,准确计数
if (setcount.getState())
b1.setText("press " + i);
else
b1.setText("press me!!!");// 不然会停留在上一次显示
}
}
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JPanel;
public class thanks extends JApplet implements ActionListener {