Here is my code:
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Test implements Runnable {
public static void main(String args[]) {
Test frame = new Test();
new Thread(frame).start();
}
int width = 800;
int height = 600;
MyCanvas canvas;
JFrame f = new JFrame();
/**
* Create the frame
*/
public Test() {
Skin.initSkin("res/skin/");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(null);
f.setSize(width, height);
guiPanel = new JPanel();
guiPanel.setLayout(null);
guiPanel.setOpaque(false);
guiPanel.setBounds(0, 0, width, height);
f.add(guiPanel);
addButton(guiPanel);
canvas = new MyCanvas();
canvas.setBounds(0, 30, width, height);
f.add(canvas);
f.setVisible(true);
}
private void addButton(Container comp) {
JButton b = new JButton("my button");
b.setName("GameButton2");
b.setBounds(100, 100, 100, 100);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
f.add(canvas);
}
});
comp.add(b);
b = new JButton("my button");
b.setBounds(200, 100, 100, 100);
comp.add(b);
b = new JButton("my button");
b.setBounds(200, 200, 100, 100);
comp.add(b);
b = new JButton("my button");
b.setBounds(100, 200, 100, 100);
comp.add(b);
JTextField jtf = new JTextField();
jtf.setBounds(0, 0, 300, 50);
jtf.setName("PetTextfield");
comp.add(jtf);
}
public void run() {
while (true) {
render();
try {
Thread.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
int frame = 0;
private JPanel guiPanel;
class MyCanvas extends Canvas {
@Override
public void paint(Graphics g) {
frame += 5;
if (frame > 255)
frame = 0;
g.setColor(new Color(frame));
g.fillRect(0, 0, getWidth(), getHeight());
}
}
public void render() {
Graphics g = canvas.getGraphics();
if (null == g)
return;
if (canvas.isVisible())
canvas.paint(g);
}
}
when I remove Skin.initSkin(“res/skin/”) , canvas render under all buttons, everything goes well, but when I add my custom look and feel skin, canvas render will cover all buttons. why? it’s that look and feel problem? my jre version is “jre6 update 12”