简体   繁体   English

Java:更改jPanel的背景颜色

[英]Java: Changing Background color of jPanel

I have panel1 with borderlayout . 我有panel1具有borderlayout There is ball object in it, and I want to change background color with a button, but ball object doesn't allow me to change background. 其中有球形物体,我想用按钮更改背景颜色,但球形物体不允许我更改背景。 Why? 为什么? It doesn't change, neither ball background nor panel background... How do I follow a way? 它不会改变,球背景或面板背景都不会改变...我该如何遵循? Here is my code: 这是我的代码:

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

public class project1 extends JFrame implements ActionListener{


   Color color;
  final JRadioButton sc;
  final JRadioButton bc;
  static JPanel panel1;
  JFrame frame = new JFrame("frame");
  private JPanel panel2;
  private ball b;




  public project1() {



   panel1 = new JPanel();   //panel for ball component

   panel1.setBackground(color);
   panel1.setLayout(new BorderLayout());

  //problem starts here

   panel1.add(b,BorderLayout.CENTER); 

   b.setOpaque(false); 


    panel2 = new JPanel();   //panel for button group
    panel2.setLayout(new FlowLayout());

    panel2.setBorder(BorderFactory.createLineBorder(Color.black));  //draw border for panel1 and panel 2
    panel1.setBorder(BorderFactory.createLineBorder(Color.blue));

    JButton st_btn = new JButton("start");
    JButton sp_btn = new JButton("stop");
    JButton color_btn = new JButton("color");

    sc = new JRadioButton("SC");
    bc = new JRadioButton("BC");

    final ButtonGroup bgroup = new ButtonGroup();
    bgroup.add(sc);
    bgroup.add(bc);



    sc.addActionListener(this);
    bc.addActionListener(this);
    color_btn.addActionListener(this);

    //panel2.add(Button);
    panel2.add(st_btn);
    panel2.add(sp_btn);
    panel2.add(sp_btn);
    panel2.add(color_btn);
    panel2.add(sc);
    panel2.add(bc);


    setLayout(new BorderLayout()); // frame layout

    add(panel2, BorderLayout.SOUTH);
    add(panel1, BorderLayout.CENTER);  // ball appears here


    pack();
    setResizable(false);
    setSize(500, 500);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setVisible(true);

  }


  @Override
   public void actionPerformed(ActionEvent e) {

      color = JColorChooser.showDialog(this, "Select a Background Color", Color.pink);
        Object o = e.getSource();

      if (o.equals(sc)) {
          panel1.setBackground(color);

            repaint();  }

      else if (o.equals(bc)) {
           b.setColor(color);
               repaint();     }
  }



  public static void main(String[] args) {




    javax.swing.SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        project1 ex = new project1();
        ex.setVisible(true);    
      }

    });
  }


}

and this my ball class.ı allocate ball to frame within project1 class.above. 这是我的球类。在上面的project1类中分配球到框架。

import java.awt.*;

import javax.swing.JPanel;


public class ball extends JPanel {





int x_Pos = 180;
  int y_Pos = 100;
  static Color color;  //ball color



    void up() {
    y_Pos -= 5;
    //repaint();
  }

  void down() {
    y_Pos += 5;
    //repaint();
  }

  void left() {
    x_Pos -= 5;
    //repaint();
  }

  void right() {
    x_Pos += 5;
    //repaint();
  }

  public void paintComponent(Graphics aBall) {

    super.paintComponent(aBall);
    aBall.drawOval(x_Pos, y_Pos, 190, 190);  // take fixed radius
    aBall.setColor(color);
    aBall.fillOval(x_Pos, y_Pos, 190, 190);

  }

  void setColor(Color color){
    this.color = color;


}
}

What's probably happening is that since you set the layout of panel to BorderLayout , any preferred size you set for ball doesn't matter, as BorderLayout doesn't respect preferred sizes. 可能发生的情况是,由于将panel的布局设置为BorderLayout ,所以为ball设置的任何首选大小都没有关系,因为BorderLayout不遵守首选大小。 That being said, when you add ball , it extends to the size of the panel. 话虽如此,当您添加ball ,它会扩展到面板的大小。 Since JPanel is opaque by default, you can set the background to the panel , but you won't see it, because it's being covered by the ball panel. 由于JPanel默认情况下是不透明的,因此您可以将panel的背景设置为,但您不会看到它,因为它已被ball面板覆盖。 So a solution would just be to setOpaque(false) to ball . 因此,解决方案就是将setOpaque(false)ball

Also, unless you don't plan on doing anything to the new ball you're adding to the panel . 另外,除非您不打算对新球做任何事情,否则您要添加到panel You may want to give it a reference. 您可能要给它一个参考。

ball someBall = new ball();
panel1.add(someBall ,BorderLayout.CENTER);

It looks like you're trying to change the background of b , which is supposedly the ball in the panel, but you add a new ball() to the panel with no reference. 看来您正在尝试更改b的背景,该背景应该是面板中的球,但是您向面板添加了一个new ball() ,但没有引用。 So if you expect whatever b is to change the color of the new ball() you added, think again. 因此,如果您期望b会改变添加的new ball()的颜色,请再考虑一下。 You can, with the reference above, do someBall.setBackground() 您可以使用上面的参考做someBall.setBackground()


Also you should follow Java naming convention, class names begin with capital letters 您还应该遵循Java命名约定,类名以大写字母开头
class ballclass Ball class ballclass Ball

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM