猿问

Java JFrame 为什么不显示图片?

createWorkPanel()方法中我创建了一个标签并在那里放了一张图片,但图片没有显示在屏幕上。也许有人知道为什么,请告诉我?

这是我的代码

import javax.swing.*;

import javax.swing.filechooser.FileNameExtensionFilter;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.File;

import java.io.IOException;


public class MainWindow extends JFrame {


    private JButton button1;


    public MainWindow() throws IOException

    {


        setLayout(new BorderLayout());

        createWorkPanel();

        setTitle("Графический дизайнер");

        //setDefaultLookAndFeelDecorated(true);

         setSize(1000,600);

       // setLocation(400,100);

       // setVisible(true);

       // pack(); // automatically size the window to fit its components

        setLocationRelativeTo(null); // center this window on the screen

        setDefaultCloseOperation(EXIT_ON_CLOSE);

    }


    public void createWorkPanel() throws IOException

    {


        setLayout(new FlowLayout());

        button1 = new JButton("Load Images");

        button1.setActionCommand("Button 1 was prassed!");

        add(button1);

        ActionListener  actionListener = new AbstractAction() {

            @Override

            public void actionPerformed(ActionEvent e) {


                File fImg = MyFileChooser.chooseFile("Image Files (png & jpg)", "png", "jpg");

                if (fImg != null)

                {

                    JLabel background=new JLabel(new ImageIcon(fImg.getAbsolutePath()));

                   // background.setLayout(new FlowLayout());

                    add(background);

                    System.out.println(" файл создан");

                }

            }

        };


        button1.addActionListener(actionListener);


    

当年话下
浏览 327回答 1
1回答
随时随地看视频慕课网APP

相关分类

Java
我要回答