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()
}
}
找找上面设置还是帮助的选项看看有没有回复默认设置就出来了window里有一个show toolbar
或者window ---reset persective还原一下
代码如下:
public class App extends JFrame {
public App() {
this.setSize(400, 400)
this.setLocationRelativeTo(null)
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
// 添加菜单
JMenuBar menuBar = new JMenuBar()
this.setJMenuBar(menuBar)
JMenu menu = new JMenu("文件")
menuBar.add(menu)
JMenuItem testMenuItem = new JMenuItem("打开")
testMenuItem.addActionListener(e -> JOptionPane.showMessageDialog(this, "打开"))
menu.add(testMenuItem)
// 添加工具栏
JToolBar toolBar = new JToolBar()
this.add(toolBar, BorderLayout.NORTH)
JButton btnSave = new JButton("保存")
btnSave.addActionListener(e -> JOptionPane.showMessageDialog(this, "保存"))
toolBar.add(btnSave)
}
public static void main(String[] args) {
new App().setVisible(true)
}
}
运行结果:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)