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

时间:2019-01-26 16:18:44

标签: java jbutton actionlistener

我目前正在开发一个程序,该程序可以从网站中提取表格并将其转换为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);
    }
}

1 个答案:

答案 0 :(得分:0)

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);
    }
相关问题