gpt4 book ai didi

在我手动调整窗口大小之前,Java 窗口无法正确重绘

转载 作者:行者123 更新时间:2023-11-30 03:49:19 25 4
gpt4 key购买 nike

我正在使用一个非常基本的设置,其中包含一个扩展 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 ,旧图像的 strip (与工具栏面板的高度相同)在新绘制的图像下方可见- 类似于从一副牌中伸出的扑克牌。但是,如果我通过拖动边框等方式手动调整窗口大小,则背景会按应有的方式变灰。

发生了什么事以及如何解决这个问题?

最佳答案

如上所述here ,您需要在调用 setVisible() 之前对框架进行 pack() 处理。您可以覆盖 getPreferredSize()指定合适的初始维度。还可以考虑使用边框。另请参阅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;
}
}

关于在我手动调整窗口大小之前,Java 窗口无法正确重绘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24823336/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com