想要在 JTextArea 上显示 JList

发布于 2024-10-31 06:01:55 字数 303 浏览 3 评论 0原文

我想在 JTextArea 上显示 JList,但它显示在 JTextArea 后面。我附上了两张图片来通过图像来描述我的问题。在运行时我们如何在 JTextArea 上设置 JList?

JTextArea 后面的 JList:

在此处输入图像描述

JTextArea 上的 JList:

在此处输入图像描述

I want to disply JList over the JTextArea but its showing it behind the JTextArea. I have attached two images to describe my problem through images.At runtime how can we set JList over the JTextArea?

JList behind JTextArea:

enter image description here

JList over JTextArea:

enter image description here

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

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

发布评论

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

评论(2

地狱即天堂 2024-11-07 06:01:55

bsm:在这种情况下,您不应该使用 JList,而应该使用 JComboBoxes,它的下拉列表可以在 JTextArea 上正确显示。例如,

import java.awt.BorderLayout;
import javax.swing.*;

public class JComboAndJTextArea extends JPanel {

   private static final String[] ITEMS1 = {"one", "two", "three", "four", "five"};
   private static final String[] ITEMS2 = {"Monday", "Tuesday", 
      "Wednesday", "Thursday", "Friday"};

   public JComboAndJTextArea() {
      JPanel northPanel = new JPanel();
      northPanel.add(new JCheckBox("Reminder"));
      northPanel.add(new JComboBox(ITEMS1));
      northPanel.add(new JComboBox(ITEMS2));

      setLayout(new BorderLayout());
      add(northPanel, BorderLayout.NORTH);
      add(new JScrollPane(new JTextArea(8, 30)), BorderLayout.CENTER);

   }

   private static void createAndShowUI() {
      JFrame frame = new JFrame("JComboAndJTextArea");
      frame.getContentPane().add(new JComboAndJTextArea());
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}

我还重新建议您暂时将 NetBeans 代码生成放在一边,因为我真诚地相信,对于许多人来说,这会阻碍他们学习如何在 Swing 中进行编码的能力。

bsm: you should not use JLists for this situation but JComboBoxes which will have drop down lists that display correctly over the JTextArea. For e.g.,

import java.awt.BorderLayout;
import javax.swing.*;

public class JComboAndJTextArea extends JPanel {

   private static final String[] ITEMS1 = {"one", "two", "three", "four", "five"};
   private static final String[] ITEMS2 = {"Monday", "Tuesday", 
      "Wednesday", "Thursday", "Friday"};

   public JComboAndJTextArea() {
      JPanel northPanel = new JPanel();
      northPanel.add(new JCheckBox("Reminder"));
      northPanel.add(new JComboBox(ITEMS1));
      northPanel.add(new JComboBox(ITEMS2));

      setLayout(new BorderLayout());
      add(northPanel, BorderLayout.NORTH);
      add(new JScrollPane(new JTextArea(8, 30)), BorderLayout.CENTER);

   }

   private static void createAndShowUI() {
      JFrame frame = new JFrame("JComboAndJTextArea");
      frame.getContentPane().add(new JComboAndJTextArea());
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}

I also re-recommend that you put your NetBeans code generation to the side for the time being as I honestly believe that for many it hinders their ability to learn how to code in Swing.

战皆罪 2024-11-07 06:01:55

我认为默认行为是在其他组件上显示组合内容,因此您可以这样。我目前想到的唯一建议是使用分层窗格。

您可以检查带有选项的组合框部分添加到哪一层。然后将该列表添加到上面的列表中。

关于 LayeredPane 的教程 http://download.oracle.com/javase/tutorial/ uiswing/components/layeredpane.html

从 RootPane 的描述中我认为组合框的选项必须显示在弹出层上 http://download.oracle.com/javase/tutorial/uiswing/components/rootpane.html

祝你好运,Boro。

I would think that default behaviour is to show combo content over other components, thus you have it this way. The only suggestion that I would think of at the moment is to use layered pane.

You could check to which layer the part of combo box with options is added. Then add the list to one above.

Tutorial about LayeredPane http://download.oracle.com/javase/tutorial/uiswing/components/layeredpane.html

From this description of RootPane I think the options of combobox must be shown on popup layer http://download.oracle.com/javase/tutorial/uiswing/components/rootpane.html

Good luck, Boro.

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