当前位置:网站首页 > Java基础 > 正文

java基础记事本代码

import

java

.awt.BorderLayout;

import

java

.awt.Container;

import

java

.awt.Font;

import

java

.awt.event.ActionEvent;

import

java

.awt.event.ActionListener;

import

java

.awt.event.InputEvent;

import

java

.awt.event.KeyAdapter;

import

java

.awt.event.KeyEvent;

import

java

.awt.event.MouseAdapter;

import

java

.awt.event.MouseEvent;

import

java

.awt.event.WindowAdapter;

import

java

.awt.event.WindowEvent;

import

java

.io.BufferedReader;

import

java

.io.BufferedWriter;

import

java

.io.File;

import

java

.io.FileReader;

import

java

.io.FileWriter;

import

java

.io.IOException;

import

java

x.swing.BorderFactory;

import

java

x.swing.JFileChooser;

import

java

x.swing.JFrame;

import

java

x.swing.JLabel;

import

java

x.swing.JMenu;

import

java

x.swing.JMenuBar;

import

java

x.swing.JMenuItem;

import

java

x.swing.JOptionPane;

import

java

x.swing.JPopupMenu;

import

java

x.swing.JScrollPane;

import

java

x.swing.JTextArea;

import

java

x.swing.KeyStroke;

import

java

x.swing.ScrollPaneConstants;

import

java

x.swing.SwingConstants;

public class JNotePadUI extends JFrame {

private JMenuItem menuOpen;

private JMenuItem menuSave;

private JMenuItem menuSaveAs;

private JMenuItem menuClose;

private JMenu editMenu;

private JMenuItem menuCut;

private JMenuItem menuCopy;

private JMenuItem menuPaste;

private JMenuItem menuAbout;

private JTextArea textArea;

private JLabel stateBar;

private JFileChooser fileChooser;

private JPopupMenu popUpMenu;

public JNotePadUI() {

super("新建文本文件");

setUpUIComponent();

setUpEventListener();

setVisible(true);

}

private void setUpUIComponent() {

setSize(640, 480);

// 菜单栏

JMenuBar menuBar = new JMenuBar();

// 设置「文件」菜单

JMenu fileMenu = new JMenu("文件");

menuOpen = new JMenuItem("打开");

// 快捷键设置

menuOpen.setAccelerator(

KeyStroke.getKeyStroke(

KeyEvent.VK_O,

InputEvent.CTRL_MASK));

menuSave = new JMenuItem("保存");

menuSave.setAccelerator(

KeyStroke.getKeyStroke(

KeyEvent.VK_S,

InputEvent.CTRL_MASK));

menuSaveAs = new JMenuItem("另存为");

menuClose = new JMenuItem("关闭");

menuClose.setAccelerator(

KeyStroke.getKeyStroke(

KeyEvent.VK_Q,

InputEvent.CTRL_MASK));

fileMenu.add(menuOpen);

fileMenu.addSeparator(); // 分隔线

fileMenu.add(menuSave);

fileMenu.add(menuSaveAs);

fileMenu.addSeparator(); // 分隔线

fileMenu.add(menuClose);

// 设置「编辑」菜单

JMenu editMenu = new JMenu("编辑");

menuCut = new JMenuItem("剪切");

menuCut.setAccelerator(

KeyStroke.getKeyStroke(KeyEvent.VK_X,

InputEvent.CTRL_MASK));

menuCopy = new JMenuItem("复制");

menuCopy.setAccelerator(

KeyStroke.getKeyStroke(KeyEvent.VK_C,

InputEvent.CTRL_MASK));

menuPaste = new JMenuItem("粘贴");

menuPaste.setAccelerator(

KeyStroke.getKeyStroke(KeyEvent.VK_V,

InputEvent.CTRL_MASK));

editMenu.add(menuCut);

editMenu.add(menuCopy);

editMenu.add(menuPaste);

// 设置「关于」菜单

JMenu aboutMenu = new JMenu("关于");

menuAbout = new JMenuItem("关于JNotePad");

aboutMenu.add(menuAbout);

menuBar.add(fileMenu);

menuBar.add(editMenu);

menuBar.add(aboutMenu);

setJMenuBar(menuBar);

// 文字编辑区域

textArea = new JTextArea();

textArea.setFont(new Font("宋体", Font.PLAIN, 16));

textArea.setLineWrap(true);

JScrollPane panel = new JScrollPane(textArea,

ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,

ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

Container contentPane = getContentPane();

contentPane.add(panel, java基础记事本代码 BorderLayout.CENTER);

// 状态栏

stateBar = new JLabel("未修改");

stateBar.setHorizontalAlignment(SwingConstants.LEFT);

stateBar.setBorder(

BorderFactory.createEtchedBorder());

contentPane.add(stateBar, BorderLayout.SOUTH);

popUpMenu = editMenu.getPopupMenu();

fileChooser = new JFileChooser();

}

private void setUpEventListener() {

// 按下窗口关闭钮事件处理

addWindowListener(

new WindowAdapter() {

public void windowClosing(WindowEvent e) {

closeFile();

}

}

);

// 菜单 - 打开

menuOpen.addActionListener(

new ActionListener() {

public void actionPerformed(ActionEvent e) {

openFile();

}

}

);

// 菜单 - 保存

menuSave.addActionListener(

new ActionListener() {

public void actionPerformed(ActionEvent e) {

saveFile();

}

}

);

// 菜单 - 另存为

menuSaveAs.addActionListener(

new ActionListener() {

public void actionPerformed(ActionEvent e) {

saveFileAs();

}

}

);

// 菜单 - 关闭文件

menuClose.addActionListener(

new ActionListener() {

public void actionPerformed(ActionEvent e) {

closeFile();

}

}

);

// 菜单 - 剪切

menuCut.addActionListener(

new ActionListener() {

public void actionPerformed(ActionEvent e) {

cut();

}

}

);

// 菜单 - 复制

menuCopy.addActionListener(

new ActionListener() {

public void actionPerformed(ActionEvent e) {

copy();

}

}

);

// 菜单 - 粘贴

menuPaste.addActionListener(

new ActionListener() {

public void actionPerformed(ActionEvent e) {

paste();

}

}

);

// 菜单 - 关于

menuAbout.addActionListener(

new ActionListener() {

public void actionPerformed(ActionEvent e) {

// 显示对话框

JOptionPane.showOptionDialog(null,

"程序名称:

JNotePad

" +

"程序设计:

" +

"简介:

一个简单的文字编辑器

" +

" 可作为验收

Java

的实现对象

" +

" 欢迎网友下载研究交流

版权声明


相关文章:

  • java基础应用程序2024-10-29 09:34:00
  • 阿里java基础面试2024-10-29 09:34:00
  • java基础语法自学2024-10-29 09:34:00
  • java基础29节2024-10-29 09:34:00
  • java基础面试题及答案最新2024-10-29 09:34:00
  • java基础银行金融管理系统2024-10-29 09:34:00
  • java基础设计书2024-10-29 09:34:00
  • java基础的核心2024-10-29 09:34:00
  • java全栈基础技术栈2024-10-29 09:34:00
  • java只会基础找工作知乎2024-10-29 09:34:00