java怎么设置工具栏

java怎么设置工具栏,第1张

private JMenuBar createJMenuBar(Action[] actions){//创建菜单

JMenuBar menubar = new JMenuBar()//实例化菜单栏

JMenu menuFile = new JMenu("文件")

JMenu menuAbout = new JMenu("帮助")

menuAbout.add(new JMenuItem(actions[0]))

menuFile.add(new JMenuItem(actions[1]))

menubar.add(menuFile)//增加菜单

menubar.add(menuAbout)

return menubar

}

private JToolBar createJToolBar(Action[] actions){//创建工具条

JToolBar toolBar = new JToolBar()//实例化工具条

for(int i=0i<actions.lengthi++){

JButton bt = new JButton(actions[i])

bt.setRequestFocusEnabled(false)

//设置不需要焦点

toolBar.add(bt)//增加按钮到工具栏

}

return toolBar//返回工具栏

}

//以下是我做项目的完整代码,你参考一下

/**************************************

* Title:数据库综合 *** 作

* Description: text类

* date :2008-7-22

* author : yangcong

***************************************/

package framejava

import java.awt.*

import java.awt.event.*

import java.io.*

import javax.swing.*

import javax.swing.text.*

//简单的文本编辑器

public class mainframe extends JFrame{

JPanel textPane = new JPanel()//文本窗格,编辑窗口

JLabel statusBar = new JLabel("JAVA综合 *** 作平台V1.1 杨聪制作")//状态栏

JFileChooser filechooser = new JFileChooser()//文本选择器

JButton shujuku = new JButton("数据库 *** 作")

JButton wangluo = new JButton("网络 *** 作")

JButton about = new JButton(" 关于 ")

JButton exit = new JButton(" 退出 ")

public mainframe(){ //构造函数

enableEvents(AWTEvent.WINDOW_EVENT_MASK)

try {

jbInit()

}

catch (Exception e) {

e.printStackTrace()

}

}

private void jbInit() throws Exception {

this.setTitle("JAVA综合 *** 作平台V1.1") //调用父类构造函数

Action[] actions={ //Action数组,各种 *** 作命令

new AboutAction(),

new ExitAction()

}

textPane.add(shujuku)

textPane.add(wangluo)

textPane.add(about)

textPane.add(exit)

/***********************************************************************/

shujuku.addActionListener(new mainframe_shujuku_actionAdapter(this))

shujuku.setSelected(true)

/***********************************************************************/

/***********************************************************************/

wangluo.addActionListener(new mainframe_wangluo_actionAdapter(this))

wangluo.setSelected(true)

/***********************************************************************/

about.addActionListener(actions[0])

exit.addActionListener(actions[1])

statusBar.setForeground(Color.red)

setJMenuBar(createJMenuBar(actions))

Container container= getContentPane()

container.add(createJToolBar(actions),BorderLayout.NORTH)

container.add(textPane,BorderLayout.CENTER)

container.add(statusBar,BorderLayout.SOUTH)

// mainframe m_view = new mainframe()

// m_view.pack()

// Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize()

// Dimension frameSize = m_view.getSize()

//if (frameSize.height >screenSize.height) {

// frameSize.height = screenSize.height

// }

// if (frameSize.width >screenSize.width) {

// frameSize.width = screenSize.width

// }

// m_view.setLocation( (screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2)

//

// m_view.setSize(260,200)

// m_view.setVisible(true)

setSize(260,200)

setVisible(true)

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

//关闭窗口时退出程序

}

private JMenuBar createJMenuBar(Action[] actions){//创建菜单栏

JMenuBar menubar = new JMenuBar()//实例化菜单栏

JMenu menuFile = new JMenu("文件")

JMenu menuAbout = new JMenu("帮助")

menuAbout.add(new JMenuItem(actions[0]))

menuFile.add(new JMenuItem(actions[1]))

menubar.add(menuFile)//增加菜单

menubar.add(menuAbout)

return menubar

}

private JToolBar createJToolBar(Action[] actions){//创建工具条

JToolBar toolBar = new JToolBar()//实例化工具条

for(int i=0i<actions.lengthi++){

JButton bt = new JButton(actions[i])

bt.setRequestFocusEnabled(false)

//设置不需要焦点

toolBar.add(bt)//增加按钮到工具栏

}

return toolBar//返回工具栏

}

/**************************数据库动作 *** 作*************************************/

void shujuku_actionPerformed(ActionEvent e) {

shujuku.setSelected(false)

datamainframe d_view = new datamainframe()

d_view.pack()

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize()

Dimension frameSize = d_view.getSize()

if (frameSize.height >screenSize.height) {

frameSize.height = screenSize.height

}

if (frameSize.width >screenSize.width) {

frameSize.width = screenSize.width

}

d_view.setLocation( (screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2)

d_view.setSize(270,190)

d_view.setVisible(true)

}

class mainframe_shujuku_actionAdapter

implements java.awt.event.ActionListener {

mainframe adaptee

mainframe_shujuku_actionAdapter(mainframe adaptee) {

this.adaptee = adaptee

}

public void actionPerformed(ActionEvent e) {

adaptee.shujuku_actionPerformed(e)

}

}

/***********************************************************************/

/********************网络动作 *** 作**************************************/

void wangluo_actionPerformed(ActionEvent e) {

wangluo.setSelected(false)

netview n_view = new netview()

n_view.pack()

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize()

Dimension frameSize = n_view.getSize()

if (frameSize.height >screenSize.height) {

frameSize.height = screenSize.height

}

if (frameSize.width >screenSize.width) {

frameSize.width = screenSize.width

}

n_view.setLocation( (screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2)

n_view.setSize(800,700)

n_view.setVisible(true)

}

class mainframe_wangluo_actionAdapter

implements java.awt.event.ActionListener {

mainframe adaptee

mainframe_wangluo_actionAdapter(mainframe adaptee) {

this.adaptee = adaptee

}

public void actionPerformed(ActionEvent e) {

adaptee.wangluo_actionPerformed(e)

}

}

/***********************************************************************/

class ExitAction extends AbstractAction{ //退出命令

public ExitAction(){

super("退出")

}

public void actionPerformed(ActionEvent e){

System.exit(0)

}

}

class AboutAction extends AbstractAction{//关于选项命令

public AboutAction(){

super("关于")

}

public void actionPerformed(ActionEvent e){

JOptionPane.showMessageDialog(mainframe.this,"JAVA综合 *** 作平台V1.1\n"+"杨聪制作")

}

}

public static void main(String args[]){

new mainframe()

}

}

1、打开Eclipse。

2、可以看到,如果是用到ADT某些版本,默认的工具栏是不会显示的,这样 *** 作不是很方便。

3、为了能够显示工具栏,我们首先,鼠标左键单击菜单中的“Window”选项,这样会d出二级目录。

4、找到“ShowToolbar”二级菜单,这个就是用来显示菜单的选项,我们进行点击。

5、这样,可以看到开发工具界面上工具栏已经出现了,这就方便我们使用一些常见的命令工具。

6、我们再看window菜单的时候,发现showtoolbar不见了,取而代之的是hidetoolbar,这个用于隐藏工具栏。

7、Eclipse是一个开放源代码的、基于Java的可扩展开发平台。就其本身而言,它只是一个框架和一组服务,用于通过插件组件构建开发环境。

JPanel toolPanel=new JPanel()

ImageIcon icon=ImageIO.read(new File("myicon.png"))// 读取当前目录下的图标

toolPanel.add(new JButton(new AbstractAction("我的按钮",icon/*设置图标*/) {

   @Override

   public void actionPerformed(ActionEvent e) {

      System.out.println("按钮被点击了")

   }

})


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11869611.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存