使用 BorderLayout 和现有的 JScrollPane 将 Swing 元素添加到 JFrame

Adding Swing Elements to JFrame with BorderLayout and existing JScrollPane

package Input;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

public class ProgramEditor2 {
    JFrame frame;
    JPanel contactListPanel;
    ArrayList<JButton> program;

    public ProgramEditor2() {
        initialize();
    }

    ActionListener al = new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            LineEditor.main((JButton) arg0.getSource(),program,contactListPanel);
        }
    };

    public void initialize() {
        frame = new JFrame();
        frame.setSize(471, 298);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        frame.setVisible(true);
        frame.getContentPane().setLayout(new BorderLayout());

        program = new ArrayList<JButton>();

        // Set layout for contactListPane
        contactListPanel = new JPanel();
        contactListPanel.setLayout(new GridLayout(15, 1)); // 15 rows, 1 column
        contactListPanel.setMinimumSize(new Dimension(471, 298));
        contactListPanel.setPreferredSize(new Dimension(471, 298));
        contactListPanel.setMaximumSize(new Dimension(471, 298));
        for (int i = 0; i < 1; i++) {
            JButton button = new JButton();
            button.addActionListener(al);
            contactListPanel.add(button);
        }
        JScrollPane scrollPane = new JScrollPane(contactListPanel);
        scrollPane
                .setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        frame.getContentPane().add(scrollPane, BorderLayout.CENTER);

    }

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

我正在尝试在滚动面板上方添加一个 JLabel,在下方添加两个 JButton it.I 认为问题在于我在第 57 行将框架布局设置为 BorderLayout。我该如何解决这个问题?

先仔细看看 How to Use BorderLayout

I am trying to add a JLabel above the scrollpanel

frame.add(new JLabel("Hello"), BorderLayout.NORTH);

and two JButtons below it

采用"compound layout"的方法,使用不同的容器和不同的布局管理器,满足内部组件的要求,然后将此容器添加到父容器中,例如

JButton btn1 = ...;
JButton btn2 = ...;
JPanel buttons = new JPanel();
buttons.add(btn1);
buttons.add(btn2);

frame.add(buttons, BorderLayout.SOUTH);

旁注:

  • 我建议使用 JFrame#pack 而不是 frame.setSize(471, 298);,但我只会在你建立基础 UI.
  • 之后才这样做
  • frame.getContentPane().setLayout(null);有点无意义
  • frame.setVisible(true);应该在你建立UI之后最后调用,否则有些组件可能不会出现
  • 避免使用 etMinimumSizesetPreferredSizesetMaximumSize,如果不小心,它们会对布局产生不利影响。有关更多讨论,请参阅 Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?。如果您想更改 JScrollPane 的工作方式,请改用 Scrollable 界面