【问题标题】:not able to set Background color of the panel无法设置面板的背景颜色
【发布时间】:2015-06-06 07:54:46
【问题描述】:

在我的图形分配中,我无法设置面板的背景颜色。如果我将 ColorPanel 放入另一个面板,面板背景的颜色会发生变化,但是如果我改变面板的大小,圆圈不会移动。在我的作业中,如果我更改框架的大小,则要求圆圈应位于框架的中心

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    public class ColorMenuFrame extends JFrame {
      private JMenu colorMenu = new JMenu("Colors");
      private ColorPanel p = new ColorPanel();

      public ColorMenuFrame(){
        p.setPreferredSize( new Dimension( 100, 100 ) );
        this.add(p);
      }

      // Main Method
      public static void main(String[] args) {
        ColorMenuFrame frame = new ColorMenuFrame();
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
      }

      //ColoRpanel class

      public class ColorPanel extends JPanel {
        private int diameter;

        public ColorPanel() {

      // Creating Colors JMEnuBar
        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);

        JMenu colorMenu = new JMenu("Colors");
        menuBar.add(colorMenu);

      // Creating Background and Foreground menus
       JMenu backGroundMenu = new JMenu("BackGround");
       JMenu foreGroundMenu = new JMenu("ForeGround");

      // Items inside the Background and Foreground menus
       JMenuItem redItem = new JMenuItem("Red");
       JMenuItem greenItem = new JMenuItem("Green");
       JMenuItem blueItem = new JMenuItem("Blue");

       JMenuItem redItem1 = new JMenuItem("Red");
       JMenuItem greenItem1 = new JMenuItem("Green");
       JMenuItem blueItem1 = new JMenuItem("Blue");

       // Adding Red, Green and Blue Items to backGround menu     
       backGroundMenu.add(redItem);
       backGroundMenu.add(greenItem);
       backGroundMenu.add(blueItem);

      // Adding Red, Green and Blue Items to ForeGround menu  
       foreGroundMenu.add(redItem1);
       foreGroundMenu.add(greenItem1);
       foreGroundMenu.add(blueItem1);

      // Adding backGround and foreground sub menus to main Menu Bar Colors  
       colorMenu.add(backGroundMenu);
       colorMenu.add(foreGroundMenu);

      //Calling ActionListeners

      // Calling ActionListner after clicking on Red button

        redItem.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            setBackground(Color.RED);
            repaint();
        }
       });

      // Calling ActionListner after clicking on Green button
       greenItem.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
           setBackground(Color.GREEN);
          repaint();
         }
       });

        // Calling ActionListner after clicking on blue button

       blueItem.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
           setBackground(Color.BLUE);
           repaint();
         }
       });

        // Calling ActionListner after clicking on Red button
       redItem1.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
           setForeground(Color.RED);
           repaint();
        }
      });

       // Calling ActionListner after clicking on Green button
      greenItem1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
         setForeground(Color.GREEN);
         repaint();
        }
      });

        // Calling ActionListner after clicking on Green button
      blueItem1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          setForeground(Color.BLUE);
         repaint();
       }
      });

     }
     //Paint Component Method, to create circle at the center of panel    
     protected void paintComponent(Graphics g){
       int width = getSize().width;
       int height =getSize().height;

       if (width <= height){
         diameter = width / 2;
       }
       else if (height <= width){
         diameter = height / 2;
       }
        int r = diameter / 2;
        int x = (width / 2) - r;
        int y = (height / 2)- r;

       //Drawing Circle
       g.fillOval(x, y, diameter, diameter);
       }
       }
      }

【问题讨论】:

    标签: java swing colors jpanel paintcomponent


    【解决方案1】:

    不要忘记在您的 paintComponent 方法覆盖中调用 super.paintComponent(g)。在第一行做。

      @Override
      protected void paintComponent(Graphics g) {
         super.paintComponent(g);
         // ....
    

    这将告诉 JPanel 自己做家务绘制。

    另外,如果您想平滑 Circle 的边框,请将图形渲染提示设置为进行抗锯齿:

         // do this:
         Graphics2D g2 = (Graphics2D) g;
         g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    
         // before calling this:
         g.fillOval(x, y, diameter, diameter);
    

    请注意,当我运行您的代码时,您的圆圈似乎可以很好地居中。

    【讨论】:

    • 非常感谢。我添加了 super.paintComponent(g);在paintComponent 方法中,它起作用了
    【解决方案2】:

    是否要将面板设置在框架的中心?你可以用 panel.setLocationRelativeTo(frame); (当然,您的面板类中应该有一个框架实例)

    【讨论】:

      猜你喜欢
      • 2012-06-04
      • 2014-03-29
      • 1970-01-01
      • 1970-01-01
      • 2013-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多