NetBeans 中的 Java Swing 滚动窗格

发布于 2024-08-18 09:11:53 字数 1171 浏览 6 评论 0原文

我有 Java 应用程序,它将 JTextFields @runtime 添加到 JPanel 中。基本上,用户单击一个按钮,然后添加新的 JTextField,再次单击添加...

每个新的 JTextField 都位于前一个 JTextField 的正下方。显然,我很快就用完了空间,所以我尝试使用 JScrollPane,这就是地狱开始的地方,因为无论我尝试什么,它都不起作用。

  1. 右键单击 JPanel 并包含在滚动窗格中。没有工作。
  2. 阅读一些示例后,我意识到我必须将 JPanel 作为 JScrollPane 构造函数的参数。我通过右键单击 ScrollPane 和 CustomizeCode 来完成此操作。因为显然自动生成的代码在 NetBeans 中受到保护,我不能手动更改所有这些声明等。还是不行。
  3. 我确实尝试将 JPanel 和/或 JScrollPane 的 PreferedSize 设置为 null,但没有帮助。
  4. JScrollPane 是 TabJPanel 的子级(TabJPanel 又是 TabbedPane 的选项卡)。我试图打乱他们的关系,基本上尝试了 JFrame、JPanel(保存文本字段)、TabJPanel 和 JScrollPane 之间的所有可能的父关系方式,但没有任何效果。
  5. 我还在案例中将 VerticalScrollBar 设置为“始终可见”。所以我看到了滚动条,只是用 JTextFields 填充 JPanel 不会影响它。
  6. 当 JTextField 太多时,它们会移到 JPanel 的底部边框“下方”,我再也看不到它们了。

添加新 JTextFields 的代码如下所示(在相关情况下)。

JTextField newField = new JTextField( columns );
Rectangle coordinates = previousTextField.getBounds();
newField.setBounds(coordinates.x , coordinates.y + 50, coordinates.width, coordinates.height);

JPanel.add(newField);
JPanel.revalidate();
JPanel.repaint();

抱歉,我的帖子很长,我只是想提供尽可能多的信息,因为作为新手,我不知道什么是完全相关的,什么是不相关的。提前致谢 :)

I have Java application which adds JTextFields @ runtime to JPanel. Basically user clicks a button and new JTextField is added, clicks again added again...

Each new JTextField is directly below the previous one. Obviously I run out of space pretty soon so I'm trying to use JScrollPane and thats where the hell begins, because it just doesnt work no matter what I try.

  1. Right click on JPanel and Enclose in Scroll Pane. Didnt work.
  2. After reading some examples I realized I must have JPanel as an argument for JScrollPane constructor. Which I did via right clicking on ScrollPane and CustomizeCode. Because apparently auto-generated code is protected in NetBeans and I cannot just change all those declarations, etc. manually. Still doesnt work.
  3. I did try to set PreferedSize to null for JPanel and/or JScrollPane, didnt help.
  4. JScrollPane is a child of lets call it TabJPanel (which in turn is a tab of TabbedPane). I tried to mess with their relationships, basically trying every possible way of parentship between JFrame, JPanel(holding textfields), TabJPanel and JScrollPane, but nothing worked.
  5. I also made VerticalScrollBar "always visible" just in a case. So I see the scrollbar, it's just that populating that JPanel with JTextFields does not affect it.
  6. When there are too many JTextFields I they go "below" the bottom border of JPanel and I cannot see them anymore.

Code for adding new JTextFields is like this, in a case it's relevant.

JTextField newField = new JTextField( columns );
Rectangle coordinates = previousTextField.getBounds();
newField.setBounds(coordinates.x , coordinates.y + 50, coordinates.width, coordinates.height);

JPanel.add(newField);
JPanel.revalidate();
JPanel.repaint();

Sorry for a long post I'm just trying to provide as much info as possible, because being newbie I dont know whats exactly relevant and whats not. Thanks in advance :)

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

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

发布评论

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

评论(3

寄意 2024-08-25 09:11:53

由于现在有另一个答案,我也添加我的建议。

这听起来与使用带有单列的 JTable 的问题完全一样。 JList 尚不可编辑 (并且可能永远不会)。

JTable 将为您处理布局问题,您可以轻松地通过表格访问值。

使用您自己的 TableModel(一个简单的 Vector 在您的情况下应该足够了),并向其添加值。

As there is another answer now, I'm adding my suggestion too.

This sounds exactly like a problem to use a JTable with a single column. JList is not yet editable (and might never be).

JTable would handle the layout problems for you, and you can easily access the values via the table.

Use your own TableModel (a simple Vector should be sufficient in your case), and add values to it.

心的憧憬 2024-08-25 09:11:53

您可以选择使用 LayoutManager,而不是直接在组件上设置边界。为了测试这一点,一个简单的单列 GridLayout 将对齐设置为垂直应该证明这个概念。

panel.setLayout(new GridLayout(0,1));

rows 参数中的零允许根据需要将行添加到布局中。

An option you have is to utilize a LayoutManager, instead of setting the bounds directly on the components. To test this, a simple single column GridLayout with the alignment set to vertical should prove the concept.

panel.setLayout(new GridLayout(0,1));

zero in the rows param allows for rows to be added to the layout as needed.

两个我 2024-08-25 09:11:53

我用这种方法添加一个滚动窗格,创建一个面板并用几个组件填充它,然后在要添加它的组件中创建一个滚动窗格,剪切并粘贴所有详细信息将落入其中的面板并调整滚动窗格的大小。由于组件占用的空间比滚动窗格上可见的右键单击并选择“设计此容器”更大的空间,因此您可以增加滚动窗格的大小并添加尽可能多的组件。

I do this way to add a scrollpane, create a panel and fill it with few components, then create a scrollpane in the component you want to add it, cut and paste the panel in which all your details will fall in and resize the scrollpane.Because the components take a larger space than the one visible right click on the scrollpane and select design this container, there you can increase the size of the scrollpane and add as many components as you have.

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