首页 文章

将两个JToolBar添加到边框布局 .

提问于
浏览
1

我对Java很新,所以我希望我不要过于复杂化我的问题 .

基本上我试图将两个JToolbar垂直添加到一个边框布局的北容器中 . 但是,我不认为你可以在边框布局中添加多个JToolbar到单个位置容器,所以我想到的可能解决方案是在borderlayout中嵌入一个borderlayout并将一个置于北方,另一个位于中心,但我不知道如何实现这一目标 . 任何有关实现这一目标的最佳解决方案的建议将不胜感激 .

以下是我的程序的源代码 . 为了让我的程序运行,我想要的第二个JToolbar位于“bar”JToolbar下面的位置已放置在南容器中 .

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class FeedBar2 extends JFrame {

public FeedBar2() {
    super("FeedBar 2");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // create icons
    ImageIcon loadIcon = new ImageIcon("load.gif");
    ImageIcon saveIcon = new ImageIcon("save.gif");
    ImageIcon subscribeIcon = new ImageIcon("subscribe.gif");
    ImageIcon unsubscribeIcon = new ImageIcon("unsubscribe.gif");
    // create buttons
    JButton load = new JButton("Load", loadIcon);
    JButton save = new JButton("Save", saveIcon);
    JButton subscribe = new JButton("Subscribe", subscribeIcon);
    JButton unsubscribe = new JButton("Unsubscribe", unsubscribeIcon);

    //create help buttons
    JButton help = new JButton("Help");
    JButton about = new JButton("About");
    JButton contact = new JButton("Contact Us");

    // add buttons to toolbar
    JToolBar bar = new JToolBar();
    bar.add(load);
    bar.add(save);
    bar.add(subscribe);
    bar.add(unsubscribe);

    //add buttons to help toolbar
    JToolBar helpbar = new JToolBar();
    helpbar.add(help);
    helpbar.add(about);
    helpbar.add(contact);

    // create dropdown menu
    JMenuItem j1 = new JMenuItem("Load");
    JMenuItem j2 = new JMenuItem("Save");
    JMenuItem j3 = new JMenuItem("Subscribe");
    JMenuItem j4 = new JMenuItem("Unsubscribe");
    JMenuItem h1 = new JMenuItem("Help");
    JMenuItem h2 = new JMenuItem("About");
    JMenuItem h3 = new JMenuItem("Contact Us");
    JMenuBar menubar = new JMenuBar();
    JMenuBar helpmenubar = new JMenuBar();
    JMenu menu = new JMenu("Feeds");
    menu.add(j1);
    menu.add(j2);
    menu.addSeparator();
    menu.add(j3);
    menu.add(j4);


    JMenu helpmenu = new JMenu("Help");
    helpmenu.add(h1);
    helpmenu.add(h2);
    helpmenu.add(h3);
    menubar.add(menu);
    menubar.add(helpmenu);

    // prepare user interface
    JTextArea edit = new JTextArea(8, 40);

    JScrollPane scroll = new JScrollPane(edit);

   BorderLayout bord = new BorderLayout();


   //Looking for the "help bar" to be vertically placed under the "bar" 
   //toolbar.

    setLayout(bord); 
    add("North", bar);
    add("Center", scroll);
    add("South", helpbar);

    setJMenuBar(menubar);



    pack();
    setVisible(true);
}

public static void main(String[] arguments) {
    FeedBar2 frame = new FeedBar2();
}

1 回答

  • 1

    我会使用GridLayout:

    //setLayout(bord); // the default layout of a frame is a BorderLayout
    //add("North", bar); // don't use String literals, the API has variables for you to use
    //add("Center", scroll);
    //add("South", helpbar);
    
    
    JPanel toolbars = new JPanel( new GridLayout(0, 1) );
    toolbars.add(bar);
    toolbars.add(helpBar);
    
    add(toolBars, BorderLayout.NORTH)
    add(scroll, BorderLayout.CENTER);
    

相关问题