将JLabel放在JPanel中的Graphics 2D Rectangle前面 [英] Put JLabel in front of Graphics 2D Rectangle in JPanel
问题描述
是否可以将JLabel放置在矩形的前面?目前,JLabel位于矩形下方.
Is there any way to put the JLabel in front of the rectangle? Currently, the JLabel is underneath the rectangle.
这是我的相关代码:
public class MainMenu_South extends JPanel {
MainMenu_BlueJay t;
JLabel start=new JLabel("START");
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.blue);
g2d.drawRect(0, 30, 900, 30);
g2d.fillRect(0, 30, 900, 30);
}
public MainMenu_South(MainMenu_BlueJay t){
setLayout(null);
this.t=t;
setBackground(Color.white);
start.setForeground(Color.red);
start.setFont(new Font("Arial", Font.BOLD, 16));
add(start);
start.setBounds(100, 10, 100, 50);
start.addMouseListener(ml);
}
MouseListener ml=new MouseListener() {
@Override
public void mouseEntered(MouseEvent e) {//hover
start.setForeground(Color.white);
}
@Override
public void mouseExited(MouseEvent e) {
start.setForeground(Color.red);
}
};
}
推荐答案
-
为获得更好的帮助,请尽快发布 MCVE
将paintComponent()
用于Swing JComponents,而不是paint()
use paintComponent()
for Swing JComponents instead of paint()
没有理由使用NullLayout
,LayoutManager
,
覆盖getPreferredSize
到JPanel
,然后JFrame.pack()
将在屏幕上生成预期的Dimension
override getPreferredSize
for JPanel
, then JFrame.pack()
will generate expected Dimension
on the screen
JLabel
是透明的,如果要在Jlabel
JLabel
is transparent, you have to setOpaque
if you to want to dispaly Color
as Backgroung
in Jlabel
您必须将repaint(last code line)
用于MouseEvents
,将Color
用于JLabel
,覆盖MouseAdapter
you have to use repaint(last code line)
for MouseEvents
and Color
for JLabel
, override MouseAdapter
不遗漏,不知道您是否需要使用透明性或半透明性,或删除JLabel
miss any, no idea if you would need use transparency or tranclucency, or remove the JLabel
这篇关于将JLabel放在JPanel中的Graphics 2D Rectangle前面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!