简体   繁体   English

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

[英]Code doesn't works but I get no error messages

Error: Strange enough I get no errors. 错误:奇怪,我没有错误。

What I'm trying to make: ( I also added // messages that explains what my code does.) 我想做的是:(我还添加了//消息,解释了我的代码的作用。)

What I'm trying to make is fairly simple. 我想做的很简单。

1) Fill in a name in the JTextField, press enter and the name should appear in the JTextArea. 1)在JTextField中填写一个名称,按Enter,该名称应出现在JTextArea中。 After the name is in the JTextArea the JTextField becomes empty so you can fill another name and so on there should appear a list of names in JTextArea. 名称在JTextArea中之后,JTextField变为空,以便您可以填充另一个名称,依此类推,应该在JTextArea中出现一个名称列表。

2) Push the button kiesWin to make the program choose a random person from the list. 2)按下按钮kiesWin,使程序从列表中选择一个随机的人。 (here it goes wrong) (这里出错了)

3) Push the button resetL to reset the program so I can make a new list to choose a random winner from it. 3)按下按钮resetL重置程序,这样我可以列出一个新列表来从中选择随机获胜者。

Problem: When I Push the button Kies (Translated: Choose) it should choose a random name from the ArrayList and show the random name in the JTextField textvak2. 问题:当我按下按钮Kies(翻译:选择)时,它应该从ArrayList中选择一个随机名称,并在JTextField textvak2中显示该随机名称。 But when I push the button Kies the program does nothing. 但是,当我按Kies键时,程序什么也没做。 And it should show me the random chosen name from the ArrayList. 它应该显示从ArrayList中随机选择的名称。

This is the class that doesn't works properly: (I Think) 这是无法正常运行的课程:(我认为)

// 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());
            }
    }
}

For in case you need the whole code: 如果您需要完整的代码:

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

Random#nextInt requires a positive number but the initial size of the List lijst is 0 hence the Exception Random#nextInt需要一个正数,但List lijst的初始大小为0因此出现异常

The docs clearly state this 文档明确指出了这一点

Throws: IllegalArgumentException - if n is not positive 抛出: IllegalArgumentException-如果n不为正

Check that there are entries in the List first. 首先检查List是否有条目。

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

Aside: Rather than casting the object from the List , extract the Naam object and use its getIngevoerdNaam method. 另外:提取Naam对象并使用其getIngevoerdNaam方法,而不是从List转换对象。

Remember too The debugger is your friend 还要记住调试器是您的朋友

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM