簡體   English   中英

JFrame在重繪時不刷新

[英]JFrame not refreshing on repaint

我編寫了一個小代碼,用於使用SWING在Java中簡單調整系統中任何圖像的大小。

http://ideone.com/9vii2E

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.FileNameExtensionFilter;

class T extends JPanel implements ActionListener {
    /**
     * 
     */
    private static final long serialVersionUID = -2900955352094956729L;
    int x = 0;
    int flag = 0;
    BufferedImage b;
    Image z;
    JButton j, a, cl;
    JTextField ht, wdth;

    T() {
        j = new JButton("Hi");
        a = new JButton("Resizze");
        a.addActionListener(this);
        add(a);
        j.addActionListener(this);
        add(j);
        setBackground(Color.WHITE);

    }

    class tobi implements ActionListener {// for close button of second window
        public void actionPerformed(ActionEvent asd) {
            // if(x==1)
            {
                int gadda = 100, w = 100;
                gadda = Integer.parseInt(ht.getText());
                w = Integer.parseInt(wdth.getText());
                z = z.getScaledInstance(gadda, w, Image.SCALE_SMOOTH);
                setBackground(Color.BLACK);
                j.repaint();
                JButton ttr = (JButton) asd.getSource();
                Window qwe = SwingUtilities.windowForComponent(ttr);
                qwe.setVisible(false);
            }
        }

    }

    public void meth()// method to create second window
    {
        JFrame win = new JFrame();
        win.setTitle("resizze!!");
        win.setLayout(new FlowLayout());
        JLabel height = new JLabel("Height:");
        ht = new JTextField(20);

        JLabel width = new JLabel("Width:");
        wdth = new JTextField(20);
        JButton cl = new JButton("close");

        cl.addActionListener(new tobi());
        win.add(height);
        win.add(ht);
        win.add(width);
        win.add(wdth);
        win.add(cl);
        win.pack();
        win.setVisible(true);
    }

    public void paintComponent(Graphics g) {

        Graphics2D gt = (Graphics2D) g;
        super.paintComponent(g);
        if (x == 1)
            g.drawImage(z, 0, 0, null);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == j) {
            JFileChooser q = new JFileChooser();
            // q.setFileFilter(new
            // FileNameExtensionFilter("Image Files",ImageIO.getReaderFileSuffixes()));
            q.addChoosableFileFilter(new FileNameExtensionFilter("Image Files",
                    "jpg", "jpeg", "png"));
            int option = q.showOpenDialog(null);
            if (option == JFileChooser.APPROVE_OPTION) {
                File f = q.getSelectedFile();
                try {
                    b = ImageIO.read(f);
                    z = b.getScaledInstance(100, 100, Image.SCALE_SMOOTH);
                } catch (IOException ae) {
                }
            }
            x = 1;
            repaint();
        }

        else if (e.getSource() == a) {
            meth();
        }

    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable(){
    public void run(){
    JFrame j = new JFrame();
        j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        j.setSize(500, 500);
        j.add(new T());
        j.setVisible(true);
    j.revalidate();
    j.repaint();

}
});
    }
}

我要求用戶上傳圖像文件,然后使用參數height和width進行大小調整。 單擊第二個窗口中的關閉按鈕后,沒有任何反應(除了前幾次),而第一個窗口中的圖像應重新繪制。 (對不起,錯誤的變量名和格式)

您遇到了一堆問題,變量命名只是其中之一...

g.drawImage(z, 0, 0, null); 應該是g.drawImage(z, 0, 0, this); ,這可以確保如果由於某些原因仍在處理圖像,則組件可以響應其任何更改並自行計划更新時間表

不要忽略異常

try {
    b = ImageIO.read(f);
    z = b.getScaledInstance(100, 100, Image.SCALE_SMOOTH);
} catch (IOException ae) {

}

應該(至少)

try {
    b = ImageIO.read(f);
    z = b.getScaledInstance(100, 100, Image.SCALE_SMOOTH);
} catch (IOException ae) {
    ae.printStackTrace();
}

這將幫助您解決代碼中的可能問題。

我也將以上內容更改為

try {
    b = ImageIO.read(f);
    z = b;
} catch (IOException ae) {
    ae.printStackTrace();
}

因此,您首先會看到原始圖像(這只是我的觀點),但是由於此時您還沒有任何實際的縮放屬性,因此對我來說很有意義。

您正在隱藏cl變量,將其聲明為實例字段,但是再次將其重新聲明為meth的局部變量。 我不是這個問題,但是您需要意識到這一點。

z = z.getScaledInstance(gadda, w, Image.SCALE_SMOOTH); 應該是z = b.getScaledInstance(gadda, w, Image.SCALE_SMOOTH); 您想從源頭進行擴展,否則將會有很多像素化問題。

您還調用了j.repaint(); 這只是重新繪制按鈕,這顯然不是您想要執行的操作,相反,您應該只在面板本身上調用repaint()

您還應該查看Image.getScaledInstance()的危險以及該示例此示例,以獲取有關如何產生更好的縮放操作的示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM