我在尝试布局JFrame时遇到问题。我试图添加ToolBar顶部,然后在下面显示Info,然后在右下方的颜色,然后在中心复制,然后打印按钮向左,然后打印机列表到底部。如果有人能帮助我朝着正确的方向发展会很棒。
// Declare GUI Components here
// One JToolBar & JButton
private JPanel mainPanel;
private JPanel detailPanel;
private JPanel toolBarPanel;
private JToolBar jToolbar;
private JButton jbtAdmin, jbtHelp;
// A JPanel called infoPanel & JLabel
private JPanel infoPanel;
private JLabel jlblOne;
// A JPanel called colourPanel
private JPanel colourPanel;
private JRadioButton bwRadioButton, colourRadioButton;
private ButtonGroup btg;
// A JPanel called noCopiesPanel
private JPanel noCopiesPanel;
private JLabel jlbCopies;
private JTextField jtfCopies;
// A JPanel called printerPanel
private JPanel printerPanel;
private JComboBox printerBox;
private JButton jbtPrint;
// Constructor - SetLayout & Add Components here...
// Constructor takes in the selected student and assigns it to currentStudent
public StudentFrame(Student studentIn){
// Set up currentStudent
currentStudent=studentIn;
// Set up Toolbar & add jbtAdmin
toolBarPanel = new JPanel();
toolBarPanel.add(jToolbar = new JToolBar());
jToolbar.add(jbtAdmin = new JButton("Admin"));
jToolbar.add(jbtHelp = new JButton("Help"));
// Set up called infoPanel
infoPanel = new JPanel();
infoPanel.add(jlblOne = new JLabel(currentStudent.toString(), JLabel.CENTER));
// Set up colourPanel with radioButtons
colourPanel = new JPanel(new GridLayout(2,1));
colourPanel.add(bwRadioButton = new JRadioButton("Black & White", true));
colourPanel.add(colourRadioButton = new JRadioButton("Colour"));
btg = new ButtonGroup();
btg.add(bwRadioButton);
btg.add(colourRadioButton);
// Put a TitledBorder around it
colourPanel.setBorder(new TitledBorder("Colour"));
// Set up noCopiesPanel
noCopiesPanel = new JPanel(new GridLayout(1,2));
noCopiesPanel.add(jlbCopies = new JLabel("Copies"));
noCopiesPanel.add(jtfCopies = new JTextField(3));
noCopiesPanel.setBorder(new TitledBorder("Print"));
// Set up jbtPrint JButton
jbtPrint = new JButton("Print",new ImageIcon("Images/printerIcon.png"));
jbtPrint.setHorizontalTextPosition(JButton.CENTER);
jbtPrint.setVerticalTextPosition(JButton.TOP);
jbtPrint.setFont(new Font("Helvetica", Font.BOLD, 30));
jbtPrint.setBackground(Color.LIGHT_GRAY);
jbtPrint.setMnemonic('P');
// Set up printerPanel
printerPanel = new JPanel();
String[] printerList = {"Printer 24001", "Printer 24002", "Printer 24003", "Printer 24004"};
printerPanel.add(printerBox = new JComboBox(printerList));
printerPanel.setBorder(new TitledBorder("Printers"));
detailPanel = new JPanel(new GridLayout(2,1));
detailPanel.add(infoPanel, BorderLayout.NORTH);
detailPanel.add(colourPanel, BorderLayout.WEST);
detailPanel.add(noCopiesPanel, BorderLayout.CENTER);
detailPanel.add(jbtPrint, BorderLayout.EAST);
detailPanel.add(printerPanel, BorderLayout.SOUTH);
mainPanel = new JPanel();
mainPanel.add(toolBarPanel, BorderLayout.NORTH);
mainPanel.add(detailPanel, BorderLayout.SOUTH);
this.add(mainPanel);
//this.add(detailPanel);
答案 0 :(得分:3)
detailPanel = new JPanel(new GridLayout(2,1));
detailPanel.add(infoPanel, BorderLayout.NORTH);
您的布局为GridLayout
,但您尝试设置BorderLayout
位置。如果要设置位置,请将detailPanel
的布局设置为BorderLayout
mainPanel = new JPanel();
mainPanel.add(toolBarPanel, BorderLayout.NORTH);
与此案例相同。 JPanel
有一个默认FlowLayout
。您需要将布局设置为BorderLayout
。
您还应该将detailPanel
添加到CENTER
的{{1}}。
还应将mainPanel
添加到具有JToolBar
BorderLayout
将toolBarPanel = new JPanel();
toolBarPanel.add(jToolbar = new JToolBar());
设置为toolBarPanel