[英]How do I make a JLabel always stay above a JButton?
So I have a gui where the layout is set to Null, and I put JLabels ontop of JButtons since the JButtons have icons and adding text to the button makes the button look distorted, so instead I am using JLabels ontop of the JButtons. 所以我有一个gui,其中的布局设置为Null,并且我将JLabels放在JButton的顶部,因为JButton带有图标,并且在按钮上添加文本使按钮看起来失真,所以我在JButton的顶部使用JLabels。 Every time the mouse goes over the JButton when I test it, the JLabel disappears.
每当我测试它时,鼠标经过JButton时,JLabel都会消失。 How can I fix that so JLabels always appear above JButtons.
我该如何解决,以使JLabel始终出现在JButtons上方。
Edit: it distorts the button so the icon is taking up most the space and the button label is cut off by the opposite edge of the button. 编辑:它会使按钮变形,因此图标占据了大部分空间,并且按钮的另一边被按钮标签切掉了。
如果您正在使用JFrame(我想一定要这样做),则可以将标签添加到位于内容窗格顶部的JLayered窗格中。
There is almost no cases when you will need to use null layout. 在几乎没有情况下,您将需要使用空布局。 You just need to do a little practice with the LayoutManagers
您只需要对LayoutManager做一些练习
You can do the thing you wish to do with a JLayeredPane . 您可以使用JLayeredPane完成您想做的事情。 Like this:
像这样:
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.add(jLabelOnJButton());
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private static JComponent jLabelOnJButton(){
JLayeredPane layers = new JLayeredPane();
JLabel label = new JLabel("label");
JButton button = new JButton("button");
label.setBounds(40, 20, 100, 50);
button.setBounds(20, 20, 150, 75);
layers.add(label, new Integer(2));
layers.add(button, new Integer(1));
return layers;
}
}
This is not a good solution I think. 我认为这不是一个好的解决方案。 Only do it if you have no better solution.
仅当您没有更好的解决方案时才这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.