我正在尝试使用菜单栏,该菜单允许用户选择文件并在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)