gpt4 book ai didi

java - 如何在此程序中更改 JFrame 的背景颜色?

转载 作者:行者123 更新时间:2023-12-02 09:14:07 24 4
gpt4 key购买 nike

我使用驱动程序类中的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 concept 。这将帮助您使 UI 独立于操作系统、显示分辨率和窗口大小调整。

关于java - 如何在此程序中更改 JFrame 的背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59142886/

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