第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

瀏覽圖像文件并使用Java Swing顯示它

瀏覽圖像文件并使用Java Swing顯示它

幕布斯6054654 2019-11-04 10:05:12
我的問題是,單擊Browse按鈕后,它會(huì)顯示目錄中的所有文件供選擇,然后所選的圖像會(huì)正確顯示在GUI中。但是,當(dāng)我第二次單擊“瀏覽”按鈕時(shí),它僅顯示舊圖像,而不顯示新圖像。請(qǐng)幫助我。package GUI;import java.awt.BorderLayout;import java.awt.EventQueue;import java.awt.Graphics2D;import javax.imageio.ImageIO;import javax.swing.ImageIcon;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.border.EmptyBorder;import javax.swing.JLabel;import javax.swing.JButton;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import java.util.logging.Level;import java.util.logging.Logger;import javax.swing.GroupLayout;import javax.swing.GroupLayout.Alignment;@SuppressWarnings("serial")public class MainAppFrame extends JFrame {    private JPanel contentPane;    File targetFile;    BufferedImage targetImg;    public JPanel panel,panel_1;    private static final int baseSize = 128;    private static final String basePath =![enter image description here][1]            "C:\\Documents and Settings\\Administrator\\Desktop\\Images";    /**     * Launch the application.     */    public static void main(String[] args) {        EventQueue.invokeLater(new Runnable() {            public void run() {                try {                    MainAppFrame frame = new MainAppFrame();                    frame.setVisible(true);                    frame.setResizable(false);                } catch (Exception e) {                    e.printStackTrace();                }            }        });    }    /**     * Create the frame.     */    public MainAppFrame() {        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        setBounds(100, 100, 550, 400);        contentPane = new JPanel();        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));        setContentPane(contentPane);        contentPane.setLayout(new BorderLayout(0, 0));
查看完整描述

3 回答

?
不負(fù)相思意

TA貢獻(xiàn)1777條經(jīng)驗(yàn) 獲得超10個(gè)贊

每次選擇新圖像時(shí),都在這里不必要地創(chuàng)建了組件,并且錯(cuò)誤地出現(xiàn)在這里:


public void setTarget(File reference) {

    //....

    panel_1.setLayout(new BorderLayout(0, 0));

    panel_1.add(new JLabel(new ImageIcon(targetImg))); 

    setVisible(true);

相反,我建議您在選擇任何文件/圖像之前,從一開始就創(chuàng)建所有這些組件,然后在此方法中,根據(jù)圖像創(chuàng)建一個(gè)ImageIcon,然后簡(jiǎn)單地使用此Icon來設(shè)置已經(jīng)存在的JLabel,而不是新的JLabel。只需調(diào)用myLabel.setIcon(new ImageIcon(targetImg));


查看完整回答
反對(duì) 回復(fù) 2019-11-04
  • 3 回答
  • 0 關(guān)注
  • 1187 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)