简体   繁体   English

Java swing Jlist创建

[英]Java swing Jlist creation

Hello I want to create a Jlist in java swing but I cannot understand where I can show it. 您好我想在java swing中创建一个Jlist,但我无法理解我可以在哪里展示它。 All the tutorials I've seen so far create the list in a new window which is not what I want. 到目前为止我看到的所有教程都在一个新窗口中创建了这个列表,这不是我想要的。 I want to use a tool or something to give specific coordinates and site at the frame I am currently in. I am new to java swing so I could use some help. 我想使用工具或其他东西在我当前所在的框架中给出特定的坐标和站点。我是java swing的新手,所以我可以使用一些帮助。 I use windows builder pro as a plug-in for eclipse. 我使用windows builder pro作为eclipse的插件。 Is there any tool I could use to "draw" where the list will be presented? 是否有任何工具可用于“绘制”列表将被呈现?

Thx in advance Thx提前

I tried this but it won't work. 我试过这个但是不行。

JPanel panel = new JPanel();
panel.setBounds(36, 109, 619, 202);
MainFrame.getContentPane().add(panel);

final JScrollPane scrollPane = new JScrollPane();
panel.add(scrollPane);


/*on a click listener of a button*/
    scrollPane.setLayout( new BorderLayout() );
                getContentPane().add( scrollPane );

                // Create some items to add to the list
                String  listData[] =
                {
                    "Item 1",
                    "Item 2",
                    "Item 3",
                    "Item 4"
                };
                JList listbox = new JList( listData );
                scrollPane.add( listbox, BorderLayout.CENTER );

but when I run the program I get an error like this: 但是当我运行程序时,我收到如下错误:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: layout of JScrollPane must be a ScrollPaneLayout at javax.swing.JScrollPane.setLayout(Unknown Source) at Main$3.mouseClicked(Main.java:122) at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Un 线程“AWT-EventQueue-0”中的异常java.lang.ClassCastException:JScrollPane的布局必须是java的Main $ 3.mouseClicked(Main.java:122)的javax.swing.JScrollPane.setLayout(Unknown Source)的ScrollPaneLayout。 awt.AWTEventMulticaster.mouseClicked(未知来源)位于java.awt的java.awt.Component.processEvent(未知来源)的javax.swing.JComponent.processMouseEvent(未知来源)的java.awt.Component.processMouseEvent(未知来源)。 java.awt.LightweightDispatcher上java.awt.Component.dispatchEvent(未知来源)的java.awt.Component上的java.awt.Component.dispatchEventImpl(未知来源)中的Container.processEvent(未知来源)。 java.awt.Window.dispatchEventImpl上的java.awt.LightweightDispatcherEispIvent(未知来源)java.awt.Window.dispatchEventImpl上的java.awt.LightweightDispatcher.dispatchEvent(未知来源)中的retargetMouseEvent(未知来源)来自java.awt.Component.dispatchEvent的Unknown Source(Un known Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$200(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatch 来自java.awt.EventQueue的java.awt.EventQueue.dispatchEventImpl(未知来源)的java.awt.EventQueue.access $(未知来源)java.awt.EventQueue $ 3.run(未知来源)java.awt.EventQueue $ 3.run java.security.Protevilege(Java)上java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知来源)java.awt.EventQueue $ 4的java.security.Protevilege(未知来源)处于(未知来源)$ 1.doIntersectionPrivilege(未知来源)。在Java.awt.EventQueue上的java.security.AccessController.doPrivileged(本地方法)的java.awt.EventQueue $ 4.run(未知来源)上运行(未知来源)java.awt.EventQueue上的java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知来源)。 java.awt.EventDispatchThread.FumpEvents(java.awt.EventDispatchThread.pumpEventsForFilter(未知来源)java.awt.EventDispatchThread.pumpEventsForHierarchy(未知来源)java.awt.EventDispatchThread.pumpEvents上的java.awt.EventDispatchThread.pumpOneEventForFilters(未知来源)中的dispatchEvent(未知来源) java.awt.EventDispatch上的Unknown Source) Thread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) java.awt.EventDispatchThread.run中的Thread.pumpEvents(未知来源)(未知来源)

