我正在尝试在JFrame上绘制一个矩形,其中网格布局填充了JPanels。但是,矩形是在JPanels下绘制的,所以我可以看到JPanels遇到的颜色。有没有办法指定一个带来前面的东西?
这是我的代码:
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.*;
import javax.swing.*;
public class TestComponentAt extends JPanel {
private static final int ROW_COUNT = 8;
private static final int W = 60;
private static final int H = W;
private static final Dimension PREF_SIZE = new Dimension(W, H);
private JFrame frame = new JFrame();
public TestComponentAt() {
setLayout(new GridLayout(ROW_COUNT, ROW_COUNT, 1, 1));
setBackground(Color.black);
for (int i = 0; i < ROW_COUNT * ROW_COUNT; i++) {
JPanel panel = new JPanel();
String name = String.format("[%d, %d]",
i / ROW_COUNT, i % ROW_COUNT);
panel.setName(name);
if ((i/ROW_COUNT) % 2 == (i%ROW_COUNT) % 2) {
panel.setBackground(Color.black);
}
panel.setPreferredSize(PREF_SIZE);
int row = i/ROW_COUNT;
add(panel);
if (panel.getBackground() == Color.black && (row <= 2 || row >= 5)){
repaint();
}
}
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
JPanel panel = (JPanel) getComponentAt(e.getPoint());
if (panel == null || panel == TestComponentAt.this) {
return;
}
if (panel != null) {
panel.removeAll();
panel.revalidate();
panel.repaint();
}
panel.add(new JLabel(panel.getName()));
panel.revalidate();
panel.repaint();
}
});
}
public void paintComponent(Graphics g){
System.out.println("this has been called");
super.paintComponent(g);
g.setColor(Color.pink);
g.fillOval(100, 100, 100, 100);
}
private void createAndShowGui() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(480, 480);
frame.getContentPane().add(new TestComponentAt());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
TestComponentAt obj = new TestComponentAt();
obj.createAndShowGui();
}
}
答案 0 :(得分:2)
可以使用JLayer绘制JPanel及其组件。例如:
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.LayerUI;
public class TestComponentAt extends JPanel {
private static final int ROW_COUNT = 8;
private static final int W = 60;
private static final int H = W;
private static final Dimension PREF_SIZE = new Dimension(W, H);
private static final Color TRANSLUCENT_PINK = new Color(255, 175, 175, 200);
private JFrame frame = new JFrame();
public TestComponentAt() {
setLayout(new GridLayout(ROW_COUNT, ROW_COUNT, 1, 1));
setBackground(Color.black);
for (int i = 0; i < ROW_COUNT * ROW_COUNT; i++) {
JPanel panel = new JPanel();
String name = String.format("[%d, %d]", i / ROW_COUNT, i % ROW_COUNT);
panel.setName(name);
if ((i / ROW_COUNT) % 2 == (i % ROW_COUNT) % 2) {
panel.setBackground(Color.black);
}
panel.setPreferredSize(PREF_SIZE);
int row = i / ROW_COUNT;
add(panel);
if (panel.getBackground() == Color.black && (row <= 2 || row >= 5)) {
frame.repaint();
}
}
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
JPanel panel = (JPanel) getComponentAt(e.getPoint());
if (panel == null || panel == TestComponentAt.this) {
return;
}
if (panel != null) {
panel.removeAll();
panel.revalidate();
panel.repaint();
}
panel.add(new JLabel(panel.getName()));
panel.revalidate();
panel.repaint();
}
});
}
private class MyLayer extends LayerUI<JComponent> {
@Override
public void paint(Graphics g, JComponent c) {
super.paint(g, c);
g.setColor(TRANSLUCENT_PINK);
g.fillOval(100, 100, 100, 100);
}
}
private void createAndShowGui() {
TestComponentAt testComponentAt = new TestComponentAt();
JLayer<JComponent> myLayer = new JLayer<JComponent>(testComponentAt, new MyLayer());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(480, 480);
frame.getContentPane().add(myLayer);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
TestComponentAt obj = new TestComponentAt();
obj.createAndShowGui();
}
}