如何使用Java Swing操作在Netbeans中创建菜单项和工具栏项
我正在读一本java书籍,作者使用相同的操作创建菜单项和工具栏项。 我如何使用NetBeans实现这一点? 代码如下:如何使用Java Swing操作在Netbeans中创建菜单项和工具栏项,java,swing,netbeans,action,netbeans-7,Java,Swing,Netbeans,Action,Netbeans 7,我正在读一本java书籍,作者使用相同的操作创建菜单项和工具栏项。 我如何使用NetBeans实现这一点? 代码如下: public class ActionInterfaceDemo extends JFrame{ private JPanel buttonPanel = new JPanel(); private FlowLayout flowLayout = new FlowLayout(); //---Default constructor p
public class ActionInterfaceDemo extends JFrame{
private JPanel buttonPanel = new JPanel();
private FlowLayout flowLayout = new FlowLayout();
//---Default constructor
public ActionInterfaceDemo()
{
//Create image icons
ImageIcon leftImageIcon = new ImageIcon(getClass().getResource("image/leftAlignment.png"));
ImageIcon centerImageIcon = new ImageIcon(getClass().getResource("image/centerAlignment.png"));
ImageIcon rightImageIcon = new ImageIcon(getClass().getResource("image/rightAlignment.png"));
//Create actions
Action leftAction = new MyAction("Left", leftImageIcon, "Left alignment for the buttons in the panel", new Integer(KeyEvent.VK_L), KeyStroke.getKeyStroke(KeyEvent.VK_L, ActionEvent.CTRL_MASK));
Action centerAction = new MyAction("Center",centerImageIcon, "Center alignment for the buttons in the panel", new Integer(KeyEvent.VK_C), KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK));
Action rightAction = new MyAction("Right", rightImageIcon, "Right alignment for the buttons in the panel", new Integer(KeyEvent.VK_R), KeyStroke.getKeyStroke(KeyEvent.VK_R, ActionEvent.CTRL_MASK));
//Create menus
JMenuBar jMenuBar1 = new JMenuBar();
JMenu jmenuAlignment = new JMenu("Alignment");
setJMenuBar(jMenuBar1);
jMenuBar1.add(jmenuAlignment);
//Add actions to the menu
jmenuAlignment.add(leftAction);
jmenuAlignment.add(centerAction);
jmenuAlignment.add(rightAction);
//Add actions to the toolbar
JToolBar jToolBar1 = new JToolBar(JToolBar.HORIZONTAL);
jToolBar1.setBorder(BorderFactory.createLineBorder(Color.red));
jToolBar1.add(leftAction);
jToolBar1.add(centerAction);
jToolBar1.add(rightAction);
//Add buttons to the button panel
buttonPanel.setLayout(flowLayout);
JButton jbtLeft = new JButton(leftAction);
JButton jbtCenter = new JButton(centerAction);
JButton jbtRight = new JButton(rightAction);
buttonPanel.add(jbtLeft);
buttonPanel.add(jbtCenter);
buttonPanel.add(jbtRight);
//Add toolbar to the east and panel to the center
add(jToolBar1, BorderLayout.NORTH);
add(buttonPanel, BorderLayout.CENTER);
}
//inner class MyAction
private class MyAction extends AbstractAction
{
String name;
MyAction(String name, Icon icon)
{
super(name, icon);
this.name = name;
}
MyAction(String name, Icon icon, String desc, Integer mnemonic, KeyStroke accelerator)
{
super(name,icon);
putValue(Action.SHORT_DESCRIPTION, desc);
putValue(Action.MNEMONIC_KEY, mnemonic);
putValue(Action.ACCELERATOR_KEY, accelerator);
this.name = name;
}
@Override
public void actionPerformed(ActionEvent e) {
if (name.equals("Left"))
flowLayout.setAlignment(FlowLayout.LEFT);
else if (name.equals("Center"))
flowLayout.setAlignment(FlowLayout.CENTER);
else if (name.equals("Right"))
flowLayout.setAlignment(FlowLayout.RIGHT);
buttonPanel.revalidate();
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
ActionInterfaceDemo frame = new ActionInterfaceDemo();
frame.setLocationRelativeTo(null); //Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("ActionInterfaceDemo");
//frame.pack();
frame.setSize(new Dimension(600, 200));
frame.setVisible(true);
}
}
谢谢。两个
JMenuItem
和JButton
都有一个setAction()
方法,可以用来设置操作的相同实例JMenuItem
和JButton
都有一个setAction()
方法,可用于设置操作的相同实例
相同操作是什么意思?我的意思是,在JMenu和JToolBar的构造函数中使用相同的操作,使用它们的add方法。我正在尝试将代码手动添加到我创建的Netbeans JForm中。我相信如果您知道自己在做什么的话,这没有问题。你说的相同操作是什么意思?我的意思是在JMenu和JToolBar的构造函数中使用相同的操作,使用它们的add方法。我现在正在尝试手动将代码添加到我创建的Netbeans JForm中。我相信如果你知道你在做什么,这没有问题。我决定用构造函数手工编码。我在预览或设计模式下看不到结果,但在运行项目时可以看到。我决定用构造函数手工编码。我在预览或设计模式下看不到结果,但在运行项目时可以看到结果。