Java 使用GridLayout在中间显示两个滚动窗格

Java 使用GridLayout在中间显示两个滚动窗格,java,swing,jscrollpane,jlist,Java,Swing,Jscrollpane,Jlist,我正在完成这项任务,需要完成这项任务 我基本上需要两个滚动窗格,每个窗格中有一个JList。我很难同时获得两个JScrollPane,以便根据需要显示每个JList的滚动条。。第二个滚动窗格覆盖了第一个 这是我的密码: public Something() { super("Something"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); setSize(700, 400

我正在完成这项任务,需要完成这项任务

我基本上需要两个滚动窗格,每个窗格中有一个JList。我很难同时获得两个JScrollPane,以便根据需要显示每个JList的滚动条。。第二个滚动窗格覆盖了第一个

这是我的密码:

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添加一组文件,并将这些文件列在左侧滚动窗格中,并获得来自分派线程的单独进程的结果。我提到这一点只是为了以防万一有什么变化

谢谢

我想您需要将scrollPane1和scrollPane2添加到中心。。。e、 g


然后在这个.addcenter,BorderLayout.center的框架中添加中心。

它不应该是center.addscrollPane1;和居中。添加滚动窗格2;?这不是评论,这是答案;没问题@里奇波特
center.add(scrollpane1) 
center.add(scrollPane2)