如何在JLabel中显示图像?

时间:2015-03-17 09:42:49

标签: java image swing jlabel imageicon

我在使用通过文件对话框接收的目录在JLabel中显示图像时遇到问题。但要检查我用静态目录检查的问题。但问题仍然存在。

如何在JLabel中显示图片?

public class lib {

private JFrame frame;
/**
 * @wbp.nonvisual location=181,9
 */
private final JLabel label = DefaultComponentFactory.getInstance().createTitle("New JGoodies title");
private JTextField textField;
private JTextField textField_1;
private JTextField textField_4;
private JTextField textField_5;
private JTextField txtTitle;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_6;
private JTextField textField_7;
private JTextField textField_8;
private JTextField textField_9;
private JTextField textField_10;
private JTextField textField_11;
public  JLabel lblNewLabel_22;
private JPanel panel;
/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                lib window = new lib();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public lib() {

    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() 
{

    frame = new JFrame("Libpal");
    frame.setForeground(new Color(49, 53, 223));
    frame.setFont(new Font("Lucida Console", Font.ITALIC, 26));
    setDesign();
    frame.setBounds(0, 0, 800, 600);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(new CardLayout(0, 0));

    JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
    frame.getContentPane().add(tabbedPane, "name_49077541710882");

    JPanel panel_1 = new JPanel();
    tabbedPane.addTab("Student Transaction ", null, panel_1, null);
    panel_1.setLayout(null);

    textField_9 = new JTextField();
    textField_9.setBounds(26, 26, 229, 28);
    panel_1.add(textField_9);
    textField_9.setColumns(10);

    JButton btnNewButton_10 = new JButton("Search");
    btnNewButton_10.setForeground(new Color(0, 100, 0));
    btnNewButton_10.setFont(new Font("Arial Rounded MT Bold", Font.ITALIC, 12));
    btnNewButton_10.setBounds(282, 26, 90, 28);
    panel_1.add(btnNewButton_10);

    JTextPane textPane_2 = new JTextPane();
    textPane_2.setEditable(false);
    textPane_2.setBounds(26, 82, 346, 258);
    panel_1.add(textPane_2);



    List list_1 = new List(5,false);
    list_1.add("Mercury");
    list_1.add("Venus");
    list_1.add("Earth");
    list_1.add("JavaSoft");
    list_1.add("Mars");
    list_1.add("Jupiter");
    list_1.add("Saturn");
    list_1.add("Uranus");
    list_1.add("Neptune");
    list_1.add("Pluto");

    panel_1.add(list_1);
    list_1.setBounds(26, 371, 346, 64);

    List list = new List(5, false);
    list.setBounds(26, 458, 346, 64);
    panel_1.add(list);

    JLabel lblNewLabel_18 = new JLabel("Student ID *");
    lblNewLabel_18.setFont(new Font("Arial Rounded MT Bold", Font.ITALIC, 14));
    lblNewLabel_18.setBounds(26, 6, 90, 21);
    panel_1.add(lblNewLabel_18);

    JLabel lblNewLabel_19 = new JLabel("Student Data");
    lblNewLabel_19.setFont(new Font("Arial Rounded MT Bold", Font.ITALIC, 14));
    lblNewLabel_19.setBounds(26, 61, 110, 21);
    panel_1.add(lblNewLabel_19);

    JLabel lblNewLabel_20 = new JLabel("In Hand");
    lblNewLabel_20.setFont(new Font("Arial Rounded MT Bold", Font.ITALIC, 14));
    lblNewLabel_20.setBounds(26, 352, 55, 16);
    panel_1.add(lblNewLabel_20);

    JLabel lblNewLabel_21 = new JLabel("Wishlist");
    lblNewLabel_21.setFont(new Font("Arial Rounded MT Bold", Font.ITALIC, 14));
    lblNewLabel_21.setBounds(26, 441, 55, 16);
    panel_1.add(lblNewLabel_21);

    JButton btnNewButton_11 = new JButton("Enter");
    btnNewButton_11.setForeground(new Color(0, 100, 0));
    btnNewButton_11.setFont(new Font("Arial Rounded MT Bold", Font.ITALIC, 12));
    btnNewButton_11.setBounds(694, 189, 77, 28);
    panel_1.add(btnNewButton_11);

    JButton btnNewButton_12 = new JButton("Enter");
    btnNewButton_12.setForeground(new Color(0, 100, 0));
    btnNewButton_12.setFont(new Font("Arial Rounded MT Bold", Font.ITALIC, 12));
    btnNewButton_12.setBounds(694, 111, 77, 28);
    panel_1.add(btnNewButton_12);

    JButton btnNewButton_13 = new JButton("Enter");
    btnNewButton_13.setForeground(new Color(0, 100, 0));
    btnNewButton_13.setFont(new Font("Arial Rounded MT Bold", Font.ITALIC, 12));
    btnNewButton_13.setBounds(694, 268, 77, 28);
    panel_1.add(btnNewButton_13);

    JComboBox comboBox_6 = new JComboBox();
    comboBox_6.setBounds(399, 95, 122, 26);
    panel_1.add(comboBox_6);

    JComboBox comboBox_7 = new JComboBox();
    comboBox_7.setBounds(533, 95, 155, 26);
    panel_1.add(comboBox_7);

    JComboBox comboBox_8 = new JComboBox();
    comboBox_8.setBounds(399, 123, 122, 26);
    panel_1.add(comboBox_8);

    JComboBox comboBox_9 = new JComboBox();
    comboBox_9.setBounds(533, 123, 155, 26);
    panel_1.add(comboBox_9);

    JComboBox comboBox_10 = new JComboBox();
    comboBox_10.setBounds(399, 174, 122, 26);
    panel_1.add(comboBox_10);

    JComboBox comboBox_11 = new JComboBox();
    comboBox_11.setBounds(533, 204, 155, 26);
    panel_1.add(comboBox_11);

    JComboBox comboBox_12 = new JComboBox();
    comboBox_12.setBounds(399, 204, 122, 26);
    panel_1.add(comboBox_12);

    JComboBox comboBox_13 = new JComboBox();
    comboBox_13.setBounds(533, 174, 155, 26);
    panel_1.add(comboBox_13);

    JComboBox comboBox_14 = new JComboBox();
    comboBox_14.setBounds(399, 255, 122, 26);
    panel_1.add(comboBox_14);

    JComboBox comboBox_15 = new JComboBox();
    comboBox_15.setBounds(533, 255, 155, 26);
    panel_1.add(comboBox_15);

    JComboBox comboBox_16 = new JComboBox();
    comboBox_16.setBounds(399, 285, 122, 26);
    panel_1.add(comboBox_16);

    JComboBox comboBox_17 = new JComboBox();
    comboBox_17.setBounds(533, 285, 155, 26);
    panel_1.add(comboBox_17);

    JButton btnNewButton_16 = new JButton("Submit");
    btnNewButton_16.setForeground(new Color(0, 100, 0));
    btnNewButton_16.setFont(new Font("Arial Rounded MT Bold", Font.ITALIC, 14));
    btnNewButton_16.setBounds(524, 458, 124, 44);
    panel_1.add(btnNewButton_16);

    textField_10 = new JTextField();
    textField_10.setBounds(487, 407, 199, 28);
    panel_1.add(textField_10);
    textField_10.setColumns(10);

    textField_11 = new JTextField();
    textField_11.setBounds(460, 346, 122, 28);
    panel_1.add(textField_11);
    textField_11.setColumns(10);

    JButton btnNewButton_17 = new JButton("Compute Fine");
    btnNewButton_17.setForeground(new Color(0, 100, 0));
    btnNewButton_17.setFont(new Font("Arial Rounded MT Bold", Font.ITALIC, 12));
    btnNewButton_17.setBounds(594, 346, 122, 28);
    panel_1.add(btnNewButton_17);

    JLabel lblNewLabel_23 = new JLabel("Withdraw");
    lblNewLabel_23.setFont(new Font("Arial Rounded MT Bold", Font.ITALIC, 14));
    lblNewLabel_23.setBounds(399, 77, 98, 16);
    panel_1.add(lblNewLabel_23);

    JLabel lblNewLabel_24 = new JLabel("Deposit");
    lblNewLabel_24.setFont(new Font("Arial Rounded MT Bold", Font.ITALIC, 14));
    lblNewLabel_24.setBounds(399, 156, 55, 16);
    panel_1.add(lblNewLabel_24);

    JLabel lblNewLabel_25 = new JLabel("Wishlist");
    lblNewLabel_25.setFont(new Font("Arial Rounded MT Bold", Font.ITALIC, 14));
    lblNewLabel_25.setBounds(399, 238, 55, 16);
    panel_1.add(lblNewLabel_25);

    JLabel lblNewLabel_26 = new JLabel("Fine Due (Rs.)");
    lblNewLabel_26.setFont(new Font("Arial Rounded MT Bold", Font.ITALIC, 14));
    lblNewLabel_26.setBounds(460, 323, 101, 28);
    panel_1.add(lblNewLabel_26);

    JLabel lblNewLabel_27 = new JLabel("Fine Paid (Rs.)");
    lblNewLabel_27.setFont(new Font("Arial Rounded MT Bold", Font.ITALIC, 14));
    lblNewLabel_27.setBounds(487, 386, 134, 21);
    panel_1.add(lblNewLabel_27);

    JLabel lblMaximum = new JLabel("** Maximum 10 books in each category.");
    lblMaximum.setFont(new Font("SansSerif", Font.BOLD, 12));
    lblMaximum.setForeground(Color.RED);
    lblMaximum.setBounds(472, 506, 229, 16);
    panel_1.add(lblMaximum);




    //Student Update Panel




     JPanel panel = new JPanel();
     tabbedPane.addTab("Student Update ", null, panel, null);
     panel.setLayout(null);



     textField_2 = new JTextField();
     textField_2.setBounds(34, 43, 198, 28);
     panel.add(textField_2);
     textField_2.setColumns(10);

     textField_3 = new JTextField();
     textField_3.setEditable(false);
     textField_3.setBounds(34, 105, 198, 28);
     panel.add(textField_3);
     textField_3.setColumns(10);

     textField_6 = new JTextField();
     textField_6.setBounds(255, 229, 122, 28);
     panel.add(textField_6);
     textField_6.setColumns(10);

     textField_7 = new JTextField();
     textField_7.setBounds(34, 229, 198, 28);
     panel.add(textField_7);
     textField_7.setColumns(10);

     JButton btnNewButton_5 = new JButton("Generate ");
     btnNewButton_5.setForeground(new Color(0, 100, 0));
     btnNewButton_5.setFont(new Font("Arial Rounded MT Bold", Font.ITALIC, 12));
     btnNewButton_5.setBounds(255, 43, 122, 28);
     panel.add(btnNewButton_5);

     textField_8 = new JTextField();
     textField_8.setBounds(34, 167, 122, 28);
     panel.add(textField_8);
     textField_8.setColumns(10);

     JButton btnNewButton_6 = new JButton("Browse ");
     btnNewButton_6.setForeground(new Color(0, 100, 0));
     btnNewButton_6.setFont(new Font("Arial Rounded MT Bold", Font.ITALIC, 12));
     btnNewButton_6.setBounds(256, 103, 121, 28);
     panel.add(btnNewButton_6);
     btnNewButton_6.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            try {
                sUpdateBrowse();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
     });


     JComboBox comboBox_2 = new JComboBox();
     comboBox_2.setModel(new DefaultComboBoxModel(new String[] {"Select", "Diploma", "B.Tech", "M.Tech"}));
     comboBox_2.setBounds(34, 292, 122, 26);
     panel.add(comboBox_2);

     JComboBox comboBox_3 = new JComboBox();
     comboBox_3.setModel(new DefaultComboBoxModel(new String[] {"Select", "Civil", "Computer Science", "Electrical", "Electronics & Communication", "Mechanical"}));
     comboBox_3.setBounds(188, 292, 189, 26);
     panel.add(comboBox_3);

     JComboBox comboBox_4 = new JComboBox();
     comboBox_4.setModel(new DefaultComboBoxModel(new String[] {"Select", "First - I", "Second - II", "Third - III", "Fourth - IV", "Fifth - V", "Sixth - VI", "Seventh - VII", "Eighth - VIII"}));
     comboBox_4.setBounds(34, 354, 122, 26);
     panel.add(comboBox_4);

     JComboBox comboBox_5 = new JComboBox();
     comboBox_5.setModel(new DefaultComboBoxModel(new String[] {"Select", "Male", "Female"}));
     comboBox_5.setBounds(188, 354, 189, 26);
     panel.add(comboBox_5);

     JButton btnNewButton_7 = new JButton("New Entry");
     btnNewButton_7.setForeground(new Color(0, 100, 0));
     btnNewButton_7.setFont(new Font("Arial Rounded MT Bold", Font.ITALIC, 18));
     btnNewButton_7.setBounds(94, 446, 135, 41);
     panel.add(btnNewButton_7);

     JButton btnNewButton_8 = new JButton("Submit");
     btnNewButton_8.setForeground(new Color(0, 100, 0));
     btnNewButton_8.setFont(new Font("Arial Rounded MT Bold", Font.ITALIC, 18));
     btnNewButton_8.setBounds(323, 446, 135, 41);
     panel.add(btnNewButton_8);

     JButton btnNewButton_9 = new JButton("Print");
     btnNewButton_9.setForeground(new Color(0, 100, 0));
     btnNewButton_9.setFont(new Font("Arial Rounded MT Bold", Font.ITALIC, 18));
     btnNewButton_9.setBounds(552, 446, 135, 41);
     panel.add(btnNewButton_9);

     JTextPane textPane_1 = new JTextPane();
     textPane_1.setEditable(false);
     textPane_1.setBounds(408, 123, 346, 254);
     panel.add(textPane_1);

     JLabel lblNewLabel_8 = new JLabel("Student ID*");
     lblNewLabel_8.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 12));
     lblNewLabel_8.setBounds(34, 25, 111, 16);
     panel.add(lblNewLabel_8);

     JLabel lblNewLabel_9 = new JLabel("Image*");
     lblNewLabel_9.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 12));
     lblNewLabel_9.setBounds(34, 83, 55, 22);
     panel.add(lblNewLabel_9);

     JLabel lblNewLabel_10 = new JLabel("Surname*");
     lblNewLabel_10.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 12));
     lblNewLabel_10.setBounds(255, 208, 100, 26);
     panel.add(lblNewLabel_10);

     JLabel lblNewLabel_11 = new JLabel("First Name*");
     lblNewLabel_11.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 12));
     lblNewLabel_11.setBounds(34, 209, 135, 24);
     panel.add(lblNewLabel_11);

     JLabel lblNewLabel_12 = new JLabel("Admission Year*");
     lblNewLabel_12.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 12));
     lblNewLabel_12.setBounds(34, 145, 111, 28);
     panel.add(lblNewLabel_12);

     JLabel lblNewLabel_13 = new JLabel("Course*");
     lblNewLabel_13.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 12));
     lblNewLabel_13.setBounds(34, 269, 55, 26);
     panel.add(lblNewLabel_13);

     JLabel lblNewLabel_14 = new JLabel("Branch*");
     lblNewLabel_14.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 12));
     lblNewLabel_14.setBounds(188, 269, 55, 26);
     panel.add(lblNewLabel_14);

     JLabel lblNewLabel_15 = new JLabel("Semester*");
     lblNewLabel_15.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 12));
     lblNewLabel_15.setBounds(34, 330, 100, 28);
     panel.add(lblNewLabel_15);

     JLabel lblNewLabel_16 = new JLabel("Gender*");
     lblNewLabel_16.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 12));
     lblNewLabel_16.setBounds(188, 330, 55, 28);
     panel.add(lblNewLabel_16);

     JLabel lblNewLabel_17 = new JLabel("Library Card");
     lblNewLabel_17.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 18));
     lblNewLabel_17.setBounds(408, 95, 150, 28);
     panel.add(lblNewLabel_17);










    //Book Update Panel


    JPanel panel_2 = new JPanel();
    tabbedPane.addTab("Book Update ", null, panel_2, null);
    panel_2.setLayout(null);

    textField_1 = new JTextField();
    textField_1.setBounds(282, 116, 220, 28);
    panel_2.add(textField_1);
    textField_1.setColumns(10);

    textField = new JTextField();
    textField.setBounds(52, 48, 157, 28);
    panel_2.add(textField);
    textField.setColumns(10);

    textField_4 = new JTextField();
    textField_4.setBounds(512, 177, 220, 28);
    panel_2.add(textField_4);
    textField_4.setColumns(10);

    textField_5 = new JTextField();
    textField_5.setBounds(509, 116, 223, 28);
    panel_2.add(textField_5);
    textField_5.setColumns(10);

    txtTitle = new JTextField();
    txtTitle.setBounds(52, 116, 220, 28);
    panel_2.add(txtTitle);
    txtTitle.setColumns(10);

    JButton btnNewButton_1 = new JButton("New Entry");
    btnNewButton_1.setForeground(new Color(0, 100, 0));
    btnNewButton_1.setFont(new Font("Arial Rounded MT Bold", Font.ITALIC, 18));
    btnNewButton_1.setBounds(512, 302, 131, 50);
    panel_2.add(btnNewButton_1);

    JComboBox comboBox = new JComboBox();
    comboBox.setModel(new DefaultComboBoxModel(new String[] {"Select", "Economy", "Fiction", "General Sciences", "Humanities", "Miscelaneous", "Technical"}));
    comboBox.setBounds(52, 178, 220, 26);
    panel_2.add(comboBox);

    JComboBox comboBox_1 = new JComboBox();
    comboBox_1.setModel(new DefaultComboBoxModel(new String[] {"Select", "Civil", "Computer Science", "Electrical", "Electronics & Communication", "Mechanical", "Multi Branch"}));
    comboBox_1.setBounds(282, 178, 220, 26);
    panel_2.add(comboBox_1);

    JButton btnNewButton_2 = new JButton("Generate");
    btnNewButton_2.setForeground(new Color(0, 100, 0));
    btnNewButton_2.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 13));
    btnNewButton_2.setBounds(221, 48, 104, 28);
    panel_2.add(btnNewButton_2);

    JLabel lblNewLabel = new JLabel("Account No.*");
    lblNewLabel.setFont(new Font("Arial Rounded MT Bold", Font.ITALIC, 14));
    lblNewLabel.setBounds(52, 30, 157, 16);
    panel_2.add(lblNewLabel);

    JLabel lblNewLabel_1 = new JLabel("Title*");
    lblNewLabel_1.setFont(new Font("Arial Rounded MT Bold", Font.ITALIC, 14));
    lblNewLabel_1.setBounds(52, 98, 55, 16);
    panel_2.add(lblNewLabel_1);

    JLabel lblNewLabel_2 = new JLabel("Author*");
    lblNewLabel_2.setFont(new Font("Arial Rounded MT Bold", Font.ITALIC, 14));
    lblNewLabel_2.setBounds(282, 98, 131, 16);
    panel_2.add(lblNewLabel_2);

    JLabel lblNewLabel_3 = new JLabel("Publisher*");
    lblNewLabel_3.setFont(new Font("Arial Rounded MT Bold", Font.ITALIC, 14));
    lblNewLabel_3.setBounds(512, 98, 112, 16);
    panel_2.add(lblNewLabel_3);

    JLabel lblNewLabel_4 = new JLabel("Category*");
    lblNewLabel_4.setFont(new Font("Arial Rounded MT Bold", Font.ITALIC, 14));
    lblNewLabel_4.setBounds(52, 156, 104, 26);
    panel_2.add(lblNewLabel_4);

    JLabel lblNewLabel_5 = new JLabel("Branch*");
    lblNewLabel_5.setFont(new Font("Arial Rounded MT Bold", Font.ITALIC, 14));
    lblNewLabel_5.setBounds(282, 161, 97, 16);
    panel_2.add(lblNewLabel_5);

    JLabel lblNewLabel_6 = new JLabel("Price*");
    lblNewLabel_6.setFont(new Font("Arial Rounded MT Bold", Font.ITALIC, 14));
    lblNewLabel_6.setBounds(512, 161, 79, 16);
    panel_2.add(lblNewLabel_6);

    JButton btnNewButton_3 = new JButton("Submit");
    btnNewButton_3.setForeground(new Color(0, 100, 0));
    btnNewButton_3.setFont(new Font("Arial Rounded MT Bold", Font.ITALIC, 18));
    btnNewButton_3.setBounds(512, 364, 131, 50);
    panel_2.add(btnNewButton_3);

    JLabel lblNewLabel_7 = new JLabel("Book Label");
    lblNewLabel_7.setFont(new Font("Arial Rounded MT Bold", Font.ITALIC, 14));
    lblNewLabel_7.setBounds(52, 216, 104, 28);
    panel_2.add(lblNewLabel_7);

    JButton btnNewButton_4 = new JButton("Print");
    btnNewButton_4.setForeground(new Color(0, 100, 0));
    btnNewButton_4.setFont(new Font("Arial Rounded MT Bold", Font.ITALIC, 18));
    btnNewButton_4.setBounds(512, 426, 131, 50);
    panel_2.add(btnNewButton_4);

    JTextPane textPane = new JTextPane();
    textPane.setEditable(false);
    textPane.setBounds(52, 244, 361, 268);
    panel_2.add(textPane);

    JPanel panel_3 = new JPanel();
    tabbedPane.addTab("Book Management ", null, panel_3, null);

    JPanel panel_5 = new JPanel();
    tabbedPane.addTab("Student Management ", null, panel_5, null);







    // log out pane

    JPanel panel_4 = new JPanel();
    tabbedPane.addTab("Log Out ", null, panel_4, null);
    panel_4.setLayout(null);

    JButton btnNewButton = new JButton("Log Out");    // log out button
    btnNewButton.setForeground(new Color(0, 100, 0));
    btnNewButton.setFont(new Font("Arial Rounded MT Bold", Font.BOLD, 20));
    btnNewButton.setBounds(287, 305, 209, 72);
    panel_4.add(btnNewButton);

    JLabel lblYouAreSigned = new JLabel("You are signed in. Click \"Log Out\" to sign out.");  // log out message
    lblYouAreSigned.setForeground(new Color(255, 0, 0));                                       // log out message color
    lblYouAreSigned.setFont(new Font("Arial Rounded MT Bold", Font.BOLD, 17));                 // log out message font
    lblYouAreSigned.setBounds(183, 217, 418, 34);                                               // log out message location
    panel_4.add(lblYouAreSigned);                                                               // log out message added
}




