gpt4 book ai didi

java - PaintComponent 上的框架和形状的背景颜色在 Netbeans 上不起作用

转载 作者:行者123 更新时间:2023-12-01 10:33:39 26 4
gpt4 key购买 nike

在java上工作,我遇到这个问题,形状不会显示在框架上。此外,当更改框架的背景时,它也不起作用。有人可以帮我解决这个问题吗?任何帮助将非常感激。这是我的代码:

public class PlayingWithShapes extends JFrame{

JMenuBar menuBar;
JMenu shapesMenu, colorsMenu;
JMenuItem rectangle, circle, square;
JMenuItem blue, red, green;

public PlayingWithShapes()
{
menuBar = new JMenuBar();
shapesMenu = new JMenu("Shapes");
colorsMenu = new JMenu("Colors");
rectangle = new JMenuItem("Rectangle");
circle = new JMenuItem("Circle");
square = new JMenuItem("Square");

blue = new JMenuItem("blue");
red = new JMenuItem("red");
green = new JMenuItem("green");

shapesMenu.add(rectangle);
shapesMenu.add(circle);
shapesMenu.add(square);

colorsMenu.add(blue);
colorsMenu.add(red);
colorsMenu.add(green);

menuBar.add(shapesMenu);
menuBar.add(colorsMenu);

setJMenuBar(menuBar);
setBackground(Color.RED);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(600,400));
pack();

}

PainComponent部分--不显示在框架上

public void paintComponent(Graphics shapes)
{
super.paintComponents(shapes);
//rectangle
shapes.setColor(Color.MAGENTA);
shapes.fillOval(250, 100, 150, 100);
}


public static void main(String[] args) {
PlayingWithShapes shapes = new PlayingWithShapes();
shapes.setVisible(true);
}

}

最佳答案

also when changing the background of the frame it does not work

改用getContentPane().setBackground(Color.RED)

I have this problem, the shapes will not display on the frame

JFrame 没有 paintComponent 方法,您可以使用 @Override 注释来测试它...

@Override
public void paintComponent(Graphics shapes)
{
// Oh look, it's paintComponent**s**, that should have raised
// eyebrows
super.paintComponents(shapes);
//rectangle
shapes.setColor(Color.MAGENTA);
shapes.fillOval(250, 100, 150, 100);
}

首先查看 Painting in AWT and SwingPerforming Custom Painting有关在 Swing 中绘画的更多详细信息。

一般建议:

  • 永远不要从 JFrame 扩展,它对您没有任何好处
  • 首先从 JPanel 等扩展开始,并重写其 paintComponent 方法。然后,将此面板的实例添加到您喜欢的任何容器中。

例如...

Shapes

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class PlayingWithShapes {

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

public PlayingWithShapes() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JMenuBar menuBar = new JMenuBar();
JMenu shapesMenu = new JMenu("Shapes");
JMenu colorsMenu = new JMenu("Colors");
JMenuItem rectangle = new JMenuItem("Rectangle");
JMenuItem circle = new JMenuItem("Circle");
JMenuItem square = new JMenuItem("Square");

JMenuItem blue = new JMenuItem("blue");
JMenuItem red = new JMenuItem("red");
JMenuItem green = new JMenuItem("green");

shapesMenu.add(rectangle);
shapesMenu.add(circle);
shapesMenu.add(square);

colorsMenu.add(blue);
colorsMenu.add(red);
colorsMenu.add(green);

menuBar.add(shapesMenu);
menuBar.add(colorsMenu);

JFrame frame = new JFrame("Testing");
frame.setJMenuBar(menuBar);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new ShapePane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class ShapePane extends JPanel {

public ShapePane() {
setBackground(Color.RED);
}

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

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(Color.MAGENTA);
g2d.fillOval(250, 100, 150, 100);
g2d.dispose();
}

}

}

关于java - PaintComponent 上的框架和形状的背景颜色在 Netbeans 上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34937765/

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