java - 如何在此程序中更改 JFrame 的背景颜色?
问题描述
我使用驱动程序类中的 setBackground() 方法来更改背景颜色,但它不起作用。
package paint1;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JPanel;
/**
*
* @author Rehan Shakir
*/
public class PaintPanel extends JPanel {
private final ArrayList<Point> point = new ArrayList<>();
private Color color;
private final JButton red,blue,yellow,green,gray;
private int x = 0;
private int y = 0;
public PaintPanel()
{
setLayout(null);
red = new JButton(" Red ");
red.setBounds(0, 0, 80, 50);
red.setBackground(Color.red);
add(red);
blue = new JButton(" Blue ");
blue.setBounds(82,0 , 80, 50);
blue.setBackground(Color.BLUE);
add(blue);
yellow = new JButton("Yellow");
yellow.setBounds(163,0 , 80, 50);
yellow.setBackground(Color.yellow);
add(yellow);
green = new JButton(" Green");
green.setBounds(242,0 , 80, 50);
green.setBackground(Color.green);
add(green);
gray = new JButton(" Gray ");
gray.setBounds(322,0 , 80, 50);
gray.setBackground(Color.gray);
add(gray);
handler h = new handler();
red.addActionListener(h);
blue.addActionListener(h);
yellow.addActionListener(h);
green.addActionListener(h);
gray.addActionListener(h);
setBackground(Color.RED);
addMouseMotionListener(
new MouseMotionAdapter()
{
@Override
public void mouseDragged(MouseEvent e)
{
x = e.getX();
y = e.getY();
repaint();
}
}
);
}
private class handler implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if(s.equals(" Red "))
color = Color.RED;
else if(s.equals(" Blue "))
color = Color.blue;
else if(s.equals("Yellow"))
color = Color.yellow;
else if(s.equals(" Green"))
color = Color.green;
else if(s.equals(" Gray "))
color = Color.gray;
}
}
@Override
public void paintComponent(Graphics g)
{
g.setColor(color);
g.fillOval(x, y, 20, 5);
}
}
<<>> 在这里,我使用 setBackground() 方法来更改颜色,但它不起作用。
package paint1;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JLabel;
/**
*
* @author Rehan Shakir
*/
public class Paint1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
JFrame Jf = new JFrame("A Simple Paint Program");
PaintPanel f = new PaintPanel();
f.setBackground(Color.red); //To Change BACKGROUND COLOR
Jf.add(f,BorderLayout.CENTER);
Jf.add(new JLabel("Drag The Mouse to Draw"),BorderLayout.SOUTH);
Jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Jf.setBackground(Color.black);
Jf.setVisible(true);
Jf.setSize(800,600);
}
}
请提供解决方案,如何更改 JFrame 的背景颜色?我只想将 JFrame 的背景颜色从默认颜色变为白色。
解决方案
您忘记在paintComponent
.
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g); // add this line to consider background!!!
g.setColor(color);
g.fillOval(x, y, 20, 5);
}
重要提示:不要使用setBounds()
而是学习LayoutManager 概念。这将帮助您使您的 UI 独立于操作系统、显示分辨率和窗口大小调整。
推荐阅读
- azure-devops - 在 docker build 在 Azure Devops CI/CD 管道中注入 Vault Secrets
- php - 在 Laravel 中使用 echo 查看输出消息
- airflow - KubernetesExecutor 的气流:记录的 pid 与当前的 pid.airflow 不匹配。获取 AirflowException:任务收到 SIGTERM 信号
- php - 无法以 symfony 形式“App\Form\QcmType”自动装配服务:
- php - 如何不在 Laravel 8 中重复我的代码?
- c# - 使用自动 JWT 请求简化 Swagger 身份验证
- typescript - 为 d3 比例顺序函数定义类型的问题
- kubernetes - AKS:omsagent-win pod 一次又一次地重新启动
- qt - 在 Flickable 和 PinchArea 内居中图像
- android - Android Firebase auth Twitter 在身份验证后不会重定向