2014-04-05 54 views
2

我在尝试布局我的JFrame时遇到问题。我正在尝试添加ToolBar顶部,然后在下面的Info,然后在右下方着色,然后在中心复制,然后将Print按钮向左移动,然后将Printer List移动​​到底部。如果任何人能够帮助我在正确的方向将是伟大的。带JPanels的JFrame布局

// 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); 

回答

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添加到mainPanelCENTER


另外一个JToolBar应该被添加到容器用BorderLayout

toolBarPanel = new JPanel(); 
toolBarPanel.add(jToolbar = new JToolBar()); 

设置toolBarPanelBorderLayout

+0

那是伟大的,非常感谢您的帮助 – user3058734