JScrollPane不能在我的代码中工作

问题描述:

在这里,我的Jscrollpane不起作用。我已经在主Jpanel上添加了scrollpane并列出了它们。 Jlist正在工作,但看起来,JScrollpane不能在此代码中工作。JScrollPane不能在我的代码中工作

我的主类扩展了Jframe,所以从主类我只通过主面板,并在同一个面板上工作。 在这里我使用绝对布局。

public class ExternalsLinks extends JPanel { 

     ///Links List 
     private JList mainList; 

     ///List Custom Border 
     Border lineBorder = BorderFactory.createLineBorder(Color.BLACK, 5); 

     ///Main Jpanel 
     private JPanel mainPanel; 

     ///Scroll Pane for Lists 
     private JScrollPane mainListScrollPane; 

     /// Lebels 
     private JLabel lblExternalLinks; 

     ///Buttons 
     private Button btnBackCredits = new Button("Back"); 

     ///Button Properties 
     private final int BUTTON_X = 220; private final int BUTTON_Y = 450; 
     private final int BUTTON_X_LENGTH = 350; private final int BUTTON_Y_LENGTH = 50; 

     ///List Options 
     private String [] listMenu = {"Searching", "Sorting"}; 

     ///Constructors 
     public ExternalsLinks(JPanel mainPanel) { 

      this.mainPanel = mainPanel; 
      this.mainPanel.setLayout(null); 

      mainPanel.setBackground(Color.WHITE); 

      initialize_All(); 
     } 

     /// Initialize method to initialize all components to JFrame 
     private void initialize_All() { 

      ///Main Panel Components 

      lblExternalLinks = new JLabel("\"External Links\""); 
      lblExternalLinks.setBounds(290, 0, 400, 100); 
      lblExternalLinks.setFont(new Font("courier", Font.BOLD, 30)); 
      mainPanel.add(lblExternalLinks); 

      mainList = new JList(listMenu); 
      mainList.setBackground(Color.CYAN); 
      mainList.setForeground(Color.BLACK); 
      mainList.setFont(new Font("Calibiri", Font.BOLD, 25)); 

      mainList.setVisibleRowCount(2); 
      mainList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 

      mainList.setBorder(lineBorder); 
      mainList.setBounds(50, 80, 300, 350); 

      mainListScrollPane = new JScrollPane(); 
      mainPanel.add(mainListScrollPane); 
      mainPanel.add(mainList); 





      ///Buttons 
      btnBackCredits.setBounds(BUTTON_X, BUTTON_Y, BUTTON_X_LENGTH, BUTTON_Y_LENGTH); 
      mainPanel.add(btnBackCredits); 

      btnBackCredits.addActionListener(new ActionListener() { 

       @Override 
       public void actionPerformed(ActionEvent arg0) { 
        mainList.setVisible(false); 

        mainPanel.removeAll(); 
        new MainMenu(mainPanel); 
       } 
      }); 
     } 
    } 

我已经加入了滚动窗格和主要的JPanel同时列出。

这是一个问题 - 不要这样做。您只能将一个组件添加到一个容器,并且JScrollPane算作一个容器。因此,将JList添加到JScrollPane的视口,然后仅将JScrollPane添加到GUI。

我使用绝对布局

这是另一个严重的问题。不要使用空布局和setBounds(...)。例如,在列表中调用setBounds将阻止滚动窗格工作,因为列表无法像应该那样在内部扩展。了解并使用布局管理器。

此外,您永远不会将JList添加到JScrollPane的视口!

你需要JScrollPane scrollPane = new JScrollPane(myList);或类似的东西,无论你的变量名是什么。请仔细阅读教程,因为这里已经详细解释了这些内容。

例如:

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Font; 
import javax.swing.*; 

@SuppressWarnings("serial") 
public class ExternalsLinks2 extends JPanel { 

    // constants 
    private static final String[] LIST_DATA = {"Searching", "Sorting"}; 
    private static final Font LIST_FONT = new Font("Calibiri", Font.BOLD, 25); 
    private static final Font LABEL_FONT = new Font("courier", Font.BOLD, 30); 
    private static final int LIST_VISIBLE_ROW_COUNT = 10; 
    private static final String TITLE_TEXT = "External Links"; 
    private static final Color LIST_BG = Color.CYAN; 

    // JList field created with constant array data 
    private JList<String> jList = new JList<>(LIST_DATA); 

    public ExternalsLinks2() { 
     jList.setFont(LIST_FONT); 
     jList.setPrototypeCellValue("ABCDEFGHIJKLMNOP ABCDE"); 
     jList.setVisibleRowCount(LIST_VISIBLE_ROW_COUNT); 
     jList.setBackground(LIST_BG); 

     JLabel titleLabel = new JLabel(TITLE_TEXT, SwingConstants.CENTER); 
     titleLabel.setFont(LABEL_FONT); 

     JButton backButton = new JButton("Back"); 
     // add ActionListener or AbstractAction here 

     setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 
     setLayout(new BorderLayout(5, 5)); // use BorderLayout 
     add(titleLabel, BorderLayout.PAGE_START); // JLabel at top 
     add(new JScrollPane(jList), BorderLayout.CENTER); // JList inside of JScrollPane in center 
     add(backButton, BorderLayout.PAGE_END); // JButton at bottom 
    } 

    private static void createAndShowGui() { 
     JFrame frame = new JFrame("External Links"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(new ExternalsLinks2()); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(() -> createAndShowGui()); 
    } 
} 

哪个作为显示:

enter image description here

+0

哪个LAYO ut会很容易并且很好用?我还需要在特定位置上设置列表位置。 –

+0

@ MD.NASHIDKAMAL:如果您有一个复杂的GUI,您通常会使用其自己的布局嵌套JPanel。 FlowLayout最简单也最不强大。我也推荐学习BorderLayout和GridLayout。熟悉这些之后,查看BoxLayout,然后查看GridBagLayout。 –

+0

流程布局只是自动排列组件:D我无聊。我知道边界布局,我会用它。谢谢:) –