关于JList的问题 我的程序中有个对话框,上面放了一个JList。奇怪的是,只要往里面添加中文项目,整个程序就无法正常退出。如果只是添加英文项目,则一切正常。谁遇到过这样的问题? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 发现了更奇怪的事情。我的源代码是listmodel.add(0,textfield.getText());如果改成listmodel.add(0,"中文");就不会有那样的问题。 class CausesDlg extends JDialog implements ActionListener{ private JList CausesList; private DefaultListModel lm; private JScrollPane scroll; private JButton addBtn=new JButton("加入"); private JTextField TextToAdd=new JTextField("",20); private JPanel BottomPane=new JPanel(); private String CausesArray[]={"Cat","Dog","Wolf"}; public CausesDlg(MainForm owner){ super(owner,"事由设置",true); setSize(320,200); setLocation(owner.getLocation().x+20,owner.getLocation().y+20); setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); lm=new DefaultListModel(); CausesList=new JList(lm); for(int i=0;i<CausesArray.length;i++) lm.add(0,CausesArray[i]); CausesList.setFixedCellHeight(18); scroll=new JScrollPane(CausesList, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); addBtn.addActionListener(this); addBtn.setPreferredSize(new Dimension(60,22)); BottomPane.add(TextToAdd); BottomPane.add(addBtn); getContentPane().add(scroll,BorderLayout.CENTER); getContentPane().add(BottomPane,BorderLayout.SOUTH); setVisible(true); } public void actionPerformed(ActionEvent actionEvent){ /*String str=TextToAdd.getText(); if(str.equals("")) return; lm.add(0,str);*/ lm.add(0,"中"); }} 我的好像没有问题,中英文都可以的,也没什么改动,只是把构造函数的参数去了。import java.awt.event.*;import java.awt.*;import javax.swing.*;public class JustTest extends JDialog implements ActionListener{ private JList CausesList; private DefaultListModel lm; private JScrollPane scroll; private JButton addBtn=new JButton("加入"); private JTextField TextToAdd=new JTextField("",20); private JPanel BottomPane=new JPanel(); private String CausesArray[]={"Cat","Dog","Wolf"}; public JustTest(){ super(); setSize(320,200); //setLocation(owner.getLocation().x+20,owner.getLocation().y+20); setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); lm=new DefaultListModel(); CausesList=new JList(lm); for(int i=0;i<CausesArray.length;i++) lm.add(0,CausesArray[i]); CausesList.setFixedCellHeight(18); scroll=new JScrollPane(CausesList, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); addBtn.addActionListener(this);// addBtn.setPreferredSize(new Dimension(60,22)); BottomPane.add(TextToAdd); BottomPane.add(addBtn); getContentPane().add(scroll,BorderLayout.CENTER); getContentPane().add(BottomPane,BorderLayout.SOUTH); setVisible(true); } public void actionPerformed(ActionEvent actionEvent){ /*String str=TextToAdd.getText(); if(str.equals("")) return; lm.add(0,str);*/ lm.add(0,TextToAdd.getText()); } public static void main(String args[]) { new JustTest(); }} 去掉JustTest类中的main方法,在其他类中调用new JustTest(),问题仍然会出现。 我放在其他类中测试了,还是没有问题啊。可能不是程序的问题了代码如下:import java.awt.event.*;import java.awt.*;import javax.swing.*;class JustTest extends JDialog implements ActionListener { private JList CausesList; private DefaultListModel lm; private JScrollPane scroll; private JButton addBtn = new JButton("加入"); private JTextField TextToAdd = new JTextField("", 20); private JPanel BottomPane = new JPanel(); private String CausesArray[] = { "Cat", "Dog", "Wolf" }; public JustTest() { super(); setSize(320, 200); // setLocation(owner.getLocation().x+20,owner.getLocation().y+20); setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); lm = new DefaultListModel(); CausesList = new JList(lm); for (int i = 0; i < CausesArray.length; i++) lm.add(0, CausesArray[i]); CausesList.setFixedCellHeight(18); scroll = new JScrollPane(CausesList, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); addBtn.addActionListener(this); // addBtn.setPreferredSize(new Dimension(60,22)); BottomPane.add(TextToAdd); BottomPane.add(addBtn); getContentPane().add(scroll, BorderLayout.CENTER); getContentPane().add(BottomPane, BorderLayout.SOUTH); setVisible(true); } public void actionPerformed(ActionEvent actionEvent) { /* * String str=TextToAdd.getText(); if(str.equals("")) return; * lm.add(0,str); */ lm.add(0, TextToAdd.getText()); }}public class Test extends JFrame{ JButton button; public Test() { button=new JButton("Click"); button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { new JustTest(); } }); setSize(300,200); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); add(button); setVisible(true); } public static void main(String args[]) { new Test(); } } 这可真是想破脑袋了。我的比你的还要简洁,主类连构造函数都是空的,main方法只有一个new JustTest(),但是仍然不能正常返回。 是不是跟平台有关?我的JDK是1.5的,OS是XP SP2. UDP传输问题 请个熟悉java的人修改一个课程设计(急!)(有酬!) 急!!!求救!encoding不支持怎么办? 牛牛牛!!!!!!!!!!!!!!! 各位朋友哪里有控制语句的视频教学,求要好下的 ,谢谢 关于java调试的问题 简单问题! 求救!(搞定绝对送分) 打印网页中的内容不提示打印那个对话框 麻烦解析一下这两道题的意思,谢谢 如何发送html格式邮件? 如何取得当前虚拟机中已经加载的用户类的列表? 结交tapestry的朋友。
listmodel.add(0,textfield.getText());
如果改成listmodel.add(0,"中文");
就不会有那样的问题。
private JList CausesList;
private DefaultListModel lm;
private JScrollPane scroll;
private JButton addBtn=new JButton("加入");
private JTextField TextToAdd=new JTextField("",20);
private JPanel BottomPane=new JPanel();
private String CausesArray[]={"Cat","Dog","Wolf"};
public CausesDlg(MainForm owner){
super(owner,"事由设置",true);
setSize(320,200);
setLocation(owner.getLocation().x+20,owner.getLocation().y+20);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
lm=new DefaultListModel();
CausesList=new JList(lm);
for(int i=0;i<CausesArray.length;i++) lm.add(0,CausesArray[i]);
CausesList.setFixedCellHeight(18);
scroll=new JScrollPane(CausesList,
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
addBtn.addActionListener(this);
addBtn.setPreferredSize(new Dimension(60,22));
BottomPane.add(TextToAdd);
BottomPane.add(addBtn);
getContentPane().add(scroll,BorderLayout.CENTER);
getContentPane().add(BottomPane,BorderLayout.SOUTH);
setVisible(true);
}
public void actionPerformed(ActionEvent actionEvent){
/*String str=TextToAdd.getText();
if(str.equals("")) return;
lm.add(0,str);*/
lm.add(0,"中");
}
}
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;public class JustTest extends JDialog implements ActionListener
{
private JList CausesList;
private DefaultListModel lm;
private JScrollPane scroll;
private JButton addBtn=new JButton("加入");
private JTextField TextToAdd=new JTextField("",20);
private JPanel BottomPane=new JPanel();
private String CausesArray[]={"Cat","Dog","Wolf"};
public JustTest(){
super();
setSize(320,200);
//setLocation(owner.getLocation().x+20,owner.getLocation().y+20);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
lm=new DefaultListModel();
CausesList=new JList(lm);
for(int i=0;i<CausesArray.length;i++) lm.add(0,CausesArray[i]);
CausesList.setFixedCellHeight(18);
scroll=new JScrollPane(CausesList,
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
addBtn.addActionListener(this);
// addBtn.setPreferredSize(new Dimension(60,22));
BottomPane.add(TextToAdd);
BottomPane.add(addBtn);
getContentPane().add(scroll,BorderLayout.CENTER);
getContentPane().add(BottomPane,BorderLayout.SOUTH);
setVisible(true);
}
public void actionPerformed(ActionEvent actionEvent){
/*String str=TextToAdd.getText();
if(str.equals("")) return;
lm.add(0,str);*/
lm.add(0,TextToAdd.getText());
}
public static void main(String args[])
{
new JustTest();
}
}
代码如下:import java.awt.event.*;
import java.awt.*;
import javax.swing.*;class JustTest extends JDialog implements ActionListener {
private JList CausesList; private DefaultListModel lm; private JScrollPane scroll; private JButton addBtn = new JButton("加入"); private JTextField TextToAdd = new JTextField("", 20); private JPanel BottomPane = new JPanel(); private String CausesArray[] = { "Cat", "Dog", "Wolf" }; public JustTest() {
super();
setSize(320, 200);
// setLocation(owner.getLocation().x+20,owner.getLocation().y+20);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); lm = new DefaultListModel();
CausesList = new JList(lm);
for (int i = 0; i < CausesArray.length; i++)
lm.add(0, CausesArray[i]); CausesList.setFixedCellHeight(18);
scroll = new JScrollPane(CausesList,
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); addBtn.addActionListener(this);
// addBtn.setPreferredSize(new Dimension(60,22));
BottomPane.add(TextToAdd);
BottomPane.add(addBtn); getContentPane().add(scroll, BorderLayout.CENTER);
getContentPane().add(BottomPane, BorderLayout.SOUTH); setVisible(true);
} public void actionPerformed(ActionEvent actionEvent) {
/*
* String str=TextToAdd.getText(); if(str.equals("")) return;
* lm.add(0,str);
*/
lm.add(0, TextToAdd.getText());
}
}
public class Test extends JFrame
{
JButton button;
public Test()
{
button=new JButton("Click");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
new JustTest();
}
});
setSize(300,200);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
add(button);
setVisible(true);
}
public static void main(String args[])
{
new Test();
}
}