import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JavaWuZiQiFrame extends JFrame {

Image backScreen = null;

Thread t = new Thread(new Runner());
//背景图片
ImageIcon bg = new ImageIcon("C:\\Users\\Forever\\Desktop\\javaProgram\\javaWuZiQi\\Background.jpg");


JButton bStart = new JButton("开始游戏");
JButton bExit = new JButton("退出");
JButton bAbout = new JButton("关于");
JavaWuZiQiFrame() {

super("五子棋");
setLayout(null);

bStart.setBounds(650,150,100,40);
bAbout.setBounds(650,200,100,40);
bExit.setBounds(650,500,100,40);
add(bStart);
add(bAbout);
add(bExit);
setVisible(true);
this.getContentPane().setBackground(Color.red);
setResizable(false);
setBounds(50,50,bg.getIconWidth() + 200,bg.getIconHeight());
/*
bAbout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
t.start();
}
});
*/
bExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//t.start();
}
public void paint(Graphics g) {
Image im = Toolkit.getDefaultToolkit().getImage("C:\\Users\\Forever\\Desktop\\javaProgram\\javaWuZiQi\\Background.jpg");
g.setColor(new Color(249,214,91));
//g.fillRect(0,0,bg.getIconWidth() + 200,bg.getIconHeight());
g.drawImage(im,0,0,null);
g.setColor(Color.red);

}

public void update(Graphics g) {

if(backScreen == null) {
//创建一幅用于双缓冲的、可在屏幕外绘制的图像
backScreen = this.createImage(bg.getIconWidth() + 200,bg.getIconHeight());
}
Graphics gBackScreen = backScreen.getGraphics();
Color c = gBackScreen.getColor();
gBackScreen.setColor(Color.BLACK);
gBackScreen.fillRect(0,0,bg.getIconWidth() + 200,bg.getIconHeight());
paint(gBackScreen);
g.drawImage(backScreen,0,0,null);
gBackScreen.setColor(c);
}
private class Runner implements Runnable {
public void run() {
while(true) {
repaint();
try {
Thread.sleep(200);

} catch(Exception e) {

}
}
}
}

}
启动程序之后,按钮没有自动出现,只有当鼠标指在按钮的位置时才出现,为什么?,还有,我如何在背景图片之后在设置一个背景色?JButton

解决方案 »

  1.   

    1. 继承JPanel 重写 paintComponent。
    2. Toolkit的getImage方法是异步的。使用ImageIO的read方法。
    3. 使用javax.swing.Timer定时重绘。
      

  2.   

    你要分清楚先画谁,他是先画控件再调用paint方法,这样那就会出现控件看不到,只有你把鼠标放上去才能显示他,你试试从最小化到能看见,他也是不会显示的。楼上说的方法就能解决了,他的顺序是先调用paintComponent。画控件。设置背景颜色我觉得你还是得通过画笔去画,这样更靠谱点
      

  3.   

    我在重写protected void paintComponent(Graphics g)的时候发现这个方法不能被自动调用,怎么解决protected void paintComponent(Graphics g) {

    Color c = g.getColor();
    g.setColor(Color.red);
    g.fillOval(100,100,200,100);
    g.setColor(c);
    }
    像这段代码没法像paint(Graphics g)那样画出Oval,我以前用的是awt,重写的是paint(Graphics g)方法,对于paintComponent,我实在是不太懂
      

  4.   

    这是完整代码import java.awt.*;
    import javax.swing.*;public class ExtendsJPanel extends JPanel {
    Frame f = new Frame("Test");
    JButton jb = new JButton("Start");
    JPanel jp = new JPanel();

    public void launchFrame() {
    f.setLayout(null);
    f.setVisible(true);
    f.setBounds(100,100,300,300);
    f.setBackground(Color.yellow);
    f.add(this);
    setVisible(true);
    setBounds(100,100,100,100);
    //setBackground(Color.red);
    jb.setBounds(100,100,100,20);
    add(jb);
    }


    protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D)g;
    Color c = g.getColor();
    g.setColor(Color.black);
    g.fillOval(100,100,200,100);
    g.setColor(c);
    }

    public static void main(String[] args) {

    ExtendsJPanel ej = new ExtendsJPanel();
    ej.launchFrame();

    }
    }
      

  5.   

    我百度了一下,发现需要设置一个具体的Layout而不是null才能画出paintComponent中的内容,求解释
      

  6.   

    自动调用的 不要你调用,调用repaint也会执行该方
      

  7.   

    repaint方法会调用paintComponent方法没错,但是为什么paintComponent中的内容显示不出来,比如我就想画一个圆fillOval都画不出来
      

  8.   

    调用drawImage方法都不行,画不出来,我设置的Layout为null,我就发现了一个问题,只有当我设置一个具体的Layout的时候,比如GridLayout,drawImage才画的出来,但是这不是我想要的结果,我不需要布局管理器,我就想自己设计界面。怎么做
      

  9.   

    我的想法很简单,不用布局管理器,在paintComponent方法中画出自己想画的东西