我在Driver Class中使用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的背景色从默认颜色更改为白色。
答案 0 :(得分:1)
您忘记了在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独立于操作系统,显示分辨率和调整窗口大小。