当前位置: 技术问答>java相关
问题:执行期异常
来源: 互联网 发布时间:2017-04-01
本文导语: import javax.swing.*; import java.awt.*; import java.awt.event.*; class javaButton { public static void main(String[] args) { ButtonTestFrame frame = new ButtonTestFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.show(); } } class ButtonTest...
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class javaButton
{
public static void main(String[] args)
{
ButtonTestFrame frame = new ButtonTestFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class ButtonTestFrame extends JFrame
{
public ButtonTestFrame()
{
setTitle("Button");
setSize(200, 400);
ButtonPanel panel = new ButtonPanel();
Container contentPane = getContentPane();
contentPane.add(panel);
}
}
class ButtonPanel extends JPanel
{
public ButtonPanel()
{
JButton yellowButton = new JButton("Yellow");
add(yellowButton);
ColorAction yellowAction = new ColorAction(Color.yellow);
yellowButton.addActionListener(yellowAction);
}
}
class ColorAction implements ActionListener
{
public ColorAction(Color back)
{
backgroundColor = back;
}
public void actionPerformed(ActionEvent event)
{
bpane.setBackground(backgroundColor);
bpane.repaint();
}
private Color backgroundColor;
private ButtonPanel bpane;
}
这是我的代码,编译通过,执行时出现异常,
import java.awt.*;
import java.awt.event.*;
class javaButton
{
public static void main(String[] args)
{
ButtonTestFrame frame = new ButtonTestFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class ButtonTestFrame extends JFrame
{
public ButtonTestFrame()
{
setTitle("Button");
setSize(200, 400);
ButtonPanel panel = new ButtonPanel();
Container contentPane = getContentPane();
contentPane.add(panel);
}
}
class ButtonPanel extends JPanel
{
public ButtonPanel()
{
JButton yellowButton = new JButton("Yellow");
add(yellowButton);
ColorAction yellowAction = new ColorAction(Color.yellow);
yellowButton.addActionListener(yellowAction);
}
}
class ColorAction implements ActionListener
{
public ColorAction(Color back)
{
backgroundColor = back;
}
public void actionPerformed(ActionEvent event)
{
bpane.setBackground(backgroundColor);
bpane.repaint();
}
private Color backgroundColor;
private ButtonPanel bpane;
}
这是我的代码,编译通过,执行时出现异常,
|
代码修改如下:
自己分析原因
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class javaButton
{
public static void main(String[] args)
{
ButtonTestFrame frame = new ButtonTestFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class ButtonTestFrame extends JFrame
{
public ButtonTestFrame()
{
setTitle("Button");
setSize(200, 400);
ButtonPanel panel = new ButtonPanel();
Container contentPane = getContentPane();
contentPane.add(panel);
}
}
class ButtonPanel extends JPanel
{
public ButtonPanel()
{
JButton yellowButton = new JButton("Yellow");
add(yellowButton);
ColorAction yellowAction = new ColorAction(Color.yellow,this);
yellowButton.addActionListener(yellowAction);
}
}
class ColorAction implements ActionListener
{
public ColorAction(Color back,JPanel backpane)
{
backgroundColor = back;
bpane = (ButtonPanel)backpane;
}
public void actionPerformed(ActionEvent event)
{
bpane.setBackground(backgroundColor);
bpane.repaint();
}
private Color backgroundColor;
private ButtonPanel bpane;
}
自己分析原因
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class javaButton
{
public static void main(String[] args)
{
ButtonTestFrame frame = new ButtonTestFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class ButtonTestFrame extends JFrame
{
public ButtonTestFrame()
{
setTitle("Button");
setSize(200, 400);
ButtonPanel panel = new ButtonPanel();
Container contentPane = getContentPane();
contentPane.add(panel);
}
}
class ButtonPanel extends JPanel
{
public ButtonPanel()
{
JButton yellowButton = new JButton("Yellow");
add(yellowButton);
ColorAction yellowAction = new ColorAction(Color.yellow,this);
yellowButton.addActionListener(yellowAction);
}
}
class ColorAction implements ActionListener
{
public ColorAction(Color back,JPanel backpane)
{
backgroundColor = back;
bpane = (ButtonPanel)backpane;
}
public void actionPerformed(ActionEvent event)
{
bpane.setBackground(backgroundColor);
bpane.repaint();
}
private Color backgroundColor;
private ButtonPanel bpane;
}
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。