简单java程序改错?
程序编译了没有错,但就是达不到预期的结果。本来点击相应的按钮就可以得到相应的背景颜色,但是现在点击按钮没有任何反映!帮我看看啊importjava.awt.*;impor...
程序编译了没有错,但就是达不到预期的结果。
本来点击相应的按钮就可以得到相应的背景颜色,但是现在点击按钮没有任何反映!帮我看看啊
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ButtonTest
{
public static void main(String[] args)
{
ButtonFrame frame=new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class ButtonFrame extends JFrame
{
public ButtonFrame()
{
setTitle("ButtonTest");
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
ButtonPanel panel=new ButtonPanel();
Container contentPane=getContentPane();
contentPane.add(panel);
}
public static final int DEFAULT_WIDTH=300;
public static final int DEFAULT_HEIGHT=200;
}
class ButtonPanel extends JPanel
{
public ButtonPanel()
{
JButton yellowButton=new JButton("Yellow");
JButton blueButton=new JButton("Blue");
JButton redButton=new JButton("RED");
//增加按钮到面板
add(yellowButton);
add(blueButton);
add(redButton);
//增加按钮动作
ActionListener yellowAction=new ColorAction(Color.YELLOW);
ActionListener blueAction=new ColorAction(Color.BLUE);
ActionListener redAction=new ColorAction(Color.RED);
}
class ColorAction implements ActionListener
{
public ColorAction(Color c)
{
backgroundColor=c;
}
public void actionPerformed(ActionEvent event)
{
setBackground(backgroundColor);
}
private Color backgroundColor;
}
} 展开
本来点击相应的按钮就可以得到相应的背景颜色,但是现在点击按钮没有任何反映!帮我看看啊
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ButtonTest
{
public static void main(String[] args)
{
ButtonFrame frame=new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class ButtonFrame extends JFrame
{
public ButtonFrame()
{
setTitle("ButtonTest");
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
ButtonPanel panel=new ButtonPanel();
Container contentPane=getContentPane();
contentPane.add(panel);
}
public static final int DEFAULT_WIDTH=300;
public static final int DEFAULT_HEIGHT=200;
}
class ButtonPanel extends JPanel
{
public ButtonPanel()
{
JButton yellowButton=new JButton("Yellow");
JButton blueButton=new JButton("Blue");
JButton redButton=new JButton("RED");
//增加按钮到面板
add(yellowButton);
add(blueButton);
add(redButton);
//增加按钮动作
ActionListener yellowAction=new ColorAction(Color.YELLOW);
ActionListener blueAction=new ColorAction(Color.BLUE);
ActionListener redAction=new ColorAction(Color.RED);
}
class ColorAction implements ActionListener
{
public ColorAction(Color c)
{
backgroundColor=c;
}
public void actionPerformed(ActionEvent event)
{
setBackground(backgroundColor);
}
private Color backgroundColor;
}
} 展开
1个回答
展开全部
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ButtonTest
{
public static void main(String[] args)
{
ButtonFrame frame=new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class ButtonFrame extends JFrame
{
public ButtonFrame()
{
setTitle("ButtonTest");
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
ButtonPanel panel=new ButtonPanel();
Container contentPane=getContentPane();
contentPane.add(panel);
}
public static final int DEFAULT_WIDTH=300;
public static final int DEFAULT_HEIGHT=200;
}
class ButtonPanel extends JPanel
{
public ButtonPanel()
{
JButton yellowButton=new JButton("Yellow");
JButton blueButton=new JButton("Blue");
JButton redButton=new JButton("RED");
//增加按钮到面板
add(yellowButton);
add(blueButton);
add(redButton);
//增加按钮动作
ActionListener yellowAction=new ColorAction(Color.YELLOW);
ActionListener blueAction=new ColorAction(Color.BLUE);
ActionListener redAction=new ColorAction(Color.RED);
//增加监听
yellowButton.addActionListener(yellowAction) ;
blueButton.addActionListener(blueAction) ;
redButton.addActionListener(redAction) ;
}
class ColorAction implements ActionListener
{
public ColorAction(Color c)
{
backgroundColor=c;
}
public void actionPerformed(ActionEvent event)
{
setBackground(backgroundColor);
}
private Color backgroundColor;
}
}
你忘记加监听了。这回试试吧
import javax.swing.*;
import java.awt.event.*;
public class ButtonTest
{
public static void main(String[] args)
{
ButtonFrame frame=new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class ButtonFrame extends JFrame
{
public ButtonFrame()
{
setTitle("ButtonTest");
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
ButtonPanel panel=new ButtonPanel();
Container contentPane=getContentPane();
contentPane.add(panel);
}
public static final int DEFAULT_WIDTH=300;
public static final int DEFAULT_HEIGHT=200;
}
class ButtonPanel extends JPanel
{
public ButtonPanel()
{
JButton yellowButton=new JButton("Yellow");
JButton blueButton=new JButton("Blue");
JButton redButton=new JButton("RED");
//增加按钮到面板
add(yellowButton);
add(blueButton);
add(redButton);
//增加按钮动作
ActionListener yellowAction=new ColorAction(Color.YELLOW);
ActionListener blueAction=new ColorAction(Color.BLUE);
ActionListener redAction=new ColorAction(Color.RED);
//增加监听
yellowButton.addActionListener(yellowAction) ;
blueButton.addActionListener(blueAction) ;
redButton.addActionListener(redAction) ;
}
class ColorAction implements ActionListener
{
public ColorAction(Color c)
{
backgroundColor=c;
}
public void actionPerformed(ActionEvent event)
{
setBackground(backgroundColor);
}
private Color backgroundColor;
}
}
你忘记加监听了。这回试试吧
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询