使用 GridLayout 居中的两个 ScrollPanes
Two ScrollPanes in Center using GridLayout
我正在完成这项任务,需要实现这一目标...
我基本上需要两个 ScrollPanes,每个 ScrollPanes 都有一个 JList。我很难将两个 JScrollPane 并排放置,以便每个 JList 的滚动条都在必要时出现。第二个 ScrollPane 覆盖了第一个...
这是我的代码:
public Something() {
super("Something");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setSize(700, 400);
setLayout(new BorderLayout());
center = new JPanel();
center.setLayout(new GridLayout(1, 2));
String labels[] = { "A", "B", "C", "D","E", "F", "G", "H","I", "J",
"K","L", "M", "N", "O","P", "Q", "R", "S","T", "U", "V", "W", "X",
"Y", "Z"};
list1 = new JList(labels);
list2 = new JList(labels);
//add a JScrollPane containing JList to frame
JScrollPane scrollPane1 = new JScrollPane();
scrollPane1.setViewportView(list1);
list1.setLayoutOrientation(JList.VERTICAL);
JScrollPane scrollPane2 = new JScrollPane();
list2.setLayoutOrientation(JList.VERTICAL);
scrollPane2.setViewportView(list2);
add(scrollPane1,BorderLayout.CENTER);
add(scrollPane2,BorderLayout.CENTER);}
除此之外,我能够正确添加所有其他组件。
现在我正在添加一个字符串数组用于演示目的,实际上我想使用 JFileChooser 添加一组文件并将这些文件列在左侧的 ScrollPane 中,并获得来自调度的单独过程的结果线。我提到这一点只是为了以防万一。
谢谢
我认为您想将 scrollPane1
和 scrollPane2
添加到 center
...例如
center.add(scrollpane1)
center.add(scrollPane2)
然后将center
添加到框架中(this.add(center,BorderLayout.CENTER)
)。
我正在完成这项任务,需要实现这一目标...
我基本上需要两个 ScrollPanes,每个 ScrollPanes 都有一个 JList。我很难将两个 JScrollPane 并排放置,以便每个 JList 的滚动条都在必要时出现。第二个 ScrollPane 覆盖了第一个...
这是我的代码:
public Something() {
super("Something");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setSize(700, 400);
setLayout(new BorderLayout());
center = new JPanel();
center.setLayout(new GridLayout(1, 2));
String labels[] = { "A", "B", "C", "D","E", "F", "G", "H","I", "J",
"K","L", "M", "N", "O","P", "Q", "R", "S","T", "U", "V", "W", "X",
"Y", "Z"};
list1 = new JList(labels);
list2 = new JList(labels);
//add a JScrollPane containing JList to frame
JScrollPane scrollPane1 = new JScrollPane();
scrollPane1.setViewportView(list1);
list1.setLayoutOrientation(JList.VERTICAL);
JScrollPane scrollPane2 = new JScrollPane();
list2.setLayoutOrientation(JList.VERTICAL);
scrollPane2.setViewportView(list2);
add(scrollPane1,BorderLayout.CENTER);
add(scrollPane2,BorderLayout.CENTER);}
除此之外,我能够正确添加所有其他组件。
现在我正在添加一个字符串数组用于演示目的,实际上我想使用 JFileChooser 添加一组文件并将这些文件列在左侧的 ScrollPane 中,并获得来自调度的单独过程的结果线。我提到这一点只是为了以防万一。
谢谢
我认为您想将 scrollPane1
和 scrollPane2
添加到 center
...例如
center.add(scrollpane1)
center.add(scrollPane2)
然后将center
添加到框架中(this.add(center,BorderLayout.CENTER)
)。