JScrollPane不能在我的代码中工作

时间:2016-11-28 22:02:51

标签: java swing jscrollpane jlist

在这里,我的Jscrollpane不起作用。我已经在主Jpanel上添加了scrollpane和列表。 Jlist正在运行,但看起来,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);
                }
            });
        }
    }

1 个答案:

答案 0 :(得分:1)

  

我添加了滚动窗格并在主Jpanel上列出。

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

  

我正在使用绝对布局

这是另一个严重的问题。不要使用空布局和setBounds(...)。例如,在列表中调用setBounds将阻止滚动窗格工作,因为列表无法正常扩展内部 JScrollPane。学习和使用布局管理器。

此外,您永远不会将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