1
我對Java相當陌生,所以我希望我不會讓我的問題複雜化。將兩個JToolBar添加到邊界佈局。
基本上我試圖將兩個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();
}
感謝您的幫助! – user2994367