// class for selecting theme.
 public final void setDesign() {

        try 
        {

            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");

        } 
        catch(Exception e) 
        {  

        }

    }



 // method to browse image
    private void sUpdateBrowse() throws IOException 
    {

        FileDialog fd = new FileDialog(frame,"File Dialog",FileDialog.LOAD);
        fd.setFile("*.jpg;*.jpeg;*.png;*.GIF");
        fd.setVisible(true);
        String imagefilepath = fd.getDirectory()+fd.getFile();
        File studentimage = new File("D:/Koala.jpg");
        BufferedImage image = ImageIO.read(studentimage);
        ImageIcon icon = new ImageIcon("D:/Koala.jpg");




        if(fd.getDirectory()==null)
        {
            textField_3.setText("Select Please*");
            textField_3.setForeground(Color.red);
        }
        else
        {   
            System.out.println(imagefilepath);
            textField_3.setText(imagefilepath);     
            JLabel lblNewLabel_22 = new JLabel();
            lblNewLabel_22.setIcon(icon);
            lblNewLabel_22.setBounds(405, 21, 200, 50);
            panel.add(lblNewLabel_22);


        }
    }

}

1 个答案:

答案 0 :(得分:-1)

此代码段显示如何在JLabel中显示图像:

public class ImageTest implements Runnable {
    static Proxy proxy = Proxy.NO_PROXY;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new ImageTest());
    }

    @Override
    public void run() {
        JFrame frame = new JFrame(ImageTest.class.getSimpleName());
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setSize(640, 480);
        frame.setLayout(new BorderLayout(4, 4));
        ImageIcon icon = null;
        try {
            URL url = new URL("https://lh4.googleusercontent.com/-s5QK2lESN1Y/AAAAAAAAAAI/AAAAAAAABBs/Tqyow0qB8vk/photo.jpg");
            URLConnection connection = url.openConnection(proxy);
            icon = new ImageIcon(ImageIO.read(connection.getInputStream()));
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }
        JLabel label = new JLabel("Test", icon, SwingConstants.CENTER);
        frame.add(label, BorderLayout.CENTER);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}