Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用于更新图像的可重用JFileChooser_Java_Swing_Jfilechooser - Fatal编程技术网

Java 用于更新图像的可重用JFileChooser

Java 用于更新图像的可重用JFileChooser,java,swing,jfilechooser,Java,Swing,Jfilechooser,我正在尝试创建一个函数,它可以预览用户从自定义JFileChooser中选择的图像。以下是来自测试框架和定制JFileChooser的代码片段 Test.java public class Test extends JFrame { private JPanel contentPane; private FileChooser file; private static JLabel lblPicPreview; public static void main(S

我正在尝试创建一个函数,它可以预览用户从自定义JFileChooser中选择的图像。以下是来自测试框架和定制JFileChooser的代码片段

Test.java

public class Test extends JFrame {
    private JPanel contentPane;
    private FileChooser file;
    private static JLabel lblPicPreview;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Test frame = new Test();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Test() {
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("main menu");
        setBounds(100, 100, 960, 540);
        setLocationRelativeTo(null);
        contentPane = new JPanel();
        setContentPane(contentPane);
        contentPane.setLayout(new FlowLayout(FlowLayout.LEADING));

        lblPicPreview = new JLabel();
        lblPicPreview.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                file.setPic();
            }
        });
        lblPicPreview.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 0)), "Preview", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
        lblPicPreview.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        lblPicPreview.setToolTipText("Click to change profile picture");

        file= new FileChooser();

        contentPane.add(file);
        contentPane.add(lblPicPreview);
    }

    // Method to update image
    public static void updatePicture(ImageIcon icon){
        lblPicPreview.setIcon(icon);
    }

    public void refresh(){
        repaint();
        revalidate();
    }
}
FileChooser.java

public class FileChooser extends JPanel implements ActionListener {

    /**
    *  Constructor FileChooser to place the UI for customized JFileChooser
    */

    // actionPerformed for the "select" button in the custom UI
    public void actionPerformed(ActionEvent e){
        returnVal= fileChooser.showOpenDialog(button);

        if(returnVal==JFileChooser.APPROVE_OPTION){
            file= fileChooser.getSelectedFile();
            fileLabel.setText(file.getAbsolutePath());
            img= new ImageIcon(file.getAbsolutePath());
            /** caller object to chg icon when user chooses new image
            *   i want to make this to update the image from its caller
            *   something like this
            *   {sourceLabel}.setIcon(img);
            */
            Test.updatePicture(img);
        }
    }
}
该代码对应用程序运行良好。问题是,我希望使定制的JFileChooser可用于所有其他框架/类。我想使我的自定义JFileChooser足够智能,以使任何调用方的类的JLabel(例如lblPicPreview)使用所选的映像更新,而不是从FileChooser类本身调用Test.java的lblPicPreview.setIcon。这样做是为了我可以将FileChooser用于框架/类的其他实例

一个问题是Test.java的lblPicPreview是一个实例变量,所以我不能从FileChooser类直接使用它


感谢所有的帮助。先谢谢你

只需添加一个方法来指定要设置的标签:

public class FileChooser extends JPanel implements ActionListener {
  protectedJLabel activeLabel;

  public void setActivelabel( JLabel label ) {
    activeLabel = label;
  }
}

如@sʜᤈɪɴɪ所述,请不要重新发布您的问题以更正代码格式ᴠᴀssʜᴜᴋʟᴀ, 不要转发问题。@sʜᤈɪɴɪᴠᴀssʜᴜᴋʟᴀ 我双击了两次错误的按钮。我现在将删除重复的帖子。请原谅。我不确定我是否理解这个问题,所以我只向您指出Swing教程中的一节,其中包含一个演示如何进行图像预览的示例。@camickr我的自定义文件选择器能够提示用户选择所需的图像并在调用方测试中显示。java的lblPicPreview,唯一的问题是,我想让我的自定义JFileChooser足够智能,以使任何调用方的类的JLabel(例如lblPicPreview)使用所选的映像更新,而不是从FileChooser类本身调用Test.java的lblPicPreview.setIcon。这样做是为了我可以将FileChooser用于框架/类的其他实例。