我有一个具有包含标签和TextField的序列,并使用弹簧的布局(这是mainPanel中),并且只包含一个按钮(buttonPanel)另一面板的面板的GUI。 我试图让我的mainPanel有一个垂直滚动条为好。 我想实现我的GUI使得JFrame的内我有2个面板。 mainPanel中出现在框架的顶部和buttonPanel出现mainPanel中的下方。
我的问题是我不能够让面板出现使得buttonPanel是下面的mainPanel,我还不能确定如何将滚动条添加到mainPanel中。 任何帮助,将不胜感激。
编辑:我能解决我的关于JPanels问题,现在我唯一的问题是,我不能让我的mainPanel滚动。 我已经添加了我的下面最近的代码:
这里是我到目前为止的代码:
public static void main(String args[]) {
JFrame frame = new JFrame("SpringLayout");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JScrollPane scroll = new JScrollPane();
Container contentPane = frame.getContentPane();
JButton next = new JButton("Next");
JPanel buttonPanel = new JPanel();
buttonPanel.add(next);
SpringLayout layout = new SpringLayout();
JPanel mainPanel = new JPanel();
mainPanel.setLayout(layout);
contentPane.setLayout(new BorderLayout());
int j = 25;
for(int i =0;i<150;i++){
JLabel label = new JLabel("Enter Name " + i );
JTextField text = new JTextField(15);
mainPanel.add(label);
mainPanel.add(text);
layout.putConstraint(SpringLayout.WEST, label, 10, SpringLayout.WEST,
contentPane);
layout.putConstraint(SpringLayout.NORTH, label, j, SpringLayout.NORTH,
contentPane);
layout.putConstraint(SpringLayout.NORTH, text, j, SpringLayout.NORTH,
contentPane);
layout.putConstraint(SpringLayout.WEST, text, 20, SpringLayout.EAST,
label);
j+=30;
}
//mainPanel.setSize(500,800);
scroll.setPreferredSize(new Dimension(500,500));
scroll.setViewportView(mainPanel);
contentPane.add(scroll);
contentPane.add(buttonPanel,BorderLayout.SOUTH);
//mainWindow.add(contentPane);
frame.setSize(500, 600);
frame.setVisible(true);
}
为了使滚动我只是需要增加我的mainPanel的最佳尺寸,使得它会比更大的滚动条。
public static void main(String args[]) {
JFrame frame = new JFrame("SpringLayout");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JScrollPane scroll = new JScrollPane();
Container contentPane = frame.getContentPane();
JButton next = new JButton("Next");
JPanel buttonPanel = new JPanel();
buttonPanel.add(next);
SpringLayout layout = new SpringLayout();
JPanel mainPanel = new JPanel();
mainPanel.setLayout(layout);
contentPane.setLayout(new BorderLayout());
int j = 25;
for(int i =0;i<18;i++){
JLabel label = new JLabel("Enter Name " + i );
JTextField text = new JTextField(15);
mainPanel.add(label);
mainPanel.add(text);
layout.putConstraint(SpringLayout.WEST, label, 10, SpringLayout.WEST,
contentPane);
layout.putConstraint(SpringLayout.NORTH, label, j, SpringLayout.NORTH,
contentPane);
layout.putConstraint(SpringLayout.NORTH, text, j, SpringLayout.NORTH,
contentPane);
layout.putConstraint(SpringLayout.WEST, text, 20, SpringLayout.EAST,
label);
j+=30;
}
mainPanel.setPreferredSize(new Dimension(mainPanel.getWidth(), 1500));
scroll.setPreferredSize(new Dimension(500,500));
scroll.setViewportView(mainPanel);
contentPane.add(scroll);
contentPane.add(buttonPanel,BorderLayout.SOUTH);
//mainWindow.add(contentPane);
frame.setSize(500, 600);
frame.setVisible(true);
}
用硬编码的码JFrame.setSize()
而不是正确JFrame#pack()
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import javax.swing.SwingUtilities;
public class Main {
public Main() {
JFrame frame = new JFrame("SpringLayout");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton next = new JButton("Next");
JPanel buttonPanel = new JPanel();
buttonPanel.add(next);
SpringLayout layout = new SpringLayout();
JPanel mainPanel = new JPanel();
mainPanel.setLayout(layout);
int j = 25;
for (int i = 0; i < 5; i++) {
JLabel label = new JLabel("Enter Name");
JTextField text = new JTextField(15);
layout.putConstraint(SpringLayout.WEST, label, 10, SpringLayout.WEST,
mainPanel);
layout.putConstraint(SpringLayout.NORTH, label, j, SpringLayout.NORTH,
mainPanel);
layout.putConstraint(SpringLayout.NORTH, text, j, SpringLayout.NORTH,
mainPanel);
layout.putConstraint(SpringLayout.WEST, text, 20, SpringLayout.EAST,
label);
j += 30;
mainPanel.add(label);
mainPanel.add(text);
}
frame.add(mainPanel, BorderLayout.CENTER);
frame.add(buttonPanel, BorderLayout.SOUTH);
frame.setSize(300, 300);
frame.setVisible(true);
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Main mn = new Main();
}
});
}
}
这是我会怎么做:
public static void main(String args[]) {
JFrame frame = new JFrame("SpringLayout");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = frame.getContentPane();
JButton next = new JButton("Next");
JPanel buttonPanel = new JPanel();
buttonPanel.add(next);
GridBagLayout layout = new GridBagLayout();
JPanel mainPanel = new JPanel();
mainPanel.setLayout(layout);
contentPane.setLayout(new BorderLayout());
GridBagConstraints gbc = new GridBagConstraints();
int j = 25;
for (int i = 0; i < 50; i++) {
JLabel label = new JLabel("Enter Name (" + i + ")");
JTextField text = new JTextField(15);
gbc.gridx = 0;
gbc.gridy = i;
mainPanel.add(label, gbc);
gbc.gridx = 1;
mainPanel.add(text, gbc);
}
contentPane.add(new JScrollPane(mainPanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED), BorderLayout.CENTER);
contentPane.add(buttonPanel, BorderLayout.SOUTH);
frame.setSize(500, 800);
frame.setVisible(true);
}
一些修改:*使用GridBagLayout的,而不是SpringLayout中(只是因为我不知道SpringLayout中)*换一个JScrollPane内您的mainPanel
不看,准确地感觉你的榜样。 GridBagConstraints的可微调。