我有一个 JPanel、4 个组合框和一个按钮。我想要一个 700 x 500 JFrame,面板占据左侧 500 x 500。我想要的右侧是垂直的 2 个组合框,另外 2 个组合框,然后是按钮。希望这是有道理的:我只想让它们全部可见,并且我希望这些框以 2 个为一组配对。我尝试过的示例代码如下:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JComboBox;
import javax.swing.JButton;
public class Test extends JFrame{
public Test () {
super();
//setLayout(new FlowLayout());
JPanel canvas = new JPanel();
canvas.setBackground(Color.red);
canvas.setSize(500, 500);
JComboBox field1 = new JComboBox();
JComboBox field2 = new JComboBox();
JComboBox field3 = new JComboBox();
JComboBox field4 = new JComboBox();
JButton button = new JButton();
JPanel info = new JPanel();
info.setBackground(Color.blue);
info.add(field1, BorderLayout.NORTH);
info.add(field2, BorderLayout.EAST);
info.add(field3, BorderLayout.CENTER);
info.add(field4, BorderLayout.WEST);
info.add(button, BorderLayout.SOUTH);
add(info, BorderLayout.EAST);
add(canvas, BorderLayout.WEST);
setTitle("TEST");
setSize(700, 500);
}
public static void main (String[] args) {
JFrame testFrame = new Test();
testFrame.setVisible(true);
}
}
关于如何进行布局的任何帮助或建议都会很棒。