Jscrollpane 不滚动

问题描述 投票:0回答:1

这是我的 GUI 的代码

public class gui extends JFrame{

JFrame mainF;
JPanel listPanel;
JTextArea jtArea;
ButtonGroup buttons;
ButtonGroup restoreButtons;
JRadioButton createButton;
JRadioButton saveButton;
JRadioButton deleteButton;
JRadioButton redo;
JRadioButton undo;

gui() {

    this.mainF = new JFrame();
    mainF.getContentPane().setBackground(Color.gray);
    mainF.setLayout(null); 
    mainF.setSize(800, 800);

    createButton = new JRadioButton("Create note");
    createButton.setBounds(10, 150, 150, 50);

    saveButton = new JRadioButton("Save note");
    saveButton.setBounds(10, 250, 150, 50);
    saveButton.setEnabled(false);




    redo = new JRadioButton("Redo");
    redo.setBounds(10, 400, 150, 50);
    redo.setEnabled(false);

    undo = new JRadioButton("Undo");
    undo.setBounds(10, 350, 150, 50);
    undo.setEnabled(false);

    deleteButton = new JRadioButton("Delete note");
    deleteButton.setBounds(10, 500, 150, 50);
    deleteButton.setEnabled(false);

    this.jtArea = new JTextArea();
    int centerX = (mainF.getWidth() - 200) / 2;
    int centerY = (mainF.getHeight() - 500) / 2;
    jtArea.setBounds(centerX, centerY, 200, 500);
    jtArea.setBackground(Color.white);
    jtArea.setEnabled(false);

    JScrollPane scrollPane = new JScrollPane(jtArea);
    scrollPane.setBounds(centerX, centerY, 200, 500); 
    jtArea.setLineWrap(true); 
    jtArea.setWrapStyleWord(true); 
    jtArea.setEnabled(false);


    this.listPanel = new JPanel(new GridLayout(0, 1));
    JScrollPane listScrollPane = new JScrollPane(listPanel);
    listScrollPane.setBounds(centerX + 285, centerY, 200, 500);
    listScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

    
    buttons = new ButtonGroup();
    buttons.add(createButton);
    buttons.add(saveButton);
    buttons.add(deleteButton);
    buttons.add(undo);
    buttons.add(redo);


    mainF.add(jtArea);
    mainF.add(createButton);
    mainF.add(saveButton);
    mainF.add(deleteButton);
    mainF.add(redo);
    mainF.add(undo);
    mainF.add(listScrollPane);
    mainF.setVisible(true); 
    mainF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

我仍然不熟悉 GUI 组件,所以这对我来说很新。它在右侧显示一个滚动条,但当它已满时我无法滚动它。我的整个代码应该向该 listPanel 添加按钮,如果它已满,那么我可以滚动它们,但情况似乎并非如此。

java swing jscrollpane
1个回答
0
投票

mainF.setLayout(null);
是你的第一个错误。

public class gui extends JFrame {
    JFrame mainF;

是你的第二个。您实际上使用的是哪个

JFrame
?!作为一般规则,避免从像
JFrame
这样的顶级容器扩展,您不会向类添加任何附加功能,并且
JFrame
是一个复杂的复合组件,最好避免它。

请记住,您并不局限于使用单个布局管理器,您可以通过“复合布局”过程来使用多个布局管理器,例如......

请注意 - 上面的屏幕截图显示了预加载的文本,示例代码中不存在

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;

public class Main {

    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new GUI());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class GUI extends JPanel {

        private JTextArea jtArea;
        private ButtonGroup buttons;
        private JRadioButton createButton;
        private JRadioButton saveButton;
        private JRadioButton deleteButton;
        private JRadioButton redo;
        private JRadioButton undo;
        private JPanel listPanel;

        GUI() {
            setBorder(new EmptyBorder(8, 8, 8, 8));
            JPanel actionsPane = new JPanel(new GridLayout(0, 1));

            // Not sure why you'd use radio buttons for this, but your problem
            createButton = new JRadioButton("Create note");

            saveButton = new JRadioButton("Save note");
            saveButton.setEnabled(false);

            redo = new JRadioButton("Redo");
            redo.setEnabled(false);

            undo = new JRadioButton("Undo");
            undo.setEnabled(false);

            deleteButton = new JRadioButton("Delete note");
            deleteButton.setEnabled(false);

            buttons = new ButtonGroup();
            buttons.add(createButton);
            buttons.add(saveButton);
            buttons.add(deleteButton);
            buttons.add(undo);
            buttons.add(redo);

            actionsPane.add(createButton);
            actionsPane.add(saveButton);
            actionsPane.add(redo);
            actionsPane.add(undo);
            actionsPane.add(deleteButton);

            this.jtArea = new JTextArea();
            // This provides a sizing hint for how wide the text area should be
            jtArea.setColumns(80);

            jtArea.setLineWrap(true);
            jtArea.setWrapStyleWord(true);
            //jtArea.setEnabled(false);

            // Just as a FYI, I'd probably consider using a JList for this
            this.listPanel = new JPanel(new GridLayout(0, 1));
            for (int index = 0; index < 100; index++) {
                listPanel.add(new JLabel("Note " + (index + 1)));
            }

            JScrollPane scrollPane = new JScrollPane(jtArea);
            JScrollPane listScrollPane = new JScrollPane(listPanel);

            setLayout(new GridBagLayout());

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.insets = new Insets(4, 4, 4, 4);
            gbc.anchor = GridBagConstraints.NORTH;

            add(actionsPane, gbc);

            gbc.gridx = 1;
            gbc.weightx = 0.5;
            gbc.weighty = 1;
            gbc.fill = GridBagConstraints.BOTH;

            add(scrollPane, gbc);
            gbc.gridx = 2;
            // If there is nothing in the listPanel, this will collapse to a 
            // 0 width, which is why I'd recommend using JList instead
            add(listScrollPane, gbc);
        }
    }
}

我建议看一下:

© www.soinside.com 2019 - 2024. All rights reserved.