如何将组件放置在 JLayeredPane 中现有组件的正下方?

发布于 2024-11-30 13:43:50 字数 1980 浏览 2 评论 0原文

我有一个 JTextField,在它的正下方我想显示一个放置在 JLayeredPane 中的 JLabel(稍后我将使用它进行自动建议)。

如何将 JLabel 放置在 JLayeredPane 中 JTextField 的正下方?

这是我的一些代码,当前结果如下面的屏幕截图所示:

public static void main(String[] args) {

    JTextField field = new JTextField();
    JLabel lbl = new JLabel("Hello");
    lbl.setBackground(Color.YELLOW);
    lbl.setOpaque(true);

    JLayeredPane layeredPane = new JLayeredPane();
    layeredPane.setLayout(new GridLayout(0,1));
    layeredPane.add(lbl, (Integer) (JLayeredPane.POPUP_LAYER - 10));
    layeredPane.setPreferredSize(field.getPreferredSize());

    JPanel panel = new JPanel(new BorderLayout());
    panel.add(field, BorderLayout.NORTH);
    panel.add(layeredPane, BorderLayout.SOUTH);

    JFrame frame = new JFrame();
    frame.add(panel);
    frame.setSize(200, 360);
    frame.setVisible(true);
}

在此处输入图像描述

第二次尝试:

public static void main(String[] args) {

    JTextField field = new JTextField();
    JLabel lbl = new JLabel("Hello");
    lbl.setBackground(Color.YELLOW);
    lbl.setOpaque(true);
    lbl.setBounds(field.getBounds().x, field.getBounds().y, 
        field.getBounds().width, field.getBounds().height);

    JPanel popPanel = new JPanel(new BorderLayout());
    popPanel.add(lbl, BorderLayout.NORTH);
    popPanel.setLocation(field.getLocation().x+10, field.getLocation().y+20);
    popPanel.setPreferredSize(field.getPreferredSize());

    JFrame frame = new JFrame();

    JLayeredPane layeredPane = frame.getRootPane().getLayeredPane();
    layeredPane.setLayout(new GridLayout(0,1));
    layeredPane.add(popPanel, (Integer) (JLayeredPane.POPUP_LAYER - 10));

    JPanel panel = new JPanel(new BorderLayout());
    panel.add(field, BorderLayout.NORTH);

    frame.add(panel);
    frame.setSize(200, 360);
    frame.setVisible(true);
}

在此处输入图像描述

I have a JTextField, and right below it I want to show a JLabel placed in a JLayeredPane (I will use it for autosuggestions later on).

How can I place my JLabel in JLayeredPane right below the JTextField?

Here is some code I have, and the current result shown in the screenshot below:

public static void main(String[] args) {

    JTextField field = new JTextField();
    JLabel lbl = new JLabel("Hello");
    lbl.setBackground(Color.YELLOW);
    lbl.setOpaque(true);

    JLayeredPane layeredPane = new JLayeredPane();
    layeredPane.setLayout(new GridLayout(0,1));
    layeredPane.add(lbl, (Integer) (JLayeredPane.POPUP_LAYER - 10));
    layeredPane.setPreferredSize(field.getPreferredSize());

    JPanel panel = new JPanel(new BorderLayout());
    panel.add(field, BorderLayout.NORTH);
    panel.add(layeredPane, BorderLayout.SOUTH);

    JFrame frame = new JFrame();
    frame.add(panel);
    frame.setSize(200, 360);
    frame.setVisible(true);
}

enter image description here

Second try:

public static void main(String[] args) {

    JTextField field = new JTextField();
    JLabel lbl = new JLabel("Hello");
    lbl.setBackground(Color.YELLOW);
    lbl.setOpaque(true);
    lbl.setBounds(field.getBounds().x, field.getBounds().y, 
        field.getBounds().width, field.getBounds().height);

    JPanel popPanel = new JPanel(new BorderLayout());
    popPanel.add(lbl, BorderLayout.NORTH);
    popPanel.setLocation(field.getLocation().x+10, field.getLocation().y+20);
    popPanel.setPreferredSize(field.getPreferredSize());

    JFrame frame = new JFrame();

    JLayeredPane layeredPane = frame.getRootPane().getLayeredPane();
    layeredPane.setLayout(new GridLayout(0,1));
    layeredPane.add(popPanel, (Integer) (JLayeredPane.POPUP_LAYER - 10));

    JPanel panel = new JPanel(new BorderLayout());
    panel.add(field, BorderLayout.NORTH);

    frame.add(panel);
    frame.setSize(200, 360);
    frame.setVisible(true);
}

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

腻橙味 2024-12-07 13:43:50

将 LayeredPane 添加到“CENTER”,而不是“SOUTH”。

然而,您对分层窗格的理解似乎有点混乱。当您希望多个组件彼此叠放(堆叠?)时,可以使用分层窗格。您仍在使用二维分层窗格,这是不必要的。您只需使用面板即可实现此目的。

如果您想弹出建议列表,那么您应该使用 JPopupMenu 并将其放置在文本字段下方。阅读 Swing 教程中有关调出弹出菜单

Add the layeredPane to the "CENTER", not the SOUTH.

However, your understanding a layed pane seems to be a little confused. You use a layered pane when you want multiple components to be displayed on top (stacked?) of one another. You are still using the layered pane in 2 dimensions which is unnecessary. YOu can just use a panel for this.

If you want to popup a list of suggestions then you should just use a JPopupMenu and position it below the text field. Read the section from the Swing tutorial on Bringing up Popup Menus.

世界和平 2024-12-07 13:43:50

首先,我认为您不应该为此使用 JLayeredPane,而只是一个永久标签。

如果您确实使用分层窗格,则必须计算文本字段的结束位置 (y = field.getY() + field.getHeight()) 并将 JPanel 设置为“panel.setLocation” (0, y)' 在 JLayeredPane 内(前提是 JLayeredPane 与底层 JFrame 具有相同的起始位置)。您可以等效地将 JLayeredPane 放置在 (0, y) 处,并将标签放置在该分层窗格内的 (0, 0) 处。

您必须确保每次调整组件大小时都执行此操作。

First of all, I don't think you should use a JLayeredPane for that, but just a permanent label.

If you do use a layered pane, you'll have to compute where the text field ends (y = field.getY() + field.getHeight()) and set your JPanel at 'panel.setLocation(0, y)' inside the JLayeredPane (provided the JLayeredPane has the same starting position as the underlying JFrame). You could equivalently position the JLayeredPane at (0, y) and put the label at (0, 0) within that layered pane.

You have to make sure this is done every time the components are resized.

满意归宿 2024-12-07 13:43:50

为什么不使用 AutoComplete ComboBox / JTextField 如果你不这样做想要显示JComboBox,那么有AutoCompleted JTextField,并且为了以某种方式减少自动建议,最好寻找未修饰的JDialog/Window,在 JScrollPane 中使用 JTable 和一个 TableColum,不带 TableHeaded,仅使用普通的 RowSorter,非常简单工作

why not using AutoComplete ComboBox / JTextField and if you don't want to display JComboBox, then there is AutoCompleted JTextField, and for somehow reduced autosuggestions, would be better look for undecorated JDialog/Window with JTable with one TableColum and without TableHeaded in the JScrollPane, just with plain RowSorter, very simle job

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文