[英]How I can change the background color of JFrame in this program?
I use the setBackground() method in the Driver Class to change the background color but it does not work.我使用驱动程序类中的 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);
}
}
<<>> Here, I use the setBackground() method to change the color but it does not work. <<>>在这里,我使用 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);
}
}
Please provide me the solution, how can I change the background color of my JFrame?请为我提供解决方案,如何更改 JFrame 的背景颜色? I just want to make the background color of JFrame from the default color to White color.
我只想将 JFrame 的背景颜色从默认颜色变为白色。
You've forget to call the parent method in paintComponent
.您忘记了在
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);
}
Important : Don't use setBounds()
but rather learn the LayoutManager concept .重要提示:不要使用
setBounds()
而是学习LayoutManager 概念。 This will help you to make your UI independed to OS, display resolution and window resizing.这将帮助您使您的 UI 独立于操作系统、显示分辨率和窗口大小调整。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.