线程中的异常" AWT-EventQueue-0" java.lang.NoSuchMethodError:AutomobileTableModel.setRow(I)V

时间:2017-11-12 02:06:24

标签: swing jtable listener frame abstracttablemodel

我想说我不是关于java的专家,我只是在开玩笑学习。 所以我们来解释一下。 我想做一张桌子,我可以在那里展示我的车,我的自行车,以及两者。现在我只有一个汽车类,所以我正在使用它。 我遇到了听众的问题,所以让我们粘贴我的代码和错误 首先,我的tablemodel。

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
import java.util.List;

public class AutomobileTableModel extends AbstractTableModel
{



private static final String colonne[] = {"Marca", "Modello", "Stato", "Prezzo", "Tipo", "Colore", "KM", "Anno Imm.", "Scadenza Bollo", "Targa", "Posti", "Portiere", "Bagagliaio", "Cilindrata", "Cilindri", "Cavalli", "Peso/Potenza", "Alimentazione", "Consumo", "Trazione", "Cambio", "Marce", "Lunghezza", "Massa a Vuoto", "Altezza Car.", "Tettuccio"};

    private static final Class<?>[] tipiColonne = {String.class, String.class, String.class, Integer.class, String.class, String.class, Integer.class, String.class, Integer.class, String.class, Integer.class, Integer.class, Integer.class, Integer.class, Integer.class, Integer.class, Double.class, String.class, Integer.class, String.class, Integer.class, Integer.class, Double.class, Integer.class, Double.class, String.class};

    private List<Automobili> automobili;

    public AutomobileTableModel(List<Automobili> automobili)
    {
        this.automobili = automobili;
    }

    @Override
    public int getRowCount()
    {
        return automobili.size();
    }

    @Override
    public int getColumnCount()
    {
        return colonne.length;
    }

    @Override
    public Class<?> getColumnClass(int columnIndex)
    {
         return tipiColonne[columnIndex];
    }

    @Override
    public String getColumnName (int columnIndex)
    {
        return colonne[columnIndex];
    }

    public void setRow(int rowIndex)
    {
        if (automobili.size() > rowIndex)
        {
            automobili.removeRange(automobili.size(), rowIndex);
            fireTableRowsDeleted(automobili.size(), rowIndex);
        }
        else
        {
            automobili.add(new Automobili());
            fireTableRowsInsert(automobili.size(), rowIndex);
        }
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex)
    {
        Automobili automobile = (Automobili)automobili.get(rowIndex);

        switch(columnIndex)
        {
            case 0: return automobile.getMarca();
            case 1: return automobile.getModello();
            case 2: return automobile.getStato();
            case 3: return automobile.getPrezzo();
            case 4: return automobile.getTipo();
            case 5: return automobile.getColore();
            case 6: return automobile.getKm();
            case 7: return automobile.getAnnoImmatricolazione();
            case 8: return automobile.getScadenzaBollo();
            case 9: return automobile.getTarga();
            case 10: return automobile.getPosti();
            case 11: return automobile.getPortiere();
            case 12: return automobile.getBagagliaio();
            case 13: return automobile.getCilindrata();
            case 14: return automobile.getCilindri();
            case 15: return automobile.getCavalli();
            case 16: return automobile.getRapportoPP();
            case 17: return automobile.getMotore();
            case 18: return automobile.getConsumo();
            case 19: return automobile.getTrazione();
            case 20: return automobile.getCambio();
            case 21: return automobile.getMarce();
            case 22: return automobile.getLunghezza();
            case 23: return automobile.getMassaVuoto();
            case 24: return automobile.getAltezzaTerra();
            case 25: return automobile.getTettuccio();
            default: return null;
        }
    }
}

我的第二帧

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;

public class FrameTabella extends JPanel 
{
    private AutomobileTableModel atb;
    private JTable tb;
    private JScrollPane p;

    public FrameTabella()
    {
        super();

        ArrayList<Automobili> automobile = new ArrayList<Automobili>();
        automobile.add(new Automobili());
        JPanel header = new JPanel();

        atb = new AutomobileTableModel(automobile);
        tb = new JTable(atb);
        p = new JScrollPane(tb);

        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

        JLabel jl = new JLabel("Inserisci il numero di veicoli da visualizzare");

        JLabel jl1 = new JLabel("Scegli cosa visualizzare");


        JRadioButton auto = new JRadioButton("Auto");
        JRadioButton moto = new JRadioButton("Moto");
        JRadioButton both = new JRadioButton("Entrambi");
        auto.setSelected(true);

        class RadioButtonActionListener implements ActionListener 
        {
            @Override
            public void actionPerformed(ActionEvent e) 
            {
                JRadioButton button = (JRadioButton)e.getSource();
                if (button == auto) JOptionPane.showMessageDialog(null, "auto");
                else if (button == moto) JOptionPane.showMessageDialog(null, "moto");
                else if (button == both) JOptionPane.showMessageDialog(null, "entrambi");
            }
        }

        RadioButtonActionListener al = new RadioButtonActionListener();
        auto.addActionListener(al);
        moto.addActionListener(al);
        both.addActionListener(al);

        ButtonGroup group = new ButtonGroup();
        group.add(auto);
        group.add(moto);
        group.add(both);


        JCheckBox cb = new JCheckBox("Mostra in kw");
        cb.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                JCheckBox c = (JCheckBox)e.getSource();
                if(c.isSelected())
                JOptionPane.showMessageDialog(null, "selezionato");
                else
                JOptionPane.showMessageDialog(null, "deselezionato");
            }
        }); 



        SpinnerModel jsm = new SpinnerNumberModel(1, 0, 1000, 1);
        final JSpinner js = new JSpinner(jsm);
        JButton b = new JButton("Visualizza");
        b.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                Integer i = (Integer)js.getValue();
                atb.setRow(i);
            }
        });


        //setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);

        header.add(jl);
        header.add(js);
        header.add(b);
        header.add(jl1);
        header.add(auto);
        header.add(moto);
        header.add(both);
        header.add(cb);

        add(header);
        add(p);
        setVisible(true);
    }
}

比我得到的错误:

Exception in thread "AWT-EventQueue-0" java.lang.NoSuchMethodError: AutomobileTableModel.setRow(I)V
        at FrameTabella$2.actionPerformed(FrameTabella.java:96)
        at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
        at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
        at java.awt.Component.processMouseEvent(Unknown Source)
        at javax.swing.JComponent.processMouseEvent(Unknown Source)
        at java.awt.Component.processEvent(Unknown Source)
        at java.awt.Container.processEvent(Unknown Source)
        at java.awt.Component.dispatchEventImpl(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Window.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
        at java.awt.EventQueue.access$500(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)

所以我只想做这件事。 当我更改微调器上的值时,我想在我的桌子上添加或删除行,当我按下JButton时,我将通过我的列表上的编辑来执行此操作。 为什么我得到这个错误?为什么当我编辑Frame Tabella时,我在AutomobileTableModel上编译时得到以下2个错误? 错误:

.\AutomobileTableModel.java:48: error: cannot find symbol
                        automobili.removeRange(automobili.size(), rowIndex);
                                  ^
  symbol:   method removeRange(int,int)
  location: variable automobili of type List<Automobili>
.\AutomobileTableModel.java:54: error: cannot find symbol
                        fireTableRowsInsert(automobili.size(), rowIndex);
                        ^
  symbol:   method fireTableRowsInsert(int,int)
  location: class AutomobileTableModel
2 errors

解决这个问题我只是删除并再次写了一封我已删除的信件,而不是我重新编译它的工作原理。为什么我也得到这个错误?

0 个答案:

没有答案