import java.awt.*;
import java.awt.event.*;
import java.awt.Checkbox;
public class TestCheckboxFrame extends Frame {
public TestCheckboxFrame() {
Checkbox cb1 = new Checkbox("你喜欢我吗?",true);
final CheckboxGroup cbg = new CheckboxGroup();//就是这个对象。
Checkbox cb2 = new Checkbox("喜欢",true,cbg);
Checkbox cb3 = new Checkbox("不喜欢",false,cbg);
FlowLayout fl = new FlowLayout();
setLayout(fl);
add(cb1);
add(cb2);
add(cb3);
// add(cbg);
class CbItemListener implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
Checkbox cb =(Checkbox)e.getItemSelectable();
if(cb.getLabel().equals("你喜欢我吗?"))
{
if(cb.getState()==true)
{
System.out.println("我很高兴!");
}
else
{
System.out.println("我很伤心。");
}
}
else if(cb.getLabel().equals("喜欢"))
{
if(e.getStateChange()==ItemEvent.SELECTED)
{
System.out.println("我也喜欢你吧。");
}
else
{
System.out.println("我也不喜欢你。");
}
}
else
{
Checkbox cbx = (Checkbox) cbg.getSelectedCheckbox();
if(cbx!=null)
{
System.out.println(cbx.getLabel());
}
}
} } CbItemListener cil = new CbItemListener();
cb1.addItemListener(cil);
cb2.addItemListener(cil);
cb3.addItemListener(cil);
MenuBar menuBar = new MenuBar();
Menu menuFile = new Menu();
MenuItem menuFileExit = new MenuItem();
menuFile.setLabel("File");
menuFileExit.setLabel("Exit");
// Add action listener.for the menu button
menuFileExit.addActionListener
(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
TestCheckboxFrame.this.windowClosed();
}
}
);
menuFile.add(menuFileExit);
menuBar.add(menuFile);
setTitle("TestCheckbox");
setMenuBar(menuBar);
setSize(new Dimension(400, 400));
// Add window listener.
this.addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
TestCheckboxFrame.this.windowClosed();
}
}
);
}
/**
* Shutdown procedure when run as an application.
*/
protected void windowClosed() {
// TODO: Check if it is safe to close the application
// Exit application.
System.exit(0);
}
}
各位大侠,小弟有一个问题讨教,这个程序中对像cbg如果不定义为final,编译时就会报错,
在内部类中的Checkbox cbx = (Checkbox) cbg.getSelectedCheckbox();说无法识别cbg对象,
我觉的应该可以访问吧。不知为什么编译不能通过,请帮忙。?
import java.awt.event.*;
import java.awt.Checkbox;
public class TestCheckboxFrame extends Frame {
public TestCheckboxFrame() {
Checkbox cb1 = new Checkbox("你喜欢我吗?",true);
final CheckboxGroup cbg = new CheckboxGroup();//就是这个对象。
Checkbox cb2 = new Checkbox("喜欢",true,cbg);
Checkbox cb3 = new Checkbox("不喜欢",false,cbg);
FlowLayout fl = new FlowLayout();
setLayout(fl);
add(cb1);
add(cb2);
add(cb3);
// add(cbg);
class CbItemListener implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
Checkbox cb =(Checkbox)e.getItemSelectable();
if(cb.getLabel().equals("你喜欢我吗?"))
{
if(cb.getState()==true)
{
System.out.println("我很高兴!");
}
else
{
System.out.println("我很伤心。");
}
}
else if(cb.getLabel().equals("喜欢"))
{
if(e.getStateChange()==ItemEvent.SELECTED)
{
System.out.println("我也喜欢你吧。");
}
else
{
System.out.println("我也不喜欢你。");
}
}
else
{
Checkbox cbx = (Checkbox) cbg.getSelectedCheckbox();
if(cbx!=null)
{
System.out.println(cbx.getLabel());
}
}
} } CbItemListener cil = new CbItemListener();
cb1.addItemListener(cil);
cb2.addItemListener(cil);
cb3.addItemListener(cil);
MenuBar menuBar = new MenuBar();
Menu menuFile = new Menu();
MenuItem menuFileExit = new MenuItem();
menuFile.setLabel("File");
menuFileExit.setLabel("Exit");
// Add action listener.for the menu button
menuFileExit.addActionListener
(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
TestCheckboxFrame.this.windowClosed();
}
}
);
menuFile.add(menuFileExit);
menuBar.add(menuFile);
setTitle("TestCheckbox");
setMenuBar(menuBar);
setSize(new Dimension(400, 400));
// Add window listener.
this.addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
TestCheckboxFrame.this.windowClosed();
}
}
);
}
/**
* Shutdown procedure when run as an application.
*/
protected void windowClosed() {
// TODO: Check if it is safe to close the application
// Exit application.
System.exit(0);
}
}
各位大侠,小弟有一个问题讨教,这个程序中对像cbg如果不定义为final,编译时就会报错,
在内部类中的Checkbox cbx = (Checkbox) cbg.getSelectedCheckbox();说无法识别cbg对象,
我觉的应该可以访问吧。不知为什么编译不能通过,请帮忙。?
public class inner {
int i=123;
int n=333;
//方法的内部类
public void say(){
int i=1;
class N {
private int j;
N(int i){
j=i;
}
public void print(){
System.out.println(j);
System.out.println(n);
}
}
N n=new N(i);
n.print();
}
public void print(){
final int k=555;
}
class M {
private int j;
M(){
System.out.println(k);//这里不能用k
j=i;
}
public void print(){
System.out.println(j);
}
}
public M getM(){
return new M();
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
inner i=new inner();
i.say();
i.getM().print();
}}
在M的构造函数里面为什么不能用k呢?按照你上面说的k应该看成是外部定义的了啊