gpt4 book ai didi

java - ClassCastException : javax. Swing .JButton

转载 作者:行者123 更新时间:2023-12-02 07:08:14 25 4
gpt4 key购买 nike

我只想做一个简单的用户密码登录。

我在那里放了一个 ActionListener,当我按下登录时,只需弹出密码并检查它是否正确。

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class PasswordForm
{
private static String password = "mypass";
public static void main(String[] args)
{
// Basic form create
JFrame frame = new JFrame("Form 1");
frame.setSize(300,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Creating the grid
JPanel panel = new JPanel(new GridBagLayout());
frame.getContentPane().add(panel, BorderLayout.NORTH);
GridBagConstraints c = new GridBagConstraints();

// Create some elements
JTextField usernameInput = new JTextField(10);
c.gridx = 0;
c.gridy = 1;
panel.add(usernameInput,c);

JPasswordField passwordInput = new JPasswordField(10);
c.gridx = 0;
c.gridy = 2;
panel.add(passwordInput,c);

JButton loginInput = new JButton("Login");
c.gridx = 0;
c.gridy = 3;
loginInput.addActionListener(new LoginButton());
panel.add(loginInput,c);


frame.setVisible(true);
}

static class LoginButton implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JTextField usernameInput = (JTextField)e.getSource();
JOptionPane.showMessageDialog(null,"Text is:");
}
}
}

有人帮忙吗?

错误

    Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JButton
at PasswordForm$LoginButton.actionPerformed(PasswordForm.java:56)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

最佳答案

当您忽略代码存在编译错误这一事实并尝试运行它时,会发生以下异常。 (我假设您使用的是 Eclipse。在相关源文件中查找红色错误标记,并检查 Problems View 。)

    Exception in thread "AWT-EventQueue-0" java.lang.Error: 
Unresolved compilation problem:
The method getText() is undefined for the type ActionEvent

奇怪的是,嵌入式编译错误消息似乎与您发布的源代码不对应。要么您更改了代码,要么构建过程中的某些缺陷导致您运行陈旧的类文件。


其他几点:

  • 您在嵌套类中违反了 Java 的命名约定。类名应该总是以大写字母开头。将“登录按钮”更改为“登录按钮”。

  • 您的 PasswordForm 类过多地使用了 staticstatic 内部类没问题,但是将 password 声明为静态,并将所有逻辑放入静态 main 方法会导致问题从长远来看。 (好吧,这段代码显然是实验性的……就目前的形式而言。)

关于java - ClassCastException : javax. Swing .JButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8752099/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com