我需要在GUI上的按钮下添加带边框工厂的网格布局!我尝试了许多不同的方法,但似乎都没有。我把我当前的代码放在了......
public abstract class TestVehicles extends Vehicle implements ActionListener {
public static void main(String[] args) {
JFrame carPark = new JFrame("Car Park");
carPark.setSize(750, 750);
carPark.setResizable(false);
carPark.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
placeComponets(carPark);
carPark.setVisible(true);
}
public static void placeComponets(JFrame carPark) {
carPark.setLayout(new FlowLayout());
JLabel userLabel = new JLabel("");
userLabel.setBounds(750, 750, 750, 750);
carPark.add(userLabel);
userLabel.setBackground(Color.LIGHT_GRAY);
userLabel.setOpaque(true);
userLabel.setHorizontalAlignment(SwingConstants.LEFT);
userLabel.setVerticalAlignment(SwingConstants.TOP);
JButton addCar = new JButton("Add Car");
addCar.setBounds(10, 10, 100, 100);
carPark.add(addCar);
addCar.setVisible(true);
ActionListener addCarButtonListener = new AddCarButtonListener();
addCar.addActionListener(addCarButtonListener);
addCar.setVisible(true);
JButton addLorry = new JButton("Add Lorry");
addLorry.setBounds(10, 10, 25, 25);
carPark.add(addLorry);
addLorry.setVisible(true);
ActionListener addLorryButtonListener = new AddLorryButtonListener();
addLorry.addActionListener(addLorryButtonListener);
addLorry.setVisible(true);
JButton addCoach = new JButton("Add Coach");
addCoach.setBounds(10, 10, 100, 100);
carPark.add(addCoach);
addCoach.setVisible(true);
ActionListener addCoachButtonListener = new AddCoachButtonListener();
addCoach.addActionListener(addCoachButtonListener);
addCoach.setVisible(true);
JPanel gridPanel = new JPanel();
JPanel gridPanelLayout = new JPanel(new GridLayout(4, 5, 10, 10));
carPark.add(gridPanelLayout);
gridPanelLayout.setBackground(Color.WHITE);
gridPanelLayout.setBorder(BorderFactory.createEmptyBorder(4, 4, 10, 10));
Border redBorder = BorderFactory.createLineBorder(Color.RED);
}
答案 0 :(得分:1)
您可以将带有GridBagLayout的JPanel用作contentPane,并将另外两个JPanels,一个带有FlowLayout,另一个带有GridLayout,放在contentPane中。
public class Test implements ActionListener {
protected static JPanel panel;
protected static JPanel panel_1;
public static void main(String[] args) {
JFrame carPark = new JFrame("Car Park");
carPark.setSize(750, 750);
carPark.setResizable(false);
carPark.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{0, 0};
gridBagLayout.rowHeights = new int[]{0, 0, 0};
gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{1.0, 1.0, Double.MIN_VALUE};
carPark.getContentPane().setLayout(gridBagLayout);
panel = new JPanel();
GridBagConstraints gbc_panel = new GridBagConstraints();
gbc_panel.insets = new Insets(0, 0, 5, 0);
gbc_panel.fill = GridBagConstraints.BOTH;
gbc_panel.gridx = 0;
gbc_panel.gridy = 0;
carPark.getContentPane().add(panel, gbc_panel);
panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
panel_1 = new JPanel();
GridBagConstraints gbc_panel_1 = new GridBagConstraints();
gbc_panel_1.fill = GridBagConstraints.BOTH;
gbc_panel_1.gridx = 0;
gbc_panel_1.gridy = 1;
carPark.getContentPane().add(panel_1, gbc_panel_1);
panel_1.setLayout(new GridLayout(1, 0, 0, 0));
carPark.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}