当前位置: 代码迷 >> J2SE >> 还是上面的有关问题继承出现java.lang.NullPointerException
  详细解决方案

还是上面的有关问题继承出现java.lang.NullPointerException

热度:151   发布时间:2016-04-24 13:14:03.0
还是上面的问题继承出现java.lang.NullPointerException
代码如下
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;
  相关解决方案