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,并在其下方添加两个JButtons。我认为问题在于第57行将框架布局设置为BorderLayout。如何解决这个问题?
frame.add(new JLabel("你好"), BorderLayout.NORTH)
; - MadProgrammer