一个晚上了,我还是不明白?各位兄弟,帮帮忙吧!看看下面一段程序是关于JList类的!

rwq_ 2001-07-20 09:54:25
下面这段程序我实现在JLIST类中增加字符串,就是不明白为什么老报错?
(用JB5编写)
package acctrans;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.*;
import com.borland.jbcl.layout.*;
import java.io.*;

public class ATMainFrame extends JFrame
{
JPanel contentPane;
XYLayout xYLayout1 = new XYLayout();
//DefaultListModel model=new DefaultListModel();
//Vector vectorList=new Vector(4,2);


//jList1.setListData(vectorList);
//vectorList.addElement("Onew");
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
DefaultListModel data=new DefaultListModel();
JList jList1 = new JList(data);
data.addElement("one!"); //这里为什么会错?

/**Construct the frame*/
public ATMainFrame()
{
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
/**Component initialization*/
private void jbInit() throws Exception
{
//setIconImage(Toolkit.getDefaultToolkit().createImage(ATMainFrame.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(xYLayout1);
this.setSize(new Dimension(640, 480));
this.setTitle("ATMainFrame");
jButton1.setText("Running");
jButton2.setText("Stop");
jButton3.setText("Exit");
contentPane.add(jButton3, new XYConstraints(390, 227, 120, -1));
contentPane.add(jButton1, new XYConstraints(385, 66, 123, -1));
contentPane.add(jButton2, new XYConstraints(389, 145, 120, -1));
contentPane.add(jList1, new XYConstraints(28, 25, 213, 391));
}
/**Overridden so we can exit when window is closed*/
protected void processWindowEvent(WindowEvent e)
{
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING)
{
System.exit(0);
}
}
}

...全文
86 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
rwq_ 2001-07-20
  • 打赏
  • 举报
回复
我现在明白了!
woxin 2001-07-20
  • 打赏
  • 举报
回复
构造方法之外的初始化只能初始化变量
ghw 2001-07-20
  • 打赏
  • 举报
回复
不知道你把data.addElement("one!");放到类的变量定义那里是什么用处,那儿是定义变量的,不是执行动作的;

我以前用过这东西,不过我都是把这样的代码放到事件的处理中或某个方法中,都没有问题。
rwq_ 2001-07-20
  • 打赏
  • 举报
回复
好办法!不过我想问为什么?
woxin 2001-07-20
  • 打赏
  • 举报
回复
将data.addElement("one!");移到jbInit()方法中

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