2

あるファイルにボタンを配置し、その actionPerformed(ActionEvent e) メソッドを別のファイルに配置することは可能ですか? actionListener をボタンに追加しようとしていますが、ファイル trialdump.java にある choose1 ですが、actionPerformed(ActionEvent e) メソッドはファイル listen.java にあります。public class trialdump extends listen を拡張しようとしましたが、エラーが表示されます。ボタンにメソッドを追加する方法はありますか? ありがとう。

ファイル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);

        }
    });
}
}
4

3 に答える 3

2

このようなことを行う別のクラスで定義されたリスナーを追加できます

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

ところで、コードのいくつかのビットを処理する必要があります。

  • クラス名は最初の文字を大文字にしてキャメルケースで入力する必要があります
  • パッケージには適切な名前を使用してください
  • リスナークラスのメインメソッドが何か役に立つとは思わない
  • リスナー クラスで JPanel から拡張する必要はまったくありません。
  • listen() で行っているように、空のコンストラクターを作成しないようにしてください。少なくとも super() を呼び出すか、単に含めないでください。
于 2013-04-09T08:37:52.707 に答える
1

サンプルコードだけで、同様の行で変更を加えることができます-

class Some extends JFrame {

    private JButton button = new JButton("Something");

    Some() {
        button.addActionListener(new MyListener());
    }
}

class MyListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        // do something
    }    
}
于 2013-04-09T08:37:41.667 に答える