如何使用动作监听器销毁一个窗口?

huangapple go评论74阅读模式
英文:

how to dispose a frame using an action listener?

问题

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class login extends JFrame implements ActionListener {
    JTextField text1;
    JTextField text2;
    JButton button3;
    JButton button4;
    JButton button5;
    String username;
    String password;

    public login(){
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(new FlowLayout());

        text1 = new JTextField();
        text1.setPreferredSize(new Dimension(250,50));
        text1.setFont(new Font("MV Boli",Font.PLAIN,15));

        button3 = new JButton("UserName");
        button3.setFont(new Font("MV Boli",Font.PLAIN,15));

        text2 = new JTextField();
        text2.setPreferredSize(new Dimension(250,50));
        text2.setFont(new Font("MV Boli",Font.PLAIN,15));

        button4 = new JButton("PassWord");
        button4.setFont(new Font("MV Boli",Font.PLAIN,15));
        button5 = new JButton("Submit");
        button5.setFont(new Font("MV Boli",Font.PLAIN,15));
        button5.addActionListener(this);

        this.add(button3);
        this.add(text1);
        this.add(button4);
        this.add(text2);
        this.add(button5);
        this.setSize(400,500);
        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==button5) {
            username = text1.getText();
            password = text2.getText();
            // You can pass the username and password to another class or method here.
            // For disposing the frame, you should call the dispose method on the JFrame object.
            this.dispose();
        }       
    }
}

It seems you have some issues with your code. The provided code snippet lacks the part where the frame5 variable is used, and there's an error message indicating a NullPointerException at line 61. To help you resolve this issue, I've made modifications to the code snippet based on what I understood from your description. Please review the modified code above.

Remember to adjust the usage of the username and password variables according to your needs, such as passing them to another class or method. If you're encountering issues with these values being null, make sure you've set them properly within the actionPerformed method.

英文:
    import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
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.JTextField;
public class login extends JFrame implements ActionListener {
JTextField text1;
JTextField text2;
JButton button3;
JButton button4;
JButton button5;
JFrame frame5;
String username;
String password;
String a;
login(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
text1 = new JTextField();
text1.setPreferredSize(new Dimension(250,50));
text1.setFont(new Font("MV Boli",Font.PLAIN,15));
button3 = new JButton("UserName");
button3.setFont(new Font("MV Boli",Font.PLAIN,15));
text2 = new JTextField();
text2.setPreferredSize(new Dimension(250,50));
text2.setFont(new Font("MV Boli",Font.PLAIN,15));
button4 = new JButton("PassWord");
button4.setFont(new Font("MV Boli",Font.PLAIN,15));
button5 = new JButton("Submit");
button5.setFont(new Font("MV Boli",Font.PLAIN,15));
button5.addActionListener(this);
this.add(button3);
this.add(text1);
this.add(button4);
this.add(text2);
this.add(button5);
this.setSize(400,500);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stu
if(e.getSource()==button5) {
frame5.dispose();
username = text1.getText();
password = text2.getText();			
}		
}		
}

I'm getting an error like

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at login.actionPerformed(login.java:61)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6636)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
at java.desktop/java.awt.Component.processEvent(Component.java:6401)
at java.desktop/java.awt.Container.processEvent(Container.java:2263)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5012)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4919)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4548)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4489)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2764)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

I just want to close the current frame when i open the the next frame by calling a new method in the action listener, but I am not able to dispose it.

I also want to inherit the values of username & password from the get text method and use them in another class to verify the username, password by establishing JDBC connection with MySQL database. If I inherit the class when I simply try to print the values of username, password in the new class I am getting the output as null. null is actually getting printed on the output screen. Please tell me how do I inherit these values in another class by also disposing the current frame.

答案1

得分: 2

以下是翻译好的部分:

在引发异常的线程中:"AWT-EventQueue-0" java.lang.NullPointerException at login.actionPerformed(login.java:61) at

引发异常的代码行是:

frame5.dispose();

frame5 字段从未被赋值,因此为 null

最快的修复方法是将 frame5.dispose(); 修改为 dispose();。建议避免不必要地扩展类。因此,更好的方法是删除 extends JFrame

JFrame frame5 = new JFrame();

(构造函数末尾的 this. 应被替换为 frame5.。)

英文:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at login.actionPerformed(login.java:61) at 

The line causing this exception is:

    frame5.dispose();

frame5 field is never assigned so is null.

The quickest fix would be to change the line frame5.dispose(); to dispose();. It is recommended to avoid unnecessarily extending classes. So a better approach would be to remove the extends JFrame.

    JFrame frame5 = new JFrame();

(this. should be replaced by frame5. towards the end of the constructor.)

huangapple
  • 本文由 发表于 2020年10月25日 12:51:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64520466.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定