Java求解:下面的程序没有错误,为什么运行不了?高人指点!
importjava.awt.BorderLayout;importjava.awt.Color;importjava.awt.Font;importjava.awt.G...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
class JRadioButtonDemo extends JFrame implements ActionListener
{
JRadioButton jf1,jf2,jf3;
JRadioButton jc1,jc2,jc3;
JLabel jL;
JRadioButtonDemo()
{
JLabel jfL=new JLabel("字体大小:");
jf1=new JRadioButton("小号");
jf1=new JRadioButton("中号",true);
jf1=new JRadioButton("大号");
ButtonGroup bg1=new ButtonGroup();
bg1.add(jf1);
bg1.add(jf2);
bg1.add(jf3);
JPanel jp1=new JPanel(new GridLayout(1,4));
jp1.add(jfL);
jp1.add(jf1);
jp1.add(jf2);
jp1.add(jf3);
jf1.addActionListener(this);
jf2.addActionListener(this);
jf3.addActionListener(this);
JLabel jcL=new JLabel("字体颜色:");
jc1=new JRadioButton("红色");
jc2=new JRadioButton("黄色");
jc3=new JRadioButton("蓝色");
ButtonGroup bg2=new ButtonGroup();
bg2.add(jc1);
bg2.add(jc2);
bg2.add(jc3);
JPanel jp2=new JPanel(new GridLayout(1,4));
jp2.add(jcL);
jp2.add(jc1);
jp2.add(jc2);
jp2.add(jc3);
jc1.addActionListener(this);
jc2.addActionListener(this);
jc3.addActionListener(this);
jL=new JLabel("Java Language!");
jL.setFont(new Font("Serif",Font.PLAIN,20));
add(jL);
add(jp1,BorderLayout.NORTH);
add(jp2,BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==jf1)
jL.setFont(new Font("Serif",Font.PLAIN,10));
else if(e.getSource()==jf2)
jL.setFont(new Font("Serif",Font.PLAIN,20));
else if(e.getSource()==jf3)
jL.setFont(new Font("Serif",Font.PLAIN,30));
if(e.getSource()==jc1)
jL.setForeground(Color.RED);
else if(e.getSource()==jc2)
jL.setForeground(Color.GREEN);
else if(e.getSource()==jc3)
jL.setForeground(Color.BLUE);
}
}
public class qinggua
{
public static void main(String[] args)
{
JRadioButtonDemo f=new JRadioButtonDemo();
f.setTitle("JRadioButtonDemo");
f.setSize(280,180);
f.setLocation(400,300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
} 展开
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
class JRadioButtonDemo extends JFrame implements ActionListener
{
JRadioButton jf1,jf2,jf3;
JRadioButton jc1,jc2,jc3;
JLabel jL;
JRadioButtonDemo()
{
JLabel jfL=new JLabel("字体大小:");
jf1=new JRadioButton("小号");
jf1=new JRadioButton("中号",true);
jf1=new JRadioButton("大号");
ButtonGroup bg1=new ButtonGroup();
bg1.add(jf1);
bg1.add(jf2);
bg1.add(jf3);
JPanel jp1=new JPanel(new GridLayout(1,4));
jp1.add(jfL);
jp1.add(jf1);
jp1.add(jf2);
jp1.add(jf3);
jf1.addActionListener(this);
jf2.addActionListener(this);
jf3.addActionListener(this);
JLabel jcL=new JLabel("字体颜色:");
jc1=new JRadioButton("红色");
jc2=new JRadioButton("黄色");
jc3=new JRadioButton("蓝色");
ButtonGroup bg2=new ButtonGroup();
bg2.add(jc1);
bg2.add(jc2);
bg2.add(jc3);
JPanel jp2=new JPanel(new GridLayout(1,4));
jp2.add(jcL);
jp2.add(jc1);
jp2.add(jc2);
jp2.add(jc3);
jc1.addActionListener(this);
jc2.addActionListener(this);
jc3.addActionListener(this);
jL=new JLabel("Java Language!");
jL.setFont(new Font("Serif",Font.PLAIN,20));
add(jL);
add(jp1,BorderLayout.NORTH);
add(jp2,BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==jf1)
jL.setFont(new Font("Serif",Font.PLAIN,10));
else if(e.getSource()==jf2)
jL.setFont(new Font("Serif",Font.PLAIN,20));
else if(e.getSource()==jf3)
jL.setFont(new Font("Serif",Font.PLAIN,30));
if(e.getSource()==jc1)
jL.setForeground(Color.RED);
else if(e.getSource()==jc2)
jL.setForeground(Color.GREEN);
else if(e.getSource()==jc3)
jL.setForeground(Color.BLUE);
}
}
public class qinggua
{
public static void main(String[] args)
{
JRadioButtonDemo f=new JRadioButtonDemo();
f.setTitle("JRadioButtonDemo");
f.setSize(280,180);
f.setLocation(400,300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
} 展开
2个回答
展开全部
Eclipse会报出java.lang.NullPointerException,上面的代码只初始化jf1,并且赋值赋了三次,jf2和jf3均没有初始化,所以代码修改如下:
jf1=new JRadioButton("小号");
jf1=new JRadioButton("中号",true); ===>jf2 = new JRadioButton("中号",true)
jf1=new JRadioButton("大号"); ===> jf3 = new JRadioButton("大号");
程序运行ok
jf1=new JRadioButton("小号");
jf1=new JRadioButton("中号",true); ===>jf2 = new JRadioButton("中号",true)
jf1=new JRadioButton("大号"); ===> jf3 = new JRadioButton("大号");
程序运行ok
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询