如何将组件放置在 JLayeredPane 中现有组件的正下方?
我有一个 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);
}
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将 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.
首先,我认为您不应该为此使用 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.
为什么不使用 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 isAutoCompleted JTextField
, and for somehow reduced autosuggestions, would be better look for undecoratedJDialog/Window
withJTable with one TableColum and without TableHeaded
in theJScrollPane
, just with plainRowSorter
, very simle job