我的程序不会运行,任何想法?

时间:2015-11-02 15:37:44

标签: java jframe jbutton actionlistener jlist

我正在学校做作业。我应该制作一个程序,您可以在产品测试后将产品名称和分数添加到列表中。

我的问题是我无法运行我的程序,我不知道为什么。 谁知道我做错了什么?

package oblig9;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;

public class Oblig9 extends JFrame implements ActionListener {
    DefaultListModel<Oblig9Brus> brus = new DefaultListModel<Oblig9Brus>();

    public Oblig9(){
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        FlowLayout fl = new FlowLayout();
        this.setLayout(fl);

        JList liste = new JList(brus);
        this.add(liste);

        JButton leggTil = new JButton("Legg til brus");
        this.add(leggTil);
        leggTil.addActionListener(this);

        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
        String produsent = JOptionPane.showInputDialog(this, "Hvilken produsent har du testet?");
        String scoreTekst = JOptionPane.showInputDialog(this, "Hvilken score vil du gi produket?");
        int score = Integer.parseInt(scoreTekst);

        Oblig9Brus o9b = new Oblig9Brus();
        o9b.setProdusent(produsent);
        o9b.setScore(score);
        brus.addElement(o9b);
    }

}

还有一个用于保存分数的课程

package oblig9;

public class Oblig9Brus {
    private String produsent;
    private int score;

    public String getProdusent() {
        return produsent;
    }
    public void setProdusent(String produsent) {
        this.produsent = produsent;
    }
    public int getScore() {
        return score;
    }
    public void setScore(int score) {
        this.score = score;
    }
    public String toString(){
        return produsent + " " + score; 
    }
}

2 个答案:

答案 0 :(得分:0)

添加主要方法

public static void main(String[] args)
{
  new Oblig9();
}

答案 1 :(得分:0)

我认为fabian可能有正确的想法,如果你试图独立运行它。我确实把它包装在一个快速的JUnit中,这似乎传递得很好,但我不确定错误是什么,如果有的话:

@Test
public void testIt(){
    Oblig9 instance = new Oblig9();
    assertNotNull(instance);
}
相关问题