小编cra*_*zy4的帖子

如何使用JFileChooser在JPanel中显示图像

我正在尝试使用菜单栏,该菜单允许用户选择文件并在JPanel中显示它,图像应该完全适合JPanel.但是,当从对话框中成功选择文件时,JFileChooser不会显示任何内容.我试过引用很多链接:如何将图像添加到JPanel?浏览图像文件并使用Java Swing显示它 但没有任何效果.请帮忙.以下是我的代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;

class Main {
private JFrame j;
private JMenu jmenu;
private JMenuBar jbar;
private JMenuItem jmi, jexit;
private JPanel jpanel, jpanelbar;
private JButton jpre, jnext;
JLabel image;
ImageIcon ic;
Image img;

Main() {
    j = new JFrame("Image Viewer");
    j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // j.setExtendedState(Frame.MAXIMIZED_BOTH);
    // j.setLocationRelativeTo(null);
    j.setLocationByPlatform(true);
    j.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    jpanel = new JPanel();
    c.anchor = GridBagConstraints.PAGE_START;
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = c.gridy = 0;
    c.gridwidth …
Run Code Online (Sandbox Code Playgroud)

java swing jfilechooser image jpanel

3
推荐指数
1
解决办法
3万
查看次数

标签 统计

image ×1

java ×1

jfilechooser ×1

jpanel ×1

swing ×1