简体   繁体   中英

Java: Changing Background color of jPanel

I have panel1 with 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.

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. That being said, when you add ball , it extends to the size of the panel. 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. So a solution would just be to setOpaque(false) to ball .

Also, unless you don't plan on doing anything to the new ball you're adding to the 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. So if you expect whatever b is to change the color of the new ball() you added, think again. You can, with the reference above, do someBall.setBackground()


Also you should follow Java naming convention, class names begin with capital letters
class ballclass Ball

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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