Java GUI 阅读器之面板设计

主要是模仿记事本这样的面板设计,不过在其中还改进改进

类名:MyNote

主要软件包:java.awt*;

javax.swing*;






代码:

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionListener;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;
import javax.swing.JToolBar;




public class MyNote extends JFrame{
private ActionListener actionListener = null;
private JTextArea textArea = null;


public MyNote(String title) {
// TODO Auto-generated constructor stub
//创建菜单事件监听对象
actionListener = new MenuListener(this);
this.setTitle(title);
this.setJMenuBar(createMenuBar());//创建菜单栏

Container container = this.getContentPane();
TextEditFrame editFrame = new TextEditFrame();
this.textArea = editFrame.getTextEdit();
//设置边界布局管理
container.setLayout(new BorderLayout());
//工具栏位置
container.add(createToolBar(),BorderLayout.NORTH);
container.add(editFrame,BorderLayout.CENTER);

this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setBounds(300, 150, 600, 400);
this.setVisible(true);

}
/**
* 创建菜单栏
* @param menuItem
* @param menuTitle
* @return
*/
private JMenu createMenu(String[] menuItems,String menuTitle){

JMenu menu = new JMenu();
JMenuItem menuItem = null;
for(int i = 0 ; i < menuItems.length ; i ++){
// 如果菜单项标题文本为Separator,则添加一个分割行
// 否则添加一个菜单
if(menuItems[i].equals("Separator")){
menu.addSeparator();
}else{
menuItem = new JMenuItem();
menuItem.setText(menuItems[i]);//设置菜单项标题
menuItem.addActionListener(actionListener);//监听
menu.add(menuItem);
}
}
menu.setText(menuTitle);
return menu;
}

private JMenuBar createMenuBar(){
String[] fileMenuTitle = {"新建","打开","Separator","保存","另存为","Separator","退出"};//设置菜单项
String[] editMenuTitle = {"复制","剪切","粘贴"};
String[] helpMenuTitle = {"关于"};
//创建一个菜单栏对象
JMenuBar menubar = new JMenuBar();
JMenu menu = null;
//创建文件菜单
menu = createMenu(fileMenuTitle,"文件(F)");
menubar.add(menu);
menu = createMenu(editMenuTitle,"编辑(E)");
menubar.add(menu);
menu = createMenu(helpMenuTitle,"帮助(H)");
menubar.add(menu);



return menubar;
}


//创建编辑器的工具栏
private JToolBar createToolBar(){
//定义工具栏按钮的标题文本
//String[] fileTitle={"新建","打开","保存","退出"};
String[] editTitle={"普通模式","护眼模式","夜晚模式"};

JToolBar toolBar = new JToolBar();//创建工具栏对象
JButton button = null;//按钮
//在工具栏中添加按钮
for(int i = 0 ; i < editTitle.length; i++){
button = new JButton(editTitle[i]);
button.addActionListener(actionListener);
toolBar.add(button);

}
toolBar.setVisible(true);
return toolBar;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub


MyNote note = new MyNote("文本编辑器");
}
public JTextArea getTextArea(){
return this.textArea;
}


}



后续监听待续............

版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值