错误当我编译文件时,我没有收到任何错误,但是当我尝试执行此文件时,我收到以下消息:
Exception in thread "main" java.lang.StackOverflowError
at sun.awt.SunToolkit.isInstanceOf(SunToolkit.java:1988)
at sun.awt.SunToolkit.isInstanceOf(SunToolkit.java:1997)
at sun.awt.SunToolkit.isInstanceOf(SunToolkit.java:1997)
at sun.awt.SunToolkit.isInstanceOf(SunToolkit.java:1997)
at sun.awt.SunToolkit.isInstanceOf(SunToolkit.java:1997)
at sun.awt.SunToolkit.isInstanceOf(SunToolkit.java:1997)
at sun.awt.SunToolkit.isInstanceOf(SunToolkit.java:1982)
at javax.swing.LookAndFeel.installProperty(LookAndFeel.java:275)
at javax.swing.plaf.basic.BasicButtonUI.installDefaults(BasicButtonUI.java:102)
at javax.swing.plaf.metal.MetalButtonUI.installDefaults(MetalButtonUI.java:80)
at javax.swing.plaf.basic.BasicButtonUI.installUI(BasicButtonUI.java:88)
at javax.swing.JComponent.setUI(JComponent.java:664)
at javax.swing.AbstractButton.setUI(AbstractButton.java:1807)
at javax.swing.JButton.updateUI(JButton.java:146)
at javax.swing.AbstractButton.init(AbstractButton.java:2172)
at javax.swing.JButton.<init>(JButton.java:136)
at javax.swing.JButton.<init>(JButton.java:109)
at Paneel$boven.<init>(Loterij3.java:65)
at Paneel$kies.<init>(Loterij3.java:125)
at Paneel$boven.<init>(Loterij3.java:66)
at Paneel$kies.<init>(Loterij3.java:125)
然后最后几行重复了很多次。
问题:我必须对代码进行哪些更改才能使其正常工作?
注意:我是 Java 新手,我知道还有更多类似的帖子,但我目前对 Java 的理解有限,无法将它们应用到我的代码中。
如果有人想知道:我正在使用 JCreator。
我想要做什么:我想要做什么是相当简单的。
1)在 JTextField 中填写一个名称,按回车键,该名称应出现在 JTextArea 中。名称在 JTextArea 中后,JTextField 变为空,因此您可以填写另一个名称,依此类推,在 JTextArea 中应该出现名称列表。(这就是我现在想做的)
2)按下按钮 kiesWin 使程序从列表中随机选择一个人。
3)按下按钮 resetL 来重置程序,这样我就可以创建一个新列表来从中随机选择一个获胜者。
整个代码:
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( "Klanten Register" );
frame.setContentPane( new Paneel() );
frame.setVisible( true );
}
}
class Paneel extends JPanel {
private boven Boven;
JTextArea textvak1;
JTextField textvak2;
OnthoudNaam onthoudNaam = new OnthoudNaam();
public Paneel() {
setLayout( new BorderLayout() ); // using border Layout.
setBackground( Color.LIGHT_GRAY );
textvak1 = new JTextArea();
add( new JScrollPane( textvak1 ) );
textvak1.setBackground( Color.WHITE );
textvak2 = new JTextField();
textvak2.setHorizontalAlignment(JTextField.CENTER);
textvak2.setEditable( false );
Boven = new boven();
add( Boven, BorderLayout.NORTH );
add( textvak1, BorderLayout.CENTER );
add( textvak2, BorderLayout.SOUTH );
}
class boven extends JPanel {
JButton kiesWin, resetL;
JLabel label1;
JTextField invoervak1;
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 );
}
}
// de naam
class naam {
private String ingevoerdNaam;
public naam( String ingevoerdNaam) {
this.ingevoerdNaam = ingevoerdNaam;
}
public String getIngevoerdNaam() {
return ingevoerdNaam;
}
}
// Arraylist
class OnthoudNaam extends JPanel {
private 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();
}
}
// invoer handler
public class InvoerVakHandler extends boven implements ActionListener {
public void actionPerformed( ActionEvent e ) {
String invoer = invoervak1.getText();
naam naam = new naam( invoer );
onthoudNaam.voegNaamToe( naam );
textvak1.setText( onthoudNaam.toString() );
}
}
// kies
class kies extends boven implements ActionListener {
public void actionPerformed( ActionEvent e ) {
}
}
// reset
class reset extends boven implements ActionListener {
public void actionPerformed( ActionEvent e ) {
}
}
}
对于所有试图帮助我的人:提前感谢您的所有帮助和耐心!