解决方案 »

  1.   

    jComboBox1=new JComboBox<String>(fruit);
    这个别指定成string,试试。
      

  2.   

    你用的jdk版本是?
    我记得JComboBox不支持泛型吧;
      

  3.   

    jComboBox1=new JComboBox(fruit);
    这么写也是一样的错啊。。
    JDK版本是1.8
      

  4.   

    Container container = this.getLayeredPane();
    //  没有遇到过这种情况,不过看这句很奇怪,因为很少用到LayeredPane 
            container.setLayout(new BorderLayout());
            container.add(combop, BorderLayout.NORTH);
            container.add(tab, BorderLayout.CENTER);
    于是改成使用ContentPane来添加组件,并且修改利用JPanel来添加JLabel        p1.add(
                    new JLabel("<选择需要转化的Excel文档>", SwingConstants.CENTER),
                    BorderLayout.CENTER);        jwordpath = new JTextField(30);
            jfilepath = new JButton("选择需要读取的Excel文档");        p1.add(new JLabel("选择输出文档的存放路径", SwingConstants.CENTER),
                    BorderLayout.CENTER);好了,没有异常了,可能是由于布局添加太混乱,导致的。swing顶层容器的布局层结构是   JRootPane -》JLayeredPane(其中包含JMenuBar和ContentPane)-》GlassPane(高级渲染用),一般使用JMenuBar和Content Pane(内容面板)就够了,高级的我也说不上来,也是刚学的新手在lz的基础上,修改的代码(99%没改),只是针对错误的地方改了一下而已,建议全重构布局。import java.awt.BorderLayout;
    import java.awt.*;
    import java.awt.GridLayout;
    import java.awt.event.*;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;import javax.swing.BorderFactory;
    import javax.swing.ComboBoxModel;
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTabbedPane;
    import javax.swing.JTextField;
    import javax.swing.SwingConstants;
    import javax.swing.filechooser.FileFilter;
    import javax.swing.filechooser.FileNameExtensionFilter;//import javax.swing.JComboBoxDemo.Model;//import com.hanfeng.ui.UI.Conversion1;public class manager_ui extends JFrame {    private JTextField jtfa;
        private JComboBox jComboBox1;
        private JButton jba;
        private JTextField jwordpath;
        private JTextField jwordOpath;
        private JTextField jword;
        private JButton jfilepath;
        private JButton jwordname;
        private JButton jwordOname;
        private JButton jbc;
        private String[] fruit = { "苹果呵呵呵", "香蕉", "桔子", "梨", "芒果" };
        private JButton jtrans;    public manager_ui() {
            super("自动化工具");
        }    public void initUI() {
            // this.getContentPane().setLayout(new GridBagLayout());
            this.setBounds(450, 450, 750, 600);
            this.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    super.windowClosing(e);
                    System.exit(0);
                }
            });
            layoutUI();
            this.setVisible(true);
        }    private void layoutUI() {        // 对象实例化
            JTabbedPane tab = new JTabbedPane(JTabbedPane.TOP);
            // 容器
            Container container = this.getLayeredPane();
            // 对象化面板
            JPanel combop = new JPanel();
            JPanel p1 = new JPanel();
            JPanel p2 = new JPanel();
            // JPanel p3 = new JPanel();
            // JPanel p4 = new JPanel();        tab.add(p1, "第一");
            tab.add(p2, "第二");        // JComboBox<String> jComboBox1=new JComboBox<String>(fruit);
            jComboBox1 = new JComboBox(fruit);
            // jComboBoxTW1.addActionListener(this);
            jComboBox1.setEditable(true);
            // jComboBox1=JComboBox.setLayout();        // 设置jList1对象的带标题边框        // 添加列表框jComboBox1至面板
            p1.add(jComboBox1);        // 下拉框监听
            jComboBox1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    if (e.getSource() == jComboBox1.getSelectedItem()) {
                        int index = jComboBox1.getSelectedIndex();
                        switch (index) {
                        case 0:
                            // 这里可以做别的事情,这样写只是为了告诉你有这个方法。
                            // box.getSelectedItem() 的返回值是 object
                            System.out.println(jComboBox1.getSelectedItem()
                                    .toString());
                            break;
                        }
                    }
                }
            }
            // JComboBox fruit
            // =(comboboxItem)((combobox)e.getSource()).getSelectedItem();
            // JOptionPane.showMessageDialog(null, fruit.getValue());
                    );
            // 利用ComboBoxModel建立JComboBox
            // ComboBoxModel myModel=new MyModel();
            // JComboBox jComboBox2=new JComboBox();
            // 设置jList1对象的带标题边框
            // jComboBox2.setBorder(BorderFactory.createTitledBorder("您最喜欢的水果:"));
            // 添加列表框jComboBox2至面板
            // p1.add(jComboBox2);        combop.add(new JLabel("工具"));        getContentPane().setLayout(new BorderLayout());
            getContentPane().add(combop, BorderLayout.NORTH);
            getContentPane().add(tab, BorderLayout.CENTER);
    //        container.setLayout(new BorderLayout());
    //        container.add(combop, BorderLayout.NORTH);
    //        container.add(tab, BorderLayout.CENTER);
            final JFileChooser fc;
            {
                fc = new JFileChooser();
                FileFilter filter = new FileNameExtensionFilter("Excel文件(xlsx)",
                        "xlsx");
                fc.setFileFilter(filter);
            }
            final JFileChooser files;
            {
                files = new JFileChooser();
                FileFilter filter = new FileNameExtensionFilter("Word文件(docx)",
                        "docx");
                files.setFileFilter(filter);
            }
            final JFileChooser files1;
            {
                files1 = new JFileChooser();
                files1.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            }
            // Container con = frame.getContentPane();
            // con.setLayout(new BorderLayout());        JPanel jpup = new JPanel();
            jpup.setLayout(new GridLayout(5, 2));
            jtfa = new JTextField(30);
            jba = new JButton("选择需要输入的文档");
            jba.addActionListener(        new ActionListener() {            public void actionPerformed(ActionEvent e) {
                    int r = fc.showOpenDialog(getContentPane());                if (r == JFileChooser.APPROVE_OPTION) {                    jtfa.setText(fc.getSelectedFile().getPath());                    jbc.setEnabled(true);
                    }            }
            }        );
            p1.add(
                    new JLabel("<选择需要转化的Excel文档>", SwingConstants.CENTER),
                    BorderLayout.CENTER);        jwordpath = new JTextField(30);
            jfilepath = new JButton("选择需要读取的Excel文档");        p1.add(new JLabel("选择输出文档的存放路径", SwingConstants.CENTER),
                    BorderLayout.CENTER);        jwordOpath = new JTextField(30);
            jwordOname = new JButton("选择输出文档的存放路径");        jword = new JTextField(30);
            jwordname = new JButton("选择输出文档的存放路径");        jbc = new JButton("开始批处理");
            jbc.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {                try {
                        // Conversion1.Cnvrt1(jtfa.getText(), jwordpath.getText(),
                        // jword.getText(), jwordOpath.getText());                } catch (Exception ee) {
                        ee.printStackTrace();
                        jbc.setEnabled(true);
                        JOptionPane.showMessageDialog(null, "读取文件失败!");
                        System.out.println(ee.getMessage());
                    }
                }
            });
            jtrans = new JButton("             ");
            // jtrans.addActionListener(new ActionListener() {        // @Override
            // public void actionPerformed(ActionEvent e) {
            // System.out.println("hehe \n");
            // }
            // });        p1.add(jtfa);
            p1.add(jba);
            p2.add(jwordpath);
            p2.add(jfilepath);
            p1.add(jwordOpath);
            p1.add(jwordOname);
            p2.add(jword);
            p2.add(jwordname);
            // p1.add(jtrans);
            // p1.add(jbc);    }    public static void main(String[] args) {
            manager_ui ui = new manager_ui();
            ui.initUI();
        }
    }