求一个JToolbar的例子

andycpp 2007-07-28 02:29:15
要求:就像普通软件的界面一样:一个主窗体,有个菜单栏,菜单栏下面是toolbar,toolbar位置被锁定,不可以任意拖动。

我做不出来啊,希望高手指点,最好有代码,没有代码有思想也行,关键是:菜单和toolbar如何同时在上面,toolbar如何锁定
...全文
886 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
empireconquer 2007-07-30
  • 打赏
  • 举报
回复
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractAction;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;
import javax.swing.border.BevelBorder;

public class ToolBarExample extends JPanel {

public JTextPane pane;

public JMenuBar menuBar;

public JToolBar toolBar;

String fonts[] = { "Serif", "SansSerif", "Monospaced", "Dialog",
"DialogInput" };

public ToolBarExample() {
menuBar = new JMenuBar();

// Create a set of actions to use in both the menu and toolbar
DemoAction leftJustifyAction = new DemoAction("Left", new ImageIcon(
"1.gif"), "Left justify text", 'L');
DemoAction rightJustifyAction = new DemoAction("Right", new ImageIcon(
"2.gif"), "Right justify text", 'R');
DemoAction centerJustifyAction = new DemoAction("Center",
new ImageIcon("3.gif"), "Center justify text", 'M');
DemoAction fullJustifyAction = new DemoAction("Full", new ImageIcon(
"4.gif"), "Full justify text", 'F');

JMenu formatMenu = new JMenu("Justify");
formatMenu.add(leftJustifyAction);
formatMenu.add(rightJustifyAction);
formatMenu.add(centerJustifyAction);
formatMenu.add(fullJustifyAction);

menuBar.add(formatMenu);

toolBar = new JToolBar("Formatting");
toolBar.add(leftJustifyAction);
toolBar.add(rightJustifyAction);
toolBar.add(centerJustifyAction);
toolBar.add(fullJustifyAction);

toolBar.addSeparator();
JLabel label = new JLabel("Font");
toolBar.add(label);

toolBar.addSeparator();
JComboBox combo = new JComboBox(fonts);
combo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
pane.getStyledDocument().insertString(
0,
"Font ["
+ ((JComboBox) e.getSource())
.getSelectedItem() + "] chosen!\n",
null);
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
toolBar.add(combo);

// Disable one of the Actions
fullJustifyAction.setEnabled(false);
}

public static void main(String s[]) {

ToolBarExample example = new ToolBarExample();
example.pane = new JTextPane();
example.pane.setPreferredSize(new Dimension(250, 250));
example.pane.setBorder(new BevelBorder(BevelBorder.LOWERED));
example.toolBar.setMaximumSize(example.toolBar.getSize());

JFrame frame = new JFrame("Menu Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setJMenuBar(example.menuBar);
frame.getContentPane().add(example.toolBar, BorderLayout.NORTH);
frame.getContentPane().add(example.pane, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}

class DemoAction extends AbstractAction {

public DemoAction(String text, Icon icon, String description,
char accelerator) {
super(text, icon);
putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(accelerator,
Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
putValue(SHORT_DESCRIPTION, description);
}

public void actionPerformed(ActionEvent e) {
try {
pane.getStyledDocument().insertString(0,
"Action [" + getValue(NAME) + "] performed!\n", null);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
sheshouxy 2007-07-30
  • 打赏
  • 举报
回复
JToolBar bar = new JToolBar();
JButton button1,button2,button3;
button1 = new JButton("pre");
button2 = new JButton("next");
button3 = new JButton("end");
bar.add(button1);
bar.add(button2);
bar.add(button3);
bar.setFloatable(false);
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.LEFT));
panel.add(bar);

JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("file");
JMenuItem exitMenuItem = new JMenuItem("退出");
menu.add(exitMenuItem);
menuBar.add(menu);

JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
frame.setJMenuBar(menuBar);
frame.add(panel,BorderLayout.NORTH);
frame.setSize(300,300);
frame.setVisible(true);

这段代码满足你所有条件了,创建了一个有菜单栏、工具栏的frame,菜单栏有一个项目file,file下有个exit的子项目;工具栏有三个按钮,不能移动。够明确仔细了吧,给分!
joejoe1991 2007-07-28
  • 打赏
  • 举报
回复
先用分割面板把一个panel分成上下两部分 上面那部分再放个panel 设置成FlowLayout。
然后向上面加button

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