What you want to do is essentially place a component (here a JList that is held by a JScrollPane -- and trust me, you want it inside of a JScrollPane) into a Container (usually a JPanel), and there are three general ways to do this: 你想要做的实际上是放置一个组件(这里是一个由JScrollPane持有的JList - 相信我,你想要它在JScrollPane中)到一个Container(通常是一个JPanel),有三种通用的方法做这个:

  1. Give the JPanel a null layout, and then set the size and position of any and all components that you add to it, including the JScrollPane-holding JPanel via the setBounds(...) method, or... 给JPanel一个null布局,然后设置你添加到它的任何和所有组件的大小和位置,包括通过setBounds(...)方法保存JScrollPane的JPanel,或者......
  2. Use a GUI builder to place components for you in an absolute fashion, or with the assistance of layout managers, or... 使用GUI构建器以绝对的方式为您放置组件,或者在布局管理器的帮助下,或者......
  3. Create your GUI using nested JPanels, each one using its own layout manager, add your JList-containing JScrollPane to one of the JPanels in an intelligent way (which depends on the layout manager) and let your layouts and the components preferred sizes set all the appropriate sizes. 使用嵌套的JPanel创建GUI,每个使用自己的布局管理器,以智能方式将包含JList的JScrollPane添加到其中一个JPanel(取决于布局管理器),并让您的布局和组件首选大小设置为全部适当的尺寸。

In the very short term the first path is the quickest and easiest, but in the medium to long term, it has serious problems because it makes for very inflexible GUI's that while they might look good on one platform look terrible on most other platforms or screen resolutions and that are very difficult to update and maintain. 在短期内,第一条路径是最快捷和最简单的,但从中长期来看,它存在严重的问题,因为它使得GUI非常不灵活,虽然它们在一个平台上看起来很好但在大多数其他平台或屏幕上看起来很糟糕分辨率很难更新和维护。 Instead you will want to study and learn the layout managers and then nest JPanels, each using its own layout manager to create pleasing and complex GUI's that look good on all OS's. 相反,您将需要学习和学习布局管理器,然后嵌套JPanels,每个JPanels都使用自己的布局管理器来创建令人满意的复杂GUI,这些GUI在所有操作系统上都很好看。

The second path will allow you to create quick and easy GUI's, but if you're not familiar with the Swing library yet and reasonably adept at using the layout managers, you can get hamstrung if you need to modify your code at a later time, since you'll be rigidly tied to the code-generation utility. 第二条路径将允许您创建快速简单的GUI,但如果您还不熟悉Swing库并且合理地擅长使用布局管理器,那么如果您以后需要修改代码,则可能会受到阻碍,因为你将严格依赖于代码生成实用程序。 Don't get me wrong though, this is a viable solution if you are pretty good with Swing coding, as long as the GUI builder software doesn't go defunct (which has happened in the past). 不要误解我的意思,如果您使用Swing编码非常好,这是一个可行的解决方案,只要GUI构建器软件不会失效(过去发生过这种情况)。

The best answer to your question in my opinion is to learn how to use the layout managers, and then use them. 在我看来,你的问题的最佳答案是学习如何使用布局管理器,然后使用它们。 And to learn them, please have a look here: The Layout Manager Tutorial . 要了解它们,请看一下: 布局管理器教程


Edit 编辑
Regarding your edited question and the code you've posted: 关于您编辑的问题以及您发布的代码:

  • Never set the layout of the JScrollPane as this will prevent the JScrollPane from functioning. 永远不要设置JScrollPane的布局,因为这会阻止JScrollPane运行。
  • Instead and again, place your JList inside of a JScrollPane (in its viewport actually, but if you pass the JList into the JScrollPane's constructor, you'll be doing this). 反过来,将JList放在JScrollPane中(实际上在其视口中,但如果将JList传递给JScrollPane的构造函数,则会执行此操作)。
  • Then add the JScrollPane into a container, a JPanel, using appropriate layouts. 然后使用适当的布局将JScrollPane添加到容器(JPanel)中。
  • Don't use setBounds(...) 不要使用setBounds(...)
  • Nest your JPanels each using its own layout, each containing other components and JPanels. 使用自己的布局将每个JPanel嵌套,每个布局包含其他组件和JPanel。
  • Pack your GUI after adding all components via the pack() method, and then call setVisible(true) on the top-level window. 在通过pack()方法添加所有组件后打包GUI,然后在顶级窗口中调用setVisible(true)

Edit 2 编辑2
You state in commment: 你在地下说:

Can you provide me with a brief tutorial? 你能给我一个简短的教程吗? I will edit the question right away to show you the problem 我将立即编辑问题以向您显示问题

I stated that I did. 我说过我做过。

I meant for the Jpanel not for the layouts! 我的意思是Jpanel不是为了布局!

Please understand that our only source of understanding of your problems is what you specifically tell us. 请理解,我们解决您问题的唯一来源是您明确告诉我们的内容。 The more pertinent information in your questions, the better we can understand them and the better we can help Here is a great link that will get you to the most important Swing tutorials. 您的问题中的相关信息越多,我们就越能理解它们,我们就越能提供帮助。这是一个很棒的链接,可以帮助您了解最重要的Swing教程。 You can find this link in the tag that you use with your question: 您可以在随问题使用的标记中找到此链接:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM