代码不起作用但我没有收到任何错误消息

时间:2013-05-09 10:26:02

标签: java swing jpanel illegalargumentexception

错误:奇怪,我没有错误。

我要做的是:(我还添加了//消息,解释了我的代码所做的事情。)

我想要做的很简单。

1)在JTextField中填写名称,按回车键,名称应出现在JTextArea中。名称在JTextArea之后,JTextField变为空,因此您可以填写另一个名称,依此类推,应该在JTextArea中显示一个名称列表。

2)按下按钮kiesWin,使程序从列表中选择一个随机的人。 (这里出错了)

3)按下resetL按钮重置程序,这样我就可以制作一个新列表,从中选择随机获胜者。

问题:当我按下按钮Kies(翻译:选择)时,它应该从ArrayList中选择一个随机名称,并在JTextField textvak2中显示随机名称。但是,当我按下按钮Kies时,程序什么都不做。它应该显示ArrayList中随机选择的名称。

这是无法正常工作的课程:(我认为)

// This is the button that chooses a random name from the ArrayList.
// The random chosen name should appear in the JTextField textvak2. (but it doesn't)
// This is also the part where it goes wrong at the moment.
class Kies extends OnthoudNaam implements ActionListener {
    public void actionPerformed( ActionEvent e ) {
        Random r = new Random();
        if (lijst.size() > 0) {
            int n = r.nextInt(lijst.size());
            Naam kiesNaam = lijst.get(n);
            textvak2.setText(kiesNaam.getIngevoerdNaam());
            }
    }
}

如果您需要整个代码:

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

// Main method to make the frame.
public class Loterij3 extends JFrame {
public static void main( String args[] ) {
    JFrame frame = new Loterij3();
    frame.setExtendedState( frame.MAXIMIZED_BOTH );
    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    frame.setTitle( "My Lottery!" );
    frame.setContentPane( new Paneel() );
    frame.setVisible( true );
}
}

// This is the Panel that goes into the frame.
class Paneel extends JPanel {
private Boven boven;
JTextArea textvak1;
JTextField textvak2;
OnthoudNaam onthoudNaam = new OnthoudNaam();
JTextField invoervak1; // JTextField from class Boven.

public Paneel() {
setLayout( new BorderLayout() ); // using border Layout.
setBackground( Color.LIGHT_GRAY );

boven = new Boven(); 

textvak1 = new JTextArea();
add( new JScrollPane( textvak1 ) );
textvak1.setBackground( Color.WHITE );

textvak2 = new JTextField();
textvak2.setHorizontalAlignment(JTextField.CENTER); 

add( boven, BorderLayout.NORTH ); // Where the class Boven should be.
add( textvak1, BorderLayout.CENTER );
add( textvak2, BorderLayout.SOUTH );
}

// This is the class Boven (Translation up or upper).
// This is where the JButtons, JTextField and JLabels are.
public class Boven extends JPanel {
JButton kiesWin, resetL;
JLabel label1;

public Boven() {
    setBackground( Color.LIGHT_GRAY );
    setLayout( new GridLayout( 1, 4, 100, 5 ) ); // using GridLayout.
    Border border = 
        BorderFactory.createEmptyBorder( 10, 10, 10, 10 );
    setBorder( border );

    kiesWin = new JButton("Kies een Winnaar!");
    kiesWin.addActionListener( new Kies() );
    resetL = new JButton("Reset alles");
    resetL.addActionListener( new Reset() );
    label1 = new JLabel("Voer Persoon in en druk op enter: ", JLabel.RIGHT);
    invoervak1 = new JTextField( 20 );
    invoervak1.addActionListener( new InvoerVakHandler() );

    add( label1 );
    add( invoervak1 );
    add( kiesWin );
    add( resetL );
    }
}

// The class Naam (translation = name).
// This is what the ArrayList should remember
// In other words ArrayList should remember the Names I put in the JTextField.
class Naam {
    private String ingevoerdNaam;

    public Naam( String ingevoerdNaam) {
        this.ingevoerdNaam = ingevoerdNaam;
    }

    public String getIngevoerdNaam() {
        return ingevoerdNaam;
    }

    public String toString() {
        return ingevoerdNaam;
    }
}

// This is my ArrayList,
// This should remember the names I type in the JTextField.
class OnthoudNaam extends JPanel {
    protected ArrayList<Naam> lijst;

    public OnthoudNaam() {
        lijst = new ArrayList<Naam>();
        }

        public void voegNaamToe(Naam x ) {
        lijst.add(x);
        }

        public String toString() {
        StringBuffer buffer = new StringBuffer();
        for(Naam x : lijst ) {
        buffer.append( x );
        buffer.append( "\n" );
    }
    return buffer.toString();
}
}

// This is the JTextField where I enter the names.
// The Name I fill in the JTextField should be remembered by the ArrayList.
// The Name I fill in should be put in the JTextArea.
public class InvoerVakHandler implements ActionListener {
    public void actionPerformed( ActionEvent e ) {
        String invoer = invoervak1.getText();
        Naam Naam = new Naam( invoer );
        onthoudNaam.voegNaamToe( Naam );
        textvak1.setText( onthoudNaam.toString() );
        invoervak1.setText( "" );
    }
}
    // This is the button that chooses a random name from the ArrayList.
    // This is also the part where it goes wrong at the moment.
class Kies extends OnthoudNaam implements ActionListener {
    public void actionPerformed( ActionEvent e ) {
        Random r = new Random();
        if (lijst.size() > 0) {
            int n = r.nextInt(lijst.size());
            Naam kiesNaam = lijst.get(n);
            textvak2.setText(kiesNaam.getIngevoerdNaam());
            }
    }
}

// This should become the button that resets everything so you can start over.
class Reset implements ActionListener {
    public void actionPerformed( ActionEvent e ) {
    }
}
}

1 个答案:

答案 0 :(得分:1)

Random#nextInt需要一个正数,但List lijst的初始大小为0因此异常

docs明确说明了这个

  

<强>抛出:   IllegalArgumentException - 如果n不是正数

首先检查List中是否有条目。

if (lijst.size() > 0) {
   int n = r.nextInt(lijst.size());
   Naam kiesNaam = lijst.get(n);
   textvak2.setText(kiesNaam.getIngevoerdNaam());
}

除此之外:不是从List转换对象,而是提取Naam对象并使用其getIngevoerdNaam方法。

还记得调试器是你的朋友