在我手动调整窗口大小之前,Java窗口没有正确重新绘制

时间:2014-07-18 10:53:34

标签: java swing

我正在使用一个非常基本的设置来扩展JPanel,我将其添加到JFrame

import java.awt.*;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.event.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.*;
import java.io.*;
import javax.imageio.ImageIO;

public class PinTestMCVE extends JPanel implements ActionListener{

    BufferedImage loadedImage;
    JButton calcButton;

    public static void main(String[] args) {
        new PinTestMCVE();
    }

    public PinTestMCVE() {
        loadedImage = getTestImage();

        JPanel toolbarPanel = new JPanel();
        calcButton = new JButton("calcButton...");
        toolbarPanel.add(calcButton);
        calcButton.addActionListener(this);

        JFrame jf = new JFrame();
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.getContentPane().setLayout(new BorderLayout()); 
        jf.getContentPane().add(toolbarPanel, BorderLayout.NORTH);
        jf.getContentPane().add(this, BorderLayout.CENTER);
        jf.setSize(1250, 950);
        jf.setVisible(true);
    }

    public void paintComponent(Graphics g) {
        g.drawImage(loadedImage, 0, 0, this);
    }

    public void actionPerformed(ActionEvent e) {
        System.out.println("ActionEvent " + e.getActionCommand());
        if(e.getSource().equals(calcButton)){
            this.repaint();
        }
    }


//Please ignore the inner workings of this
public static BufferedImage getTestImage(){
    BufferedImage image = new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = image.createGraphics();
    g2d.setPaint(Color.GRAY);
    g2d.fillRect ( 0, 0, image.getWidth(), image.getHeight() );
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,  RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setPaint(Color.gray);
    int x = 5;
    int y = 7;
    GradientPaint redtowhite = new GradientPaint(x, y, Color.red, 200, y, Color.blue);
    g2d.setPaint(redtowhite);
    g2d.fill(new RoundRectangle2D.Double(x, y, 200, 200, 10, 10));
    return image;
}
}

实际上,窗口是正确绘制的,但是一旦调用paintComponent,旧图像的条带(与工具栏面板的高度相同)在新绘制的图像下方可见 - 类似于扑克牌从甲板上伸出来。但是,如果我通过拖动边框手动调整窗口大小,背景会显示为灰色。

发生了什么,我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

按照here所述,您需要pack()框架才能调用setVisible()。您可以覆盖getPreferredSize()以指定合适的初始Dimension。另请考虑使用Border。另请参阅Initial Threads

image

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.*;

public class PinTestMCVE extends JPanel implements ActionListener{

    private static final int SIZE = 200;
    BufferedImage loadedImage;
    JButton calcButton;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new PinTestMCVE();
            }
        });
    }

    public PinTestMCVE() {
        loadedImage = getTestImage();
        JPanel toolbarPanel = new JPanel();
        calcButton = new JButton("calcButton...");
        toolbarPanel.add(calcButton);
        calcButton.addActionListener(this);

        JFrame jf = new JFrame();
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.add(toolbarPanel, BorderLayout.NORTH);
        jf.add(this, BorderLayout.CENTER);
        jf.pack();
        jf.setLocationRelativeTo(null);
        jf.setVisible(true);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(SIZE, SIZE);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(loadedImage, 0, 0, this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("ActionEvent " + e.getActionCommand());
        if(e.getSource().equals(calcButton)){
            this.repaint();
        }
    }


    //Please ignore the inner workings of this
    public static BufferedImage getTestImage(){
        BufferedImage image = new BufferedImage(SIZE, SIZE, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = image.createGraphics();
        g2d.setPaint(Color.GRAY);
        g2d.fillRect ( 0, 0, image.getWidth(), image.getHeight() );
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,  RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setPaint(Color.gray);
        GradientPaint redtowhite = new GradientPaint(5, 5, Color.red, SIZE, 5, Color.blue);
        g2d.setPaint(redtowhite);
        g2d.fill(new RoundRectangle2D.Double(5, 5, SIZE - 10, SIZE - 10, 10, 10));
        return image;
    }
}