国产激情久久久久影院小草_国产91高跟丝袜_99精品视频99_三级真人片在线观看

Java程序編寫記事本方法

時(shí)間:2024-06-22 18:41:31 JAVA認(rèn)證 我要投稿
  • 相關(guān)推薦

Java程序編寫記事本方法大全

  java語言可以實(shí)現(xiàn)很多程序,那么用java怎么編寫記事本呢?下面跟yjbys小編一起來看看吧!

  import java.awt.*;

  import java.awt.event.*;

  import javax.swing.*;

  import java.io.*;

  public class Notepad extends JFrame {

  // 系統(tǒng)組件聲明

  private JMenuBar menuBar = new JMenuBar();

  private JEditorPane content = new JEditorPane();

  private JScrollPane scroll = new JScrollPane(content);

  private JFileChooser filechooser = new JFileChooser() ;

  private BorderLayout bord = new BorderLayout();

  private JLabel statusBar = new JLabel();

  private JPanel pane = new JPanel();

  private File file = null;

  // 定義文件菜單

  private JMenu fileMenu = new JMenu();

  private JMenuItem newMenuItem = new JMenuItem();

  private JMenuItem openMenuItem = new JMenuItem();

  private JMenuItem saveMenuItem = new JMenuItem();

  private JMenuItem saveAsMenuItem = new JMenuItem();

  private JMenuItem pageSetupMenuItem = new JMenuItem();

  private JMenuItem printMenuItem = new JMenuItem();

  private JMenuItem exitMenuItem = new JMenuItem();

  // 定義風(fēng)格菜單

  private JMenu styleMenu = new JMenu();

  private ButtonGroup styleMenuGroup = new ButtonGroup();

  private JRadioButtonMenuItem javaStyleMenuItem = new JRadioButtonMenuItem();

  private JRadioButtonMenuItem metalStyleMenuItem = new JRadioButtonMenuItem();

  private JRadioButtonMenuItem windowsStyleMenuItem = new JRadioButtonMenuItem();

  // 定義幫助菜單

  private JMenuItem aboutMenuItem = new JMenuItem();

  private JMenuItem helpTopicMenuItem = new JMenuItem();

  private JMenu helpMenu = new JMenu();

  // 構(gòu)造函數(shù)

  public Notepad(){

  initComponents();

  }

  private void initComponents(){

  // 添加子菜單項(xiàng)到文件菜單

  fileMenu.setText("/u6587/u4ef6 (F)");

  newMenuItem.setText(" 新建(N) Ctrl+N");

  openMenuItem.setText(" 打開(O)... Ctrl+O");

  saveMenuItem.setText(" 保存(S) Ctrl+S");

  saveAsMenuItem.setText(" 另存為(A)...");

  pageSetupMenuItem.setText(" 頁面設(shè)置(U)...");

  printMenuItem.setText(" 打印(P)... Ctrl+P");

  exitMenuItem.setText(" 退出");

  fileMenu.add(newMenuItem);

  fileMenu.add(openMenuItem);

  fileMenu.add(saveMenuItem);

  fileMenu.add(saveAsMenuItem);

  fileMenu.addSeparator();

  fileMenu.add(pageSetupMenuItem);

  fileMenu.add(printMenuItem);

  fileMenu.addSeparator();

  fileMenu.add(exitMenuItem);

  // 添加子菜單項(xiàng)到風(fēng)格菜單

  styleMenu.setText("風(fēng)格(S)");

  javaStyleMenuItem.setText("Java默認(rèn)");

  metalStyleMenuItem.setText("Metal風(fēng)格");

  windowsStyleMenuItem.setText("Windows風(fēng)格");

  styleMenuGroup.add(javaStyleMenuItem);

  styleMenuGroup.add(metalStyleMenuItem);

  styleMenuGroup.add(windowsStyleMenuItem);

  styleMenu.add(javaStyleMenuItem);

  styleMenu.add(metalStyleMenuItem);

  styleMenu.add(windowsStyleMenuItem);

  // 添加子菜單項(xiàng)到幫助菜單

  helpMenu.setText("幫助(H)");

  helpTopicMenuItem.setText(" 幫助主題(H)");

  aboutMenuItem.setText(" 關(guān)于記事本(A)");

  helpMenu.add(helpTopicMenuItem);

  helpMenu.addSeparator();

  helpMenu.add(aboutMenuItem);

  // 定義文件菜單下的事件監(jiān)聽

  newMenuItem.addActionListener(new newMenuItem_actionAdapter(this));

  openMenuItem.addActionListener(new openMenuItem_actionAdapter(this));

  saveMenuItem.addActionListener(new saveMenuItem_actionAdapter(this));

  saveAsMenuItem.addActionListener(new saveAsMenuItem_actionAdapter(this));

  pageSetupMenuItem.addActionListener(new pageSetupMenuItem_actionAdapter(this));

  printMenuItem.addActionListener(new printMenuItem_actionAdapter(this));

  exitMenuItem.addActionListener(new exitMenuItem_actionAdapter(this));

  // 定義風(fēng)格菜單下的事件監(jiān)聽

  javaStyleMenuItem.addActionListener(new javaStyleMenuItem_actionAdapter(this));

  metalStyleMenuItem.addActionListener(new metalStyleMenuItem_actionAdapter(this));

  windowsStyleMenuItem.addActionListener(new windowsStyleMenuItem_actionAdapter(this));

  // 定義幫助菜單下的事件監(jiān)聽

  helpTopicMenuItem.addActionListener(new helpTopicMenuItem_actionAdapter(this));

  aboutMenuItem.addActionListener(new aboutMenuItem_actionAdapter(this));

  // 填加菜單到菜單欄

  menuBar.add(fileMenu);

  menuBar.add(styleMenu);

  menuBar.add(helpMenu);

  // 對(duì)主窗口的一些設(shè)置

  this.setDefaultCloseOperation(EXIT_ON_CLOSE);

  this.setTitle("無標(biāo)題 - /u8bb0/u4e8b/u672c");

  this.setSize(640,480);

  setJMenuBar(menuBar);

  pane.setLayout(bord);

  pane.add("Center",scroll);

  setContentPane(pane);

  }

  // 定義新建菜單項(xiàng)方法

  public void newMenuItemActionPerformed(ActionEvent evt){

  file = null;

  if(!("".equals(content.getText()))){

  Object[] options = { " 是(Y) ", " 否(N) "," 取消 " };

  int s = JOptionPane.showOptionDialog(null, "/u6587/u4ef6 "+getTitle()+" /u7684/u6587/u5b57/u5df2/u7ecf/u6539/u53d8/u3002/n/u60f3/u4fdd/u5b58/u6587/u4ef6/u5417/uff1f", "/u8bb0/u4e8b/u672c",

  JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,

  null, options, options[0]);

  switch(s){

  case 0:

  int returnVal=filechooser.showSaveDialog(this);

  if(returnVal == JFileChooser.APPROVE_OPTION) {

  file=filechooser.getSelectedFile();

  try{

  FileWriter fw=new FileWriter(file);

  fw.write(content.getText());

  setTitle(filechooser.getSelectedFile().getName()+" - /u8bb0/u4e8b/u672c");

  fw.close();

  }

  catch(Exception e){

  e.printStackTrace();

  }

  break;

  }

  case 1:

  content.setText("");

  setTitle("無標(biāo)題 - /u8bb0/u4e8b/u672c");

  }

  }

  }

  // 定義打開菜單項(xiàng)方法

  public void openMenuItemActionPerformed(ActionEvent evt){

  try {

  file = null;

  int returnVal = filechooser.showOpenDialog(this);

  if(returnVal == JFileChooser.APPROVE_OPTION){

  file = filechooser.getSelectedFile();

  FileReader fr = new FileReader(file);

  int len = (int)file.length();

  char[] buffer = new char[len];

  fr.read(buffer,0,len);

  fr.close();

  content.setText(new String(buffer));

  }

  }

  catch(Exception e){

  e.printStackTrace();

  }

  }

  // 定義退出菜單項(xiàng)方法

  public void exitMenuItem_actionPerformed(ActionEvent e){

  if(!("".equals(content.getText()))){

  Object[] options = { " 是(Y) ", " 否(N) "," 取消 " };

  int s = JOptionPane.showOptionDialog(null, "文件的文字已經(jīng)改變。/n想保存文件嗎?", "/u8bb0/u4e8b/u672c",

  JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,

  null, options, options[0]);

  switch(s){

  case 0:

  int returnVal=filechooser.showSaveDialog(this);

  if(returnVal == JFileChooser.APPROVE_OPTION) {

  file=filechooser.getSelectedFile();

  try{

  FileWriter fw=new FileWriter(file);

  fw.write(content.getText());

  setTitle(filechooser.getSelectedFile().getName()+" - /u8bb0/u4e8b/u672c");

  fw.close();

  }

  catch(Exception ex){

  ex.printStackTrace();

  }

  break;

  }

  case 1:

  System.exit(0);

  }

  }

  else{

  System.exit(0);

  }

  }

  // 保存事件

  public void saveMenuItemActionPerformed(ActionEvent evt){

  int returnVal=filechooser.showSaveDialog(this);

  if(returnVal == JFileChooser.APPROVE_OPTION) {

  file=filechooser.getSelectedFile();

  try{

  FileWriter fw=new FileWriter(file);

  fw.write(content.getText());

  setTitle(filechooser.getSelectedFile().getName()+" - /u8bb0/u4e8b/u672c");

  fw.close();

  }

  catch(Exception e){

  e.printStackTrace();

  }

  }

  }

  // 另存為事件

  public void saveAsMenuItemActionPerformed(ActionEvent evt){

  filechooser.setDialogTitle("另存為...");

  int returnVal = filechooser.showSaveDialog(this);

  if(returnVal == JFileChooser.APPROVE_OPTION) {

  file=filechooser.getSelectedFile();

  try{

  FileWriter fw=new FileWriter(file);

  fw.write(content.getText());

  setTitle(filechooser.getSelectedFile().getName()+" - /u8bb0/u4e8b/u672c");

  fw.close();

  }

  catch(Exception e){

  e.printStackTrace();

  }

  }

  }

  // 頁面設(shè)置事件

  public void pageSetupMenuItemActionPerformed(ActionEvent evt){

  JOptionPane.showMessageDialog(null,"此功能正在開發(fā)中...");

  }

  // 打印事件

  public void printMenuItemActionPerformed(ActionEvent evt){

  JOptionPane.showMessageDialog(null,"打印中...");

  }

  // 更新風(fēng)格外觀方法

  void changeLookFeel(String className) {

  try {

  UIManager.setLookAndFeel(className);

  }

  catch (Exception e) {

  System.out.println(e);

  }

  SwingUtilities.updateComponentTreeUI(this);

  }

  // Java風(fēng)格事件

  public void javaStyleMenuItemActionPerformed(ActionEvent evt){

  changeLookFeel("javax.swing.plaf.metal.MetalLookAndFeel");

  }

  // Motif風(fēng)格事件

  public void metalStyleMenuItemActionPerformed(ActionEvent evt){

  changeLookFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");

  }

  // MAC風(fēng)格事件

  public void windowsStyleMenuItemActionPerformed(ActionEvent evt){

  changeLookFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

  }

  // 幫助事件

  public void helpTopicMenuItemActionPerformed(ActionEvent evt){

  JOptionPane.showMessageDialog(null,"/u9700/u8981/u5e2e/u52a9/u5417/uff1f");

  }

  // 關(guān)于事件

  public void aboutMenuItemActionPerformed(ActionEvent evt){

  JOptionPane.showMessageDialog(null,"/n/u7a0b/u5e8f/u540d/u79f0/uff1aJava /u8bb0/u4e8b/u672c/n/u4ee3/u7801/u7f16/u5199/uff1aCavien/n/u4f5c/u8005/u7f51/u7ad9/uff1ahttp:/www.cavien.com/nE-mail/u3000/uff1aCavien@163.com");

  }

  // 主函數(shù)

  public static void main(String args[]) {

  Notepad notepad = new Notepad();

  notepad.setVisible(true);

  }

  }

  // 定義新建事件類

  class newMenuItem_actionAdapter implements ActionListener{

  Notepad adaptee;

  newMenuItem_actionAdapter(Notepad adaptee){

  this.adaptee = adaptee;

  }

  public void actionPerformed(ActionEvent evt){

  adaptee.newMenuItemActionPerformed(evt);

  }

  }

  // 定義打開事件類

  class openMenuItem_actionAdapter implements ActionListener{

  Notepad adaptee;

  openMenuItem_actionAdapter(Notepad adaptee){

  this.adaptee = adaptee;

  }

  public void actionPerformed(ActionEvent evt){

  adaptee.openMenuItemActionPerformed(evt);

  }

  }

  // 定義保存事件類

  class saveMenuItem_actionAdapter implements ActionListener{

  Notepad adaptee;

  saveMenuItem_actionAdapter(Notepad adaptee){

  this.adaptee = adaptee;

  }

  public void actionPerformed(ActionEvent evt){

  adaptee.saveMenuItemActionPerformed(evt);

  }

  }

  // 定義另存為事件類

  class saveAsMenuItem_actionAdapter implements ActionListener{

  Notepad adaptee;

  saveAsMenuItem_actionAdapter(Notepad adaptee){

  this.adaptee = adaptee;

  }

  public void actionPerformed(ActionEvent evt){

  adaptee.saveAsMenuItemActionPerformed(evt);

  }

  }

  // 定義頁面設(shè)置事件類

  class pageSetupMenuItem_actionAdapter implements ActionListener{

  Notepad adaptee;

  pageSetupMenuItem_actionAdapter(Notepad adaptee){

  this.adaptee = adaptee;

  }

  public void actionPerformed(ActionEvent evt){

  adaptee.pageSetupMenuItemActionPerformed(evt);

  }

  }

  // 定義打印事件類

  class printMenuItem_actionAdapter implements ActionListener{

  Notepad adaptee;

  printMenuItem_actionAdapter(Notepad adaptee){

  this.adaptee = adaptee;

  }

  public void actionPerformed(ActionEvent evt){

  adaptee.printMenuItemActionPerformed(evt);

  }

  }

  // 定義Java風(fēng)格事件類

  class javaStyleMenuItem_actionAdapter implements ActionListener{

  Notepad adaptee;

  javaStyleMenuItem_actionAdapter(Notepad adaptee){

  this.adaptee = adaptee;

  }

  public void actionPerformed(ActionEvent evt){

  adaptee.javaStyleMenuItemActionPerformed(evt);

  }

  }

  // 定義Java風(fēng)格事件類

  class metalStyleMenuItem_actionAdapter implements ActionListener{

  Notepad adaptee;

  metalStyleMenuItem_actionAdapter(Notepad adaptee){

  this.adaptee = adaptee;

  }

  public void actionPerformed(ActionEvent evt){

  adaptee.metalStyleMenuItemActionPerformed(evt);

  }

  }

  // 定義Java風(fēng)格事件類

  class windowsStyleMenuItem_actionAdapter implements ActionListener{

  Notepad adaptee;

  windowsStyleMenuItem_actionAdapter(Notepad adaptee){

  this.adaptee = adaptee;

  }

  public void actionPerformed(ActionEvent evt){

  adaptee.windowsStyleMenuItemActionPerformed(evt);

  }

  }

  // 定義幫助主題事件類

  class helpTopicMenuItem_actionAdapter implements ActionListener{

  Notepad adaptee;

  helpTopicMenuItem_actionAdapter(Notepad adaptee){

  this.adaptee = adaptee;

  }

  public void actionPerformed(ActionEvent evt){

  adaptee.helpTopicMenuItemActionPerformed(evt);

  }

  }

  // 定義關(guān)于軟件事件類

  class aboutMenuItem_actionAdapter implements ActionListener{

  Notepad adaptee;

  aboutMenuItem_actionAdapter(Notepad adaptee){

  this.adaptee = adaptee;

  }

  public void actionPerformed(ActionEvent evt){

  adaptee.aboutMenuItemActionPerformed(evt);

  }

  }

  // 定義退出事件類

  class exitMenuItem_actionAdapter implements ActionListener {

  Notepad adaptee;

  exitMenuItem_actionAdapter(Notepad adaptee){

  this.adaptee = adaptee;

  }

  public void actionPerformed(ActionEvent evt){

  adaptee.exitMenuItem_actionPerformed(evt);

  }

【Java程序編寫記事本方法】相關(guān)文章:

Java怎么編寫走馬燈程序08-19

Java怎么編寫Oracle存儲(chǔ)過程09-15

如何編譯java程序09-28

Java編寫計(jì)算器的的常見做法01-22

Java程序開發(fā)與運(yùn)行環(huán)境06-21

實(shí)現(xiàn)鼠標(biāo)畫圖的Java程序06-22

Java線程同步的方法10-25

Java枚舉的常用方法10-05

如何使用Swing編寫全屏程序09-20

sun認(rèn)證java程序員須知Java日志框架09-11