java - JButton 和 actionPerformed 方法是否可能位于不同的文件中?

标签 java swing button actionlistener

是否可以在一个文件上放置一个按钮,而在另一文件中放置其 actionPerformed(ActionEvent e) 方法?我正在尝试向按钮添加一个actionListener,choose1,它位于文件Trialdump.java 中,但actionPerformed(ActionEvent e) 方法位于文件listen.java 中。我尝试扩展公共(public)类审判转储扩展监听,但它显示错误。知道如何将该方法添加到按钮吗?谢谢。

这是我在文件 Trialdump.java 中的代码:

package Core;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.*;
import java.awt.*;

// Create a simple GUI window
public class trialdump {

private static void createWindow() {

    // Create and set up the window.
    JFrame frame = new JFrame("PDF Denoiser");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // My edit
    JPanel panel = new JPanel();
    GroupLayout layout = new GroupLayout(panel);
    panel.setLayout(layout);

    layout.setAutoCreateGaps(true);
    layout.setAutoCreateContainerGaps(true);

    JLabel label1 = new JLabel("Image File");
    JLabel label2 = new JLabel("Destination");
    JLabel label3 = new JLabel("Preview");

    JTextField current = new JTextField();
    JTextField dest = new JTextField();
    JTextArea preview = new JTextArea();

    preview.setEditable(false);
    JScrollPane previewScrollPane = new JScrollPane(preview);

    JButton choose1 = new JButton("Search1");
    JButton choose2 = new JButton("Search2");
    JButton algo1 = new JButton("MDWM");
    JButton algo2 = new JButton("BFMR");
    JButton algo3 = new JButton("Mine");



    // Horizontal arrangement
    layout.setHorizontalGroup(layout
            .createSequentialGroup()
            .addGroup(
                    layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(label1)
                            .addComponent(label2).addComponent(label3))
            .addGroup(
                    layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(current)
                            .addComponent(dest).addComponent(preview))
            .addGroup(
                    layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(choose1)
                            .addComponent(choose2).addComponent(algo1).addComponent(algo2).addComponent(algo3)));

    layout.linkSize(SwingConstants.HORIZONTAL, choose1, choose2, algo1, algo2, algo3);

    // Vertical arrangement
    layout.setVerticalGroup(layout
            .createSequentialGroup()
            .addGroup(
                    layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(label1)
                            .addComponent(current).addComponent(choose1))
            .addGroup(
                    layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(label2)
                            .addComponent(dest).addComponent(choose2))
            .addGroup(
                    layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                            .addComponent(label3)
                            .addComponent(preview)
                            .addGroup(
                                    layout.createSequentialGroup().addComponent(algo1).addComponent(algo2)
                                            .addComponent(algo3))));

    // Display the window.
    frame.setLocationRelativeTo(null);
    frame.add(panel);
    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {

    createWindow();

}
}

这是我在listen.java中的代码:

package components;

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.*;

public class listen extends JPanel implements ActionListener{

static private String newline = "\n";
private JTextArea log;
private JFileChooser fc;

public listen() {



}

public void actionPerformed(ActionEvent e) {
    //Set up the file chooser.
    if (fc == null) {
        fc = new JFileChooser();

    //Add a custom file filter and disable the default
    //(Accept All) file filter.
        fc.addChoosableFileFilter(new imagefilter());
        fc.setAcceptAllFileFilterUsed(false);

    //Add custom icons for file types.
        fc.setFileView(new imagefileview());

    //Add the preview pane.
        fc.setAccessory(new imagepreview(fc));
    }

    //Show it.
    int returnVal = fc.showDialog(listen.this,
                                  "Attach");

    //Process the results.
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();

    } else {
        log.append("Attachment cancelled by user." + newline);
    }
    log.setCaretPosition(log.getDocument().getLength());

    //Reset the file chooser for the next time it's shown.
    fc.setSelectedFile(null);
}

/**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event dispatch thread.
 */


public static void main(String[] args) {
    //Schedule a job for the event dispatch thread:
    //creating and showing this application's GUI.
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            //Turn off metal's use of bold fonts
            UIManager.put("swing.boldMetal", Boolean.FALSE);

        }
    });
}
}

最佳答案

您可以添加在另一个类中定义的监听器,执行类似的操作

JButton choose1 = new JButton("Search1");
choose1.addActionListener(new listen());

顺便说一句,您应该注意代码的更多部分:

  • 类名称应采用驼峰命名法,且首字母大写
  • 为您的包使用正确的名称
  • 我认为监听器类中的主要方法没有任何用处
  • 根本不需要从监听器类中的 JPanel 进行扩展
  • 尽量避免创建空构造函数,就像在listen()中所做的那样。至少调用 super(),或者只是不包含它。

关于java - JButton 和 actionPerformed 方法是否可能位于不同的文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15896991/

相关文章:

java - 在 For 循环增量中使用 if else

Java JPanel 在 BorderLayout 中展开一个面板

java - 不确定为什么这个图形屏幕没有更新

swift - 将 View 作为参数传递给按钮,将其触发为模态

java - Jersey 记录服务的所有响应时间

java - 如何在java中的多个类中使用一个接口(interface)?

java - 如何在 JFrame 中显示两个 JTable

jquery ui按钮图标在firefox中不居中

Python Gtk3 控制按钮大小

java在Linux上发送邮件: Error certification