gpt4 book ai didi

java - 为什么矩形不显示?

转载 作者:行者123 更新时间:2023-12-01 09:28:02 26 4
gpt4 key购买 nike

我检查了这段代码几个小时,但矩形没有显示,谁能告诉我为什么它没有显示?:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Main {

public static void main(String[] args) {

JFrame frame = new JFrame();
int resx = 700,resy = 500;
frame.setSize(resx,resy);
frame.setLocationRelativeTo(null);
frame.setTitle("Game");
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
try {
frame.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("C:\\Users\\ivans\\Pictures\\Cookies.png")))));
} catch (IOException e) {
}
frame.repaint();
frame.setLayout(new FlowLayout());
frame.add(new JPanel(){
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.WHITE);
g.fillRect(50,50,450,650);
}
}, BorderLayout.CENTER);
frame.repaint();
frame.setVisible(true);
}

}

每次我尝试激活背景时,都不显示矩形,每次激活矩形时,都不显示背景。请帮忙!

最佳答案

您将 JFrame 的 contentPane 设置为 JLabel(一个不使用布局的容器),因此向其中添加组件将不允许显示该组件,除非您完全指定该组件的大小和位置(即其边界) 。这是我避免对 contentPanes 使用 JLabels 的原因之一(而且它不会根据它所持有的组件设置其首选大小),而是通常更喜欢在后台 JPanel 的 PaintComponent 方法中进行绘图。

侧面建议:

  • 主方法中发生了太多事情 - 除非该程序除了演示目的之外没有任何其他用途
  • 您将 JFrame 的原始 contentPane(JPanel)设置为 FlowLayout,但要明白,一旦更改 contentPane,这就没有意义了。
  • 尽管您假设 contentPane 使用 FlowLayout,但您试图将绘图 JPanel 添加到 BorderLayout 位置,这是没有意义的。
  • 你有一个空的 catch block ,这是几乎不应该做的事情。
  • 将图像作为资源而不是文件获取。
  • 避免使用绝对文件路径,而优先使用资源的相对路径。
  • 如果可以避免,请不要设置事物的大小。
  • 避免所谓的“魔数(Magic Number)”,例如 g.fillRect(50,50,450,650);,因为这会使您的代码难以调试和增强。

例如,类似:

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;

@SuppressWarnings("serial")
public class ShowRectangle extends JPanel {
private static final int RECT_X = 50;
private static final int RECT_Y = RECT_X;
private static final int RECT_W = 200;
private static final int RECT_H = 200;
private static final String URL_SPEC = "https://duke.kenai.com/guitar/DukeAsKeith-daylightSmall.png";
private BufferedImage img;

public ShowRectangle(BufferedImage img) {
this.img = img;
}

// have same JPanel draw image and graphic element
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (img != null) {
g.drawImage(img, 0, 0, this);
}

// avoid magic numbers
// g.fillRect(50,50,450,650);
g.fillRect(RECT_X, RECT_Y, RECT_W, RECT_H);
}

// Size the JPanel to the image size
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet() || img == null) {
return super.getPreferredSize();
}
return new Dimension(img.getWidth(), img.getHeight());
}

private static void createAndShowGui(BufferedImage image) {
ShowRectangle mainPanel = new ShowRectangle(image);

JFrame frame = new JFrame("ShowRectangle");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

public static void main(String[] args) {

try {
URL imageUrl = new URL(URL_SPEC);
BufferedImage img = ImageIO.read(imageUrl);
SwingUtilities.invokeLater(() -> createAndShowGui(img));
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
}
}
例如,如果您避免使用“魔术”数字,则很容易使黑色矩形可拖动,因为它现在是由变量值绘制的,您可以在 MouseAdapter(MouseListener 和 MouseMotionListener 组合)内部更改这些值。例如:

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.*;

@SuppressWarnings("serial")
public class ShowRectangle extends JPanel {
private static final int RECT_X = 50;
private static final int RECT_Y = RECT_X;
private static final int RECT_W = 200;
private static final int RECT_H = 200;
private static final String URL_SPEC = "https://duke.kenai.com/guitar/DukeAsKeith-daylightSmall.png";
private int rectX = RECT_X;
private int rectY = RECT_Y;
private BufferedImage img;

public ShowRectangle(BufferedImage img) {
this.img = img;
MouseAdapter myMouse = new MyMouse();
addMouseListener(myMouse);
addMouseMotionListener(myMouse);
}

// have same JPanel draw image and graphic element
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (img != null) {
g.drawImage(img, 0, 0, this);
}

// avoid magic numbers
// g.fillRect(50,50,450,650);
g.fillRect(rectX, rectY, RECT_W, RECT_H);
}

// Size the JPanel to the image size
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet() || img == null) {
return super.getPreferredSize();
}
return new Dimension(img.getWidth(), img.getHeight());
}

private class MyMouse extends MouseAdapter {
private Point p1;
private Point rectP = null;

@Override
public void mousePressed(MouseEvent e) {
p1 = e.getPoint();
if (new Rectangle(rectX, rectY, RECT_W, RECT_H).contains(p1)) {
rectP = new Point(rectX, rectY);
}
}

@Override
public void mouseDragged(MouseEvent e) {
moveRect(e.getPoint());
}

@Override
public void mouseReleased(MouseEvent e) {
moveRect(e.getPoint());
rectP = null;
}

private void moveRect(Point p2) {
if (rectP == null) {
return;
}
rectX = rectP.x + p2.x - p1.x;
rectY = rectP.y + p2.y - p1.y;
repaint();
}

}

private static void createAndShowGui(BufferedImage image) {
ShowRectangle mainPanel = new ShowRectangle(image);

JFrame frame = new JFrame("ShowRectangle");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

public static void main(String[] args) {

try {
URL imageUrl = new URL(URL_SPEC);
BufferedImage img = ImageIO.read(imageUrl);
SwingUtilities.invokeLater(() -> createAndShowGui(img));
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
}
}

关于java - 为什么矩形不显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39681442/

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