首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

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

2012-01-24 
还是上面的问题继承出现java.lang.NullPointerException代码如下package aaimport javax.swing.JFrameim

还是上面的问题继承出现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;


  toupiao_jButton2_actionAdapter(toupiao adaptee) {
  this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e) {
  adaptee.jButton2_actionPerformed(e);
  }
}


class toupiao_jButton1_actionAdapter implements ActionListener {
  private toupiao adaptee;
  toupiao_jButton1_actionAdapter(toupiao adaptee) {
  this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e) {
  adaptee.jButton1_actionPerformed(e);
  }
}
//但是在进入Design就不能再移动里面的东西还报java.lang.NullPointerException,高手帮忙搞一下谢谢!!!!

[解决办法]
class toupiao_jButton2_actionAdapter implements ActionListener { 
private toupiao adaptee; 
toupiao_jButton2_actionAdapter(toupiao adaptee) { 
this.adaptee = adaptee; 

//这里要实例化对象,否则容易出出对象NullPointerException, 还有问题请提清楚点,你这样子的问题,高手一般不会回答,象我这样的新手又 回答得不是很清楚,对象最重要的是要看其指向,
public void actionPerformed(ActionEvent e) { 
adaptee.jButton2_actionPerformed(e); 




class toupiao_jButton1_actionAdapter implements ActionListener { 
private toupiao adaptee; 
toupiao_jButton1_actionAdapter(toupiao adaptee) { 
this.adaptee = adaptee; 


public void actionPerformed(ActionEvent e) { 
adaptee.jButton1_actionPerformed(e); 


热点排行
Bad Request.