0

错误:很奇怪,我没有收到任何错误。

我想要做什么:(我还添加了 // 解释我的代码功能的消息。)

我想做的很简单。

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 ) {
    }
}
}
4

1 回答 1

1

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

文档清楚地说明了这一点

抛出: 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方法。

也要记住调试器是你的朋友

于 2013-05-09T10:28:51.097 回答