내가 원한 모두 간단한 사용자 암호 로그인을 만드는 것입니다.ClassCastException : javax.swing.JButton
나는 거기에 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)
질문은 무엇인가? –
그리고 네 질문은 뭐니? – mre
어떻게 작동합니까? 그것은 이상한 오류를 준다 ... – Master345