【发布时间】:2018-04-06 10:54:37
【问题描述】:
我目前尝试在我的程序中实现 JScrollPane,但我无法让它工作。
我使用 Eclipse WindowBuilder 来设置我的 GUI,它看起来像 this.
在当前显示的 TabbedPane 中,您可以在顶部看到三个 TextAreas 作为标题,在底部看到一堆 TextAreas。底部的条目是动态生成的,我希望允许用户在那里添加、删除和修改这些项目。因此,用户应该能够滚动浏览底部的这些条目。
从程序右侧的 ScrollBar 可以看出,我已经尝试实现 JScrollPane,但实际上并没有成功滚动。我已经尝试了很多不同的东西,使用了多种类型的容器,阅读了所有的解释并观看了我能找到的所有教程,但我似乎无法在我自己的特定案例中实现它。
在第 61 行中,我创建了 JPanel,它以我自己创建的 Class ItemPane 的形式保存我的项目。 然后在第 64 行中,我在构造函数中创建 JScrollPanel,并将 JPanel 作为 Viewport。 在 #70-#74 的 for 循环中,我将 ItemPanes 添加到我的 JPanel。
正如您在图片中看到的,一切正常,只是我无法滚动浏览我的项目,这些项目进一步延伸到我的窗口底部。
任何建议我做错了什么或为什么它不起作用? 我真的很感激!
(注意:我已将代码精简到所有可能重要的内容。我删除了简单的 Getter 和 Setter 以及与布局本身没有任何关系的所有内容。)
这是我的窗口初始化的样子:
1 public class Test
2 {
3 JFrame frame_Main;
4 JTextField textField_Col1;
5 JTextField textField_Col2;
6 JTextField textField_Col3;
7
8 private void initialize()
9 {
10 frame_Main = new JFrame();
11 frame_Main.setTitle("My Program");
12 frame_Main.setResizable(false);
13 frame_Main.setBounds(100, 100, 1080, 720);
14 frame_Main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
15 frame_Main.getContentPane().setLayout(null);
16
17 JTabbedPane tabbedPane_Main = new JTabbedPane(SwingConstants.TOP);
18 tabbedPane_Main.setBorder(null);
19 tabbedPane_Main.setToolTipText("");
20 tabbedPane_Main.setBounds(0, 0, 1074, 691);
21 frame_Main.getContentPane().add(tabbedPane_Main);
22
23 JPanel panel_Tab1 = new JPanel();
24 panel_Tab1.setBorder(null);
25 tabbedPane_Main.addTab("Tab 1", null, panel_Tab1, "");
26 panel_Tab1.setLayout(null);
27
28 JTabbedPane tabbedPane_Tab1 = new JTabbedPane(SwingConstants.TOP);
29 tabbedPane_Tab1.setBorder(null);
30 tabbedPane_Tab1.setBounds(0, 0, 1069, 663);
31 panel_Tab1.add(tabbedPane_Tab1);
32
33 JPanel panel_Tab1_1 = new JPanel();
34 panel_Tab1_1.setBorder(null);
35 tabbedPane_Tab1.addTab("Tab 1.1", null, panel_Tab1_1, null);
36 panel_Tab1_1.setLayout(null);
37
38 textField_Col1 = new JTextField();
39 textField_Col1.setHorizontalAlignment(SwingConstants.CENTER);
40 textField_Col1.setBounds(10, 11, 341, 20);
41 textField_Col1.setEditable(false);
42 textField_Col1.setText("Col 1");
43 panel_Tab1_1.add(textField_Col1);
44
45 textField_Col2 = new JTextField();
46 textField_Col2.setHorizontalAlignment(SwingConstants.CENTER);
47 textField_Col2.setBounds(362, 11, 341, 20);
48 textField_Col2.setEditable(false);
49 textField_Col2.setText("Col 2");
50 textField_Col2.setColumns(10);
51 panel_Tab1_1.add(textField_Col2);
52
53 textField_Col3 = new JTextField();
54 textField_Col3.setHorizontalAlignment(SwingConstants.CENTER);
55 textField_Col3.setBounds(713, 11, 341, 20);
56 textField_Col3.setEditable(false);
57 textField_Col3.setText("Col 3");
58 textField_Col3.setColumns(10);
59 panel_Tab1_1.add(textField_Col3);
60
61 JPanel panel_Tab1_1_Items = new JPanel();
62 panel_Tab1_1_Items.setLayout(null);
63
64 JScrollPane scrollPane_Items = new JScrollPane(panel_Tab1_1_Items);
65 scrollPane_Items.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
66 scrollPane_Items.setBounds(0, 42, 1064, 593);
67
68 panel_Tab1_1.add(scrollPane_Items);
69
70 for (int i = 0; i < 25; i++)
71 {
72 ItemPane temp = new ItemPane(new Item("Col1 " + i, "Col2 " + i, "Col3 " + i), i);
73 panel_Tab1_1_Items.add(temp);
74 }
75
76 JPanel panel_Tab1_2 = new JPanel();
77 panel_Tab1_2.setBorder(null);
78 tabbedPane_Tab1.addTab("Tab 1.2", null, panel_Tab1_2, null);
79 panel_Tab1_2.setLayout(null);
80
81 JPanel panel_Tab2 = new JPanel();
82 panel_Tab2.setBorder(null);
83 tabbedPane_Main.addTab("Tab 2", null, panel_Tab2, "");
84 panel_Tab2.setLayout(null);
85 }
86 }
虽然 ItemPane 是:
1 public class ItemPane extends JPanel
2 {
3 JTextPane[] textPanes;
4
5 public ItemPane(Item item, int num)
6 {
7 super();
8
9 setBounds(10, 11 + num * 30, 1043, 20);
10 setLayout(null);
11
12 textPanes = new JTextPane[3];
13
14 String[] texts = new String[] { item.getCol1(), item.getCol2(), item.getCol3() };
15
16 for (int i = 0; i < textPanes.length; i++)
17 {
18 textPanes[i] = new JTextPane();
19 textPanes[i].setEditable(false);
20 textPanes[i].setBounds(351 * i, 0, 341, 20);
21 textPanes[i].setText(texts[i]);
22 add(textPanes[i]);
23 }
24 }
25 }
还有物品:
1 public class Item
2 {
3 private String m_col1;
4 private String m_col2;
5 private String m_col3;
6
7 public Item(String col1, String col2, String col3)
8 {
9 m_col1 = col1;
10 m_col2 = col2;
11 m_col3 = col3;
12 }
13 }
【问题讨论】:
-
避免使用
null布局。 -
一般情况下我应该避免使用它吗?我应该避免将它与 JScrollBars 一起使用吗?在任何一种情况下,我为什么要避免它?会带来什么问题?正如我所说,我在 Eclipse 中使用了 WindowBuilder,我认为它会自行将一些布局设置为空。
标签: java swing jpanel jscrollpane jtabbedpane