我的程序中有个对话框,上面放了一个JList。奇怪的是,只要往里面添加中文项目,整个程序就无法正常退出。如果只是添加英文项目,则一切正常。谁遇到过这样的问题?

解决方案 »

  1.   

    发现了更奇怪的事情。我的源代码是
    listmodel.add(0,textfield.getText());
    如果改成listmodel.add(0,"中文");
    就不会有那样的问题。
      

  2.   

    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,"中");
        }
    }
      

  3.   

    我的好像没有问题,中英文都可以的,也没什么改动,只是把构造函数的参数去了。
    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();
        }
    }
      

  4.   

    去掉JustTest类中的main方法,在其他类中调用new JustTest(),问题仍然会出现。
      

  5.   

    我放在其他类中测试了,还是没有问题啊。可能不是程序的问题了
    代码如下: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();
     }
     }
      

  6.   

    这可真是想破脑袋了。我的比你的还要简洁,主类连构造函数都是空的,main方法只有一个new JustTest(),但是仍然不能正常返回。
      

  7.   

    是不是跟平台有关?我的JDK是1.5的,OS是XP SP2.