gpt4 book ai didi

Java:更改 jPanel 的背景颜色

转载 作者:行者123 更新时间:2023-12-01 13:08:34 24 4
gpt4 key购买 nike

我有 panel1borderlayout。其中有球对象,我想用按钮更改背景颜色,但球对象不允许我更改背景。为什么?它不会改变,无论是球背景还是面板背景......我如何遵循一条路?这是我的代码:

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

});
}


}

这是我的球类。我将球分配给项目 1 类中的框架。上面。

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;


}
}

最佳答案

可能发生的情况是,因为您设置了 panel 的布局至BorderLayout ,您为 ball 设置的任何首选尺寸没关系,如BorderLayout不尊重首选尺寸。话虽如此,当您添加 ball 时,它延伸到面板的尺寸。自 JPanel默认不透明,可以将背景设置为panel ,但您不会看到它,因为它被 ball 覆盖。控制板。所以解决方案就是 setOpaque(false)ball

此外,除非您不打算对要添加到 panel 的新球进行任何操作, 。您可能想给它一个引用。

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

您似乎正在尝试更改 b 的背景,这应该是面板中的球,但您添加了 new ball()到面板没有引用。所以如果你期待什么b就是改变new ball()的颜色你补充道,再想想。您可以根据上面的引用,执行 someBall.setBackground()

<小时/>

此外,您应该遵循Java命名约定,类名以大写字母开头
class ballclass Ball

关于Java:更改 jPanel 的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23080563/

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