import java.awt.*;
import java.awt.event.*;
import javax.swing.*;class FirstPanel extends JPanel
implements ActionListener
{ public FirstPanel()
{ JButton redButton = new JButton("Red");//你在这个地方不应该再定义三个JButton,你把这三个button前的JButton去掉就可以了!你试试!
JButton blueButton = new JButton("Blue");
JButton yellowButton = new JButton("Yellow"); add(redButton);
add(blueButton);
add(yellowButton); redButton.addActionListener(this);
blueButton.addActionListener(this);
yellowButton.addActionListener(this);
} public void actionPerformed(ActionEvent evt)
{ Object source = evt.getSource();
Color color = getBackground();
if (source == redButton) color = Color.red;
else if (source == blueButton) color = Color.blue;
else if (source == yellowButton) color = Color.yellow;
setBackground(color);
repaint();
}
private JButton redButton;
private JButton blueButton;
private JButton yellowButton;
}
class FirstFrame extends JFrame
{ public FirstFrame()
{ setTitle("FirstFrame");
setSize(300, 200);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
});
Container contentPane = getContentPane();
contentPane.add(new FirstPanel());
}
}public class FirstTest
{ public static void main(String[] args)
{ JFrame frame = new FirstFrame();
frame.setVisible(true);
}
}
import java.awt.event.*;
import javax.swing.*;class FirstPanel extends JPanel
implements ActionListener
{ public FirstPanel()
{ JButton redButton = new JButton("Red");//你在这个地方不应该再定义三个JButton,你把这三个button前的JButton去掉就可以了!你试试!
JButton blueButton = new JButton("Blue");
JButton yellowButton = new JButton("Yellow"); add(redButton);
add(blueButton);
add(yellowButton); redButton.addActionListener(this);
blueButton.addActionListener(this);
yellowButton.addActionListener(this);
} public void actionPerformed(ActionEvent evt)
{ Object source = evt.getSource();
Color color = getBackground();
if (source == redButton) color = Color.red;
else if (source == blueButton) color = Color.blue;
else if (source == yellowButton) color = Color.yellow;
setBackground(color);
repaint();
}
private JButton redButton;
private JButton blueButton;
private JButton yellowButton;
}
class FirstFrame extends JFrame
{ public FirstFrame()
{ setTitle("FirstFrame");
setSize(300, 200);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
});
Container contentPane = getContentPane();
contentPane.add(new FirstPanel());
}
}public class FirstTest
{ public static void main(String[] args)
{ JFrame frame = new FirstFrame();
frame.setVisible(true);
}
}
解决方案 »
- 随机获得数组String s[] = {"gequ1","gequ2","gequ3","gequ4"};中的字符串,要不重复的,分别赋值给单选按钮的4个选项
- 关于JRE的问题
- JFrame
- 请教为什么我的Eclipse3.2.1启动不了呢?
- 关于包的问题,我有点蒙
- org.apache.jasper.JasperException: Unable to compile class for JSP
- 高分!有谁用过xindice&java,用过的进来看问题,拿分了!
- 使用JNDI的起码软件配置
- Help::Applet的参数问题
- VisualAge高手请进!!急???
- 怎么可以学好java和相关的语言。
- 怎样在jbuilder中添加日历控件?
private JButton redButton;
private JButton blueButton;
private JButton yellowButton;
然后构造器中再
JButton redButton = new JButton("Red");
JButton blueButton = new JButton("Blue");
JButton yellowButton = new JButton("Yellow");
明白 ? 一开始偶也被迷惑了. . . . .
我不明白为什么要有private JButton redButton;这几句话,作用是什么阿?
为什么有了这几句话前面加上JButton就无法改变颜色呢?请赐教!
被你打败了
很简单,在构造器中你的JButton redButton跟private JButton redButton根本就不是同一个JButton,作用域不同 !
看来你一点基础知识也没有呀. ..good good study,day day up !