[英]Code doesn't works but I get no error messages
錯誤:奇怪,我沒有錯誤。
我想做的是:(我還添加了//消息,解釋了我的代碼的作用。)
我想做的很簡單。
1)在JTextField中填寫一個名稱,按Enter,該名稱應出現在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 ) {
}
}
}
Random#nextInt
需要一個正數,但List
lijst
的初始大小為0
因此出現異常
文檔明確指出了這一點
拋出: IllegalArgumentException-如果n不為正
首先檢查List
是否有條目。
if (lijst.size() > 0) {
int n = r.nextInt(lijst.size());
Naam kiesNaam = lijst.get(n);
textvak2.setText(kiesNaam.getIngevoerdNaam());
}
另外:提取Naam
對象並使用其getIngevoerdNaam
方法,而不是從List
轉換對象。
還要記住調試器是您的朋友
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.