Java 为什么我的背景不会改变颜色?
我在课堂上做这个实验,当我尝试改变背景颜色时,它会保持默认的白色。有人能解释一下我的编程哪里出错了吗Java 为什么我的背景不会改变颜色?,java,swing,Java,Swing,我在课堂上做这个实验,当我尝试改变背景颜色时,它会保持默认的白色。有人能解释一下我的编程哪里出错了吗 import javax.swing.*; import java.awt.*; public class DemoPoly extends JFrame { // constructor public DemoPoly() { // defines Frame characteristics int size = 300; setSize(size,size); se
import javax.swing.*;
import java.awt.*;
public class DemoPoly extends JFrame {
// constructor
public DemoPoly() {
// defines Frame characteristics
int size = 300;
setSize(size,size);
setTitle("a random window");
getContentPane().setBackground(Color.red);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main (String[] args){
// instantiates a JFrame
// exits on close (opional)
JFrame object = new DemoPoly();
}
public void paint (Graphics g){
// This provides the Graphics object g, where
// you are going to use you graphics primitive
// to paint on the content pane of the frame.
int[] arr = {0,100,100,0};
int[] yarr = {0,0,100,100};
Square object = new Square(arr,yarr,Color.red);
AbstractPolygon randSquare = new Square(arr, yarr, Color.red);
}
很抱歉,我不明白你的问题。但下面是将背景改为红色的代码
public class DemoPoly extends JFrame {
public DemoPoly() {
// defines Frame characteristics
int size = 300;
setSize(size, size);
setTitle("a random window");
//change background here
getContentPane().setBackground(Color.red);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
// instantiates a JFrame
// exits on close (opional)
JFrame object = new DemoPoly();
}
}
你的代码很好。可能在绘制方法中使用@override 我发现您的代码中有几个问题:
JFrame
就像说你的类是一个JFrame
,JFrame
是一个刚性容器,而不是基于JPanel
s创建GUI。有关更多信息,请参阅paint(…)
方法上的super.paint(g)
调用,您正在破坏绘制链。将GUI更改为扩展JPanel
而不是JFrame
时,应使用paintComponent(…)
方法。拿这个绘制(…)
方法上添加@Override
符号main()
方法来解决此问题:
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
//Your constructor here
}
});
}
JFrame
大小,而是覆盖getPreferredSize()
方法并调用pack()
。看见普遍的共识是肯定的您的问题可以通过添加
super.paint(g);
在绘制(…)
方法上:
@Override
public void paint(Graphics g) {
super.paint(g); //Never remove this
//Your code goes here
}
考虑到以上所有建议,您的代码现在应该如下所示:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
public class DemoPoly {
private JFrame frame;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new DemoPoly().createAndShowGui();
}
});
}
public void createAndShowGui() {
frame = new JFrame(getClass().getSimpleName());
CustomPanel cp = new CustomPanel();
cp.setBackground(Color.RED);
frame.add(cp);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
class CustomPanel extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
}
这会产生这个输出(与您当前代码的输出相同,但更好,因为它使您能够更好地控制组件)
本网站对Java图形或屏幕对象的解释过于宽泛。