【问题标题】:JFrame Layout with JPanels使用 JPanels 的 JFrame 布局
【发布时间】:2014-04-05 17:49:36
【问题描述】:

我在尝试布局 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);

【问题讨论】:

    标签: java swing layout jframe jpanel


    【解决方案1】:
    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());
    

    toolBarPanel 设置为BorderLayout

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-13
      • 2012-09-24
      • 1970-01-01
      • 1970-01-01
      • 2012-01-29
      • 1970-01-01
      • 1970-01-01
      • 2019-07-13
      相关资源
      最近更新 更多