当前位置: 技术问答>java相关
很简单的Swing问题,谁能帮我解决啊!!!
来源: 互联网 发布时间:2015-08-22
本文导语: 我在Applet里使用列表框,希望在ItemListener里能够取得List和Label的信息,但在编译时始终说"lstScrollingList.addItemListener(new lstItemListener(this.labListStatus, this.lstScrollingList));"这句话不对,我不知道是什么原因. 程序如下: import jav...
我在Applet里使用列表框,希望在ItemListener里能够取得List和Label的信息,但在编译时始终说"lstScrollingList.addItemListener(new lstItemListener(this.labListStatus, this.lstScrollingList));"这句话不对,我不知道是什么原因.
程序如下:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class ListExample extends Applet
{
public Label labListStatus;
public List lstScrollingList;
public void init()
{
lstScrollingList = new List(3, true);
lstScrollingList.add("Moe");
lstScrollingList.add("Larry");
lstScrollingList.add("Curly");
lstScrollingList.add("Shemp");
lstScrollingList.add("Curly Joe");
lstScrollingList.select(3);
add(lstScrollingList);
labListStatus = new Label("You selected entry Shemp");
add(labListStatus);
lstScrollingList.addItemListener(new lstItemListener(this.labListStatus, this.lstScrollingList));
}
}
class lstItemListener implements ItemListener
{
private Label myLabel;
private List myList;
public void lstItemListener(Label pList, List pLabel)
{
this.myLabel = pListExample.labListStatus;
this.myList = pListExample.lstScrollingList;
}
public void itemStateChanged(ItemEvent even)
{
String selectionString;
String selection;
int selectionNum;
if (even.getStateChange() == ItemEvent.SELECTED)
{
selectionNum = ((Integer)even.getItem()).intValue();
selection = myList.getItem(selectionNum);
selectionString = "You select " + selection;
myLabel.setText(selectionString);
System.out.println(selectionString);
}
else
{
selectionNum = ((Integer)even.getItem()).intValue();
selection = myList.getItem(selectionNum);
selectionString = "You deselect " + selection;
myLabel.setText(selectionString);
System.out.println(selectionString);
}
}
}
程序如下:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class ListExample extends Applet
{
public Label labListStatus;
public List lstScrollingList;
public void init()
{
lstScrollingList = new List(3, true);
lstScrollingList.add("Moe");
lstScrollingList.add("Larry");
lstScrollingList.add("Curly");
lstScrollingList.add("Shemp");
lstScrollingList.add("Curly Joe");
lstScrollingList.select(3);
add(lstScrollingList);
labListStatus = new Label("You selected entry Shemp");
add(labListStatus);
lstScrollingList.addItemListener(new lstItemListener(this.labListStatus, this.lstScrollingList));
}
}
class lstItemListener implements ItemListener
{
private Label myLabel;
private List myList;
public void lstItemListener(Label pList, List pLabel)
{
this.myLabel = pListExample.labListStatus;
this.myList = pListExample.lstScrollingList;
}
public void itemStateChanged(ItemEvent even)
{
String selectionString;
String selection;
int selectionNum;
if (even.getStateChange() == ItemEvent.SELECTED)
{
selectionNum = ((Integer)even.getItem()).intValue();
selection = myList.getItem(selectionNum);
selectionString = "You select " + selection;
myLabel.setText(selectionString);
System.out.println(selectionString);
}
else
{
selectionNum = ((Integer)even.getItem()).intValue();
selection = myList.getItem(selectionNum);
selectionString = "You deselect " + selection;
myLabel.setText(selectionString);
System.out.println(selectionString);
}
}
}
|
帮你改好了:
java文件:ListExample.java
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class ListExample extends Applet
{
public Label labListStatus;
public List lstScrollingList;
public void init()
{
lstScrollingList = new List(3, true);
lstScrollingList.add("Moe");
lstScrollingList.add("Larry");
lstScrollingList.add("Curly");
lstScrollingList.add("Shemp");
lstScrollingList.add("Curly Joe");
lstScrollingList.select(3);
add(lstScrollingList);
labListStatus = new Label("You selected entry Shemp");
add(labListStatus);
lstScrollingList.addItemListener(new lstItemListener(this.labListStatus, this.lstScrollingList));
}
}
class lstItemListener implements ItemListener
{
private Label myLabel;
private List myList;
public lstItemListener(Label pLabel, List pList)
{
this.myLabel = pLabel;
this.myList = pList;
}
public void itemStateChanged(ItemEvent even)
{
String selectionString;
String selection;
int selectionNum;
if (even.getStateChange() == ItemEvent.SELECTED)
{
selectionNum = ((Integer)even.getItem()).intValue();
selection = myList.getItem(selectionNum);
selectionString = "You select " + selection;
myLabel.setText(selectionString);
System.out.println(selectionString);
}
else
{
selectionNum = ((Integer)even.getItem()).intValue();
selection = myList.getItem(selectionNum);
selectionString = "You deselect " + selection;
myLabel.setText(selectionString);
System.out.println(selectionString);
}
}
}
html文件:ListExample.html
New Document
运行命令:
appletviewer ListExample.html
java文件:ListExample.java
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class ListExample extends Applet
{
public Label labListStatus;
public List lstScrollingList;
public void init()
{
lstScrollingList = new List(3, true);
lstScrollingList.add("Moe");
lstScrollingList.add("Larry");
lstScrollingList.add("Curly");
lstScrollingList.add("Shemp");
lstScrollingList.add("Curly Joe");
lstScrollingList.select(3);
add(lstScrollingList);
labListStatus = new Label("You selected entry Shemp");
add(labListStatus);
lstScrollingList.addItemListener(new lstItemListener(this.labListStatus, this.lstScrollingList));
}
}
class lstItemListener implements ItemListener
{
private Label myLabel;
private List myList;
public lstItemListener(Label pLabel, List pList)
{
this.myLabel = pLabel;
this.myList = pList;
}
public void itemStateChanged(ItemEvent even)
{
String selectionString;
String selection;
int selectionNum;
if (even.getStateChange() == ItemEvent.SELECTED)
{
selectionNum = ((Integer)even.getItem()).intValue();
selection = myList.getItem(selectionNum);
selectionString = "You select " + selection;
myLabel.setText(selectionString);
System.out.println(selectionString);
}
else
{
selectionNum = ((Integer)even.getItem()).intValue();
selection = myList.getItem(selectionNum);
selectionString = "You deselect " + selection;
myLabel.setText(selectionString);
System.out.println(selectionString);
}
}
}
html文件:ListExample.html
New Document
运行命令:
appletviewer ListExample.html
|
public void lstItemListener(Label pList, List pLabel)
他是一个构建器吧?怎么会是void型的,我想可能使这里有问题吧?
去掉void试一下!
他是一个构建器吧?怎么会是void型的,我想可能使这里有问题吧?
去掉void试一下!
|
去掉public void
不然就加上
public class lstItemListener implements ItemListener
去掉void
不然就加上
public class lstItemListener implements ItemListener
去掉void