代码如下
package aa;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import java.awt.Rectangle;
import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Dimension;
public class toupiao extends JFrame{
int a=0,b=0,c=0;
JRadioButton jRadioButton1 = new JRadioButton();
JRadioButton jRadioButton2 = new JRadioButton();
JRadioButton jRadioButton3 = new JRadioButton();
ButtonGroup buttonGroup1 = new ButtonGroup();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
public toupiao() {
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void jbInit() throws Exception {
this.getContentPane().setLayout(null);
setSize(new Dimension(400, 300));
jRadioButton1.setText("jRadioButton1");
jRadioButton1.setBounds(new Rectangle(30, 65, 128, 16));
jButton2.addActionListener(new toupiao_jButton2_actionAdapter(this));
this.getContentPane().add(jRadioButton1);
jRadioButton3.setText("jRadioButton3");
jRadioButton3.setBounds(new Rectangle(39, 166, 95, 14));
this.getContentPane().add(jRadioButton2);
this.getContentPane().add(jRadioButton3);
this.getContentPane().add(jLabel1);
this.getContentPane().add(jLabel2);
this.getContentPane().add(jLabel3);
this.getContentPane().add(jButton1);
this.getContentPane().add(jButton2);
jRadioButton2.setText("jRadioButton2");
jRadioButton2.setBounds(new Rectangle(35, 114, 98, 15));
jLabel1.setBounds(new Rectangle(236, 66, 109, 14));
jLabel2.setBounds(new Rectangle(235, 111, 69, 16));
jLabel3.setBounds(new Rectangle(240, 166, 79, 14));
jButton1.setBounds(new Rectangle(52, 223, 107, 19));
jButton1.setText("jButton1");
jButton1.addActionListener(new toupiao_jButton1_actionAdapter(this));
jButton2.setBounds(new Rectangle(241, 223, 93, 23));
jButton2.setText("jButton2");
buttonGroup1.add(jRadioButton1);
buttonGroup1.add(jRadioButton2);
buttonGroup1.add(jRadioButton3);
}
public static void main(String []args){
toupiao tp=new toupiao();
tp.setVisible(true);
}
public void jButton1_actionPerformed(ActionEvent e) {
// 这里要实现的是点一下按钮对应的jLabel++,是累积的效果
if(jRadioButton1.isSelected()==true){
a++;
}
if(jRadioButton2.isSelected()==true){
b++;
}
if(jRadioButton3.isSelected()==true){
c++;
}
jLabel1.setText(a+"");
jLabel2.setText(b+"");
jLabel3.setText(c+"");
}
public void jButton2_actionPerformed(ActionEvent e) {
System.exit(0);
}
}
class toupiao_jButton2_actionAdapter implements ActionListener {
private toupiao adaptee;