繁体   English   中英

如何修复无响应的按钮侦听器?

[英]How do I Fix an Unresponsive Button Listener?

我目前正在开发一个程序,该程序可以从网站中提取表格并将其转换为csv文件。 由于某种原因,我放入GUI中的JButton启动该过程没有响应。 此版本应打开一个面板。 该按钮出现,但是单击它不会执行任何操作。 如何获得激活动作监听器的按钮? 我的代码如下。

package importfriendly;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class UserInterface extends JPanel implements ActionListener {

    /**
     * 
     */
    private static final long serialVersionUID = 7729565734298298985L;
    public UserInterface() {
        JLabel lab1 = new JLabel("Insert URL Here");
        lab1.setLocation(250, 0);
        JTextField jtf = new JTextField(20);
        jtf.setLocation(250, 50);
        JButton initiate = new JButton("Start");
        initiate.setLocation(250, 100);
        add(lab1);
        add(jtf);
        add(initiate);
        initiate.addActionListener(null);

        }
        // TODO Auto-generated constructor stub


    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        ;
        }
        JFrame frame2 = new JFrame("Loading");
        frame2.setSize(100, 200);
        frame2.setResizable(true);
        frame2.setVisible(true);
        frame2.setContentPane(new Loading_Panel());
        frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame2.setLocation(400, 500);
    }




    public static void main(String[] args) {
        // TODO Auto-generated method stub
        JFrame frame = new JFrame("RazorScraper");
        frame.setSize(300, 300);
        frame.setResizable(true);
        frame.setVisible(true);
        frame.setContentPane(new UserInterface());
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setLocation(400, 500);
    }
}

更改initiate.addActionListener(null); initiate.addActionListener(this);

另外,您需要在ActionPerformed函数中插入创建frame2的代码,如下所示:

  @Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
     JFrame frame2 = new JFrame("Loading");
     frame2.setSize(100, 200);
     frame2.setResizable(true);
     frame2.setVisible(true);
     frame2.setContentPane(new Loading_Panel());
     frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
     frame2.setLocation(400, 500);
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM