框架和形状的背景颜色在paintComponent不工作在netbeans

时间:2016-01-22 01:58:01

标签: java netbeans

使用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);
}

}

1 个答案:

答案 0 :(得分:1)

  

当更改框架的背景时也不起作用

使用getContentPane().setBackground(Color.RED)代替

  

我有这个问题,形状不会显示在框架上

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();
        }

    }

}