当我继承JFrame时,用setBackground(Color.red);不能改变背景色,一直是灰色的背景。继承Frame时,背景色可以改变,但是圆圈没有画出来。请问在使用JFrame和Frame时要注意什么区别,怎么选择是使用JFrame还是Frame?他们有多少函数使用时是不一样的?
import java.awt.*;
import java.awt.Graphics;
import java.awt.event.*;
import javax.swing.*;public class TankClient extends JFrame {
public void lauchFrame(){
this.setSize(800,600);
this.setTitle("TankWar");
this.setLocation(200,100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setBackground(Color.red);//不能改变背景色?
this.setVisible(true);
DrawPanel p = new DrawPanel();
add(p,BorderLayout.CENTER);
}
public static void main(String[] args) {
TankClient tc = new TankClient();
tc.lauchFrame();
}}
class DrawPanel extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Color c = g.getColor();
g.setColor(Color.RED);
g.fillOval(50, 50, 30, 30);
g.setColor(c);
}
}
import java.awt.*;
import java.awt.Graphics;
import java.awt.event.*;
import javax.swing.*;public class TankClient extends JFrame {
public void lauchFrame(){
this.setSize(800,600);
this.setTitle("TankWar");
this.setLocation(200,100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setBackground(Color.red);//不能改变背景色?
this.setVisible(true);
DrawPanel p = new DrawPanel();
add(p,BorderLayout.CENTER);
}
public static void main(String[] args) {
TankClient tc = new TankClient();
tc.lauchFrame();
}}
class DrawPanel extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Color c = g.getColor();
g.setColor(Color.RED);
g.fillOval(50, 50, 30, 30);
g.setColor(c);
}
}
import javax.swing.*;
public class book{
static JFrame aWindow = new JFrame("图书管理系统");
public static void main(String[] args){
aWindow.setSize(600,500);
aWindow.setLocation(300, 200);
//JFrame和Frame有轻微的不兼容,但具体解析这种情况我也不太清楚
//如果发现某个方法无法实现,你可以尝试2种方法
//第一如果这个方法是他的父类方法,你可以通过获取它父类的对象后进行调用
//第二重写(需要比较高的水平)
aWindow.getContentPane().setBackground(Color.BLACK);
aWindow.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
aWindow.setVisible(true);
}
}
aWindow.getContentPane().setBackground(Color.BLACK);
this.validate();
this.repaint();
让JFrame重画一次.至于说你的面板的颜色问题,我也是对JFrame和无语不管怎么的都设置不了颜色,最后想了想,其实不要颜色也比较的合理,因为JPanel本来就是一个透明的容器,如果要设置颜色当然是在JPanel中进行设置还有关于JFrame和Frame,我查看API上面是这样说的:
JFrame 类与 Frame 轻微不兼容。与其他所有 JFC/Swing 顶层容器一样,JFrame 包含一个 JRootPane 作为其惟一的子容器。根据规定,根窗格所提供的内容窗格应该包含 JFrame 所显示的所有非菜单组件。这不同于 AWT Frame。作为 add 的便捷方法及其变体,已经重写了 remove 和 setLayout,以在必要时将其转发到 contentPane。这意味着可以编写:
frame.add(child);
子级将被添加到 contentPane。内容窗格始终是非 null 的。试图将其设置为 null 会导致 JFrame 抛出异常。默认的内容窗格上会设置有 BorderLayout 管理器。有关添加、移除和设置 JFrame 的 LayoutManager 的详情,请参阅 RootPaneContainer。
与 Frame 不同,当用户试图关闭窗口时,JFrame 知道如何进行响应。用户关闭窗口时,默认的行为只是简单地隐藏 JFrame。要更改默认的行为,可调用方法 setDefaultCloseOperation(int)。要使 JFrame 的行为与 Frame 实例相同,请使用 setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE)。 也没有提到什么面板颜色为什么和Frame不一样,感觉也是一个没有影响的问题如果你硬是要给JFrame设置颜色,那你就重写他的paint()方法,在上面你填充一个矩形和你的JFrame一样大就能填充颜色了
我在我原来的程序里加了this.getContentPane().setBackground(Color.BLACK);背景色还是不变,但是去掉添加JPanel的语句,就能改变背景色了,应该就如yueguangkai001所说的因为JPanel本来就是一个透明的容器,如果要设置颜色当然是在JPanel中进行设置,我在DrawPanel类中添加了this.setBackground(Color.green);果然OK,呵呵。
但是关于this.setVisable(true)的位置我没发现有影响。
this.getContentPane().setBackground(Color.BLACK);
给分过年