java 如何将 JLabel 放在标题栏下方的右上角?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5850949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to place JLabel on top right corner just below the title bar?
提问by Sachin Doiphode
Hello I read about Layouts but didn't get which one to use for my application. I want to add image to JPanel and place JLabel on op right corner just below the title bar.
您好,我阅读了有关 Layouts 的内容,但没有找到适合我的应用程序的布局。我想将图像添加到 JPanel 并将 JLabel 放在标题栏下方的右上角。
I have written code for that but it is not working. JLabel is displayed at Centre.
我已经为此编写了代码,但它不起作用。JLabel 显示在中心。
My code is
我的代码是
// Below line adds image to Jpanel
panel = new ImagePanel(backgroundImage);
// I want to add below label to Jpanel
JLabel jdesignNo=new JLabel(designNo);
jdesignNo.setFont((new java.awt.Font("Times New Roman", 1, 30)));
jdesignNo.setBounds(900, 100, 50, 30);
jdesignNo.setBackground(Color.GREEN);
panel.add(jdesignNo);
frame.getContentPane().add(panel);
frame.setVisible(true);
In above code i have set required location by setBound but it does not work.
在上面的代码中,我通过 setBound 设置了所需的位置,但它不起作用。
How to solve this problem ?
如何解决这个问题呢 ?
Thanks !!!
谢谢 !!!
回答by camickr
In above code i have set required location by setBound but it does not work.
在上面的代码中,我通过 setBound 设置了所需的位置,但它不起作用。
There is no need to do that. You should not be using setBounds(...). Use layout managers:
没有必要这样做。您不应该使用 setBounds(...)。使用布局管理器:
JLabel label = new JLabel("Some Text");
label.setHorizontalAlignment(JLabel.RIGHT);
frame.add(label, BorderLayout.NORTH);
回答by MeBigFatGuy
Since you have a dominating feature in your layout (the image) you probably want to use a BorderLayout as your main layout.
由于您的布局(图像)中有一个主要功能,您可能希望使用 BorderLayout 作为主要布局。
frame.getContentPane().setLayout(new BorderLayout(4, 4));
then add the image to the BorderLayout.CENTER
然后将图像添加到 BorderLayout.CENTER
next the JLabel wants to go in the BorderLayout.NORTH
接下来 JLabel 想要进入 BorderLayout.NORTH
Now, if you do that, it won't go to the far right, so create a JPanel for the north, add the JLabel to the panel, and place the panel in the north.
现在,如果你这样做,它不会去最右边,所以为北方创建一个 JPanel,将 JLabel 添加到面板,并将面板放置在北方。
Now you need a layout for the north panel. A BoxLayout will work
现在您需要北面板的布局。BoxLayout 将起作用
northPanel.add(Box.createHorizontalGlue());
northPanel.add(label);
回答by Nicolas_75
A possibility with a GridBagLayout:
使用 GridBagLayout 的可能性:
import java.awt.*;
import javax.swing.*;
public class MyPanel extends JPanel {
public MyPanel() {
setLayout(new GridBagLayout());
add(new JLabel("TOP RIGHT"), new GridBagConstraints(
0, // gridx
0, // gridy
1, // gridwidth
1, // gridheight
1, // weightx
1, // weighty
GridBagConstraints.NORTHEAST, // anchor <------------
GridBagConstraints.NONE, // fill
new Insets(0, // inset top
0, // inset left
0, // inset bottom
0), // inset right
0, // ipadx
0)); // ipady
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setResizable(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new MyPanel());
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Nicolas
尼古拉斯
回答by Bala R
Make sure you have
确保你有
panel.setLayout(null);
if you wish to position the components yourself.
如果您想自己定位组件。
But identifying and using a layout manager that would fit your needs would make things easier as the complexity of your application grows.
但是,随着应用程序复杂性的增加,确定和使用适合您需求的布局管理器会使事情变得更容易。