Ch16
This presentation is the property of its rightful owner.
Sponsored Links
1 / 9

Ch16 筆記本 PowerPoint PPT Presentation


  • 111 Views
  • Uploaded on
  • Presentation posted in: General

Ch16 筆記本. 物件導向系統實務. Step 1: 建立視窗. Win_NoteBook_01 import javax.swing.*; import java.awt.*; import java.awt.event.*; class Win_NoteBook_01 extends JFrame implements ActionListener { private Container c; Win_NoteBook_01() { super(" 筆記本 ");

Download Presentation

Ch16 筆記本

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Ch16

Ch16 筆記本

物件導向系統實務


Step 1

Step 1:建立視窗


Ch16

Win_NoteBook_01

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

class Win_NoteBook_01 extends JFrame

implements ActionListener

{

private Container c;

Win_NoteBook_01()

{

super("筆記本");

c = getContentPane();

c.setBackground(Color.white);

}

public void actionPerformed (ActionEvent e)

{

}

}

Win_NoteBook_Main.java

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

class Win_NoteBook_Main

{

public static void main(String [] args)

{

Win_NoteBook_01 NB = new Win_NoteBook_01();

NB.addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent evt)

{ System.exit(0); }

} );

NB.setSize(300, 200);

NB.setVisible(true);

}

}


Step2

Step2: 加上下式拉式選單


Ch16

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

class Win_NoteBook_02 extends JFrame

implements ActionListener

{

private Container c;

private JMenuBar jmb = new JMenuBar();

private JMenu file = new JMenu("檔案(F)");

private JMenu edit = new JMenu("編輯(E)");

private JMenu help = new JMenu("說明(H)");

Win_NoteBook_02()

{

super("筆記本");

c = getContentPane();

c.setBackground(Color.white);

setJMenuBar(jmb);

file.setMnemonic(KeyEvent.VK_F);

edit.setMnemonic(KeyEvent.VK_E);

help.setMnemonic(KeyEvent.VK_H);

JMenuItem item;

file.add(item = new JMenuItem("新增(N)", KeyEvent.VK_N));

item.addActionListener(this);

file.add(item = new JMenuItem("開啟(O)", KeyEvent.VK_O));

item.addActionListener(this);

file.add(item = new JMenuItem("儲存(S)", KeyEvent.VK_S));

item.addActionListener(this);

file.addSeparator(); //分隔線

file.add(item = new JMenuItem("離開(X)", KeyEvent.VK_X));

item.addActionListener(this);

jmb.add(file);

}

public void actionPerformed (ActionEvent e)

{

if(e.getActionCommand()=="離開(X)")

System.exit(0);

}

}


Step 3

Step 3:加入有捲軸的文字區域


Ch16

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

class Win_NoteBook_03 extends JFrame

implements ActionListener

{

private Container c;

private JMenuBar jmb = new JMenuBar();

private JMenu file = new JMenu("檔案(F)");

private JMenu edit = new JMenu("編輯(E)");

private JMenu help = new JMenu("說明(H)");

private JTextArea area = new JTextArea(15,30);

Win_NoteBook_03()

{

super("筆記本");

c = getContentPane();

c.setBackground(Color.white);

setJMenuBar(jmb);

file.setMnemonic(KeyEvent.VK_F);

edit.setMnemonic(KeyEvent.VK_E);

help.setMnemonic(KeyEvent.VK_H);

JMenuItem item;

file.add(item = new JMenuItem("新增(N)", KeyEvent.VK_N));

item.addActionListener(this);

file.add(item = new JMenuItem("開啟(O)", KeyEvent.VK_O));

item.addActionListener(this);

file.add(item = new JMenuItem("儲存(S)", KeyEvent.VK_S));

item.addActionListener(this);

file.addSeparator(); //分隔線

file.add(item = new JMenuItem("離開(X)", KeyEvent.VK_X));

item.addActionListener(this);

jmb.add(file);

JScrollPane scroll = new JScrollPane(area);

c.add(scroll);

}

public void actionPerformed (ActionEvent e)

{

if(e.getActionCommand()=="離開(X)")

System.exit(0);

}

}


Step 4

Step 4:加入檔案選擇元件


Ch16

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import javax.swing.filechooser.*;

class Win_NoteBook_04 extends JFrame

implements ActionListener

{

private Container c;

private JMenuBar jmb = new JMenuBar();

private JMenu file = new JMenu("檔案(F)");

private JMenu edit = new JMenu("編輯(E)");

private JMenu help = new JMenu("說明(H)");

private JTextArea area = new JTextArea(15,30);

private JFileChooser jfc = new JFileChooser();

Win_NoteBook_04()

{

super("筆記本");

c = getContentPane();

c.setBackground(Color.white);

setJMenuBar(jmb);

file.setMnemonic(KeyEvent.VK_F);

edit.setMnemonic(KeyEvent.VK_E);

help.setMnemonic(KeyEvent.VK_H);

JMenuItem item;

file.add(item = new JMenuItem("新增(N)", KeyEvent.VK_N));

item.addActionListener(this);

file.add(item = new JMenuItem("開啟(O)", KeyEvent.VK_O));

item.addActionListener(this);

file.add(item = new JMenuItem("儲存(S)", KeyEvent.VK_S));

item.addActionListener(this);

file.addSeparator(); //分隔線

file.add(item = new JMenuItem("離開(X)", KeyEvent.VK_X));

item.addActionListener(this);

jmb.add(file);

JScrollPane scroll = new JScrollPane(area);

c.add(scroll);

}

public void actionPerformed (ActionEvent e)

{

if(e.getActionCommand() == "新增(N)")

area.setText("");

if(e.getActionCommand() == "開啟(O)")

{

int n = jfc.showOpenDialog(Win_NoteBook_04.this);

if ( n == JFileChooser.APPROVE_OPTION)

{

File selectedFile = jfc.getSelectedFile();

area.append("開啟檔案:");

area.append(selectedFile.getName() + "\n");

}

}

if(e.getActionCommand() == "儲存(S)")

{

int n = jfc.showSaveDialog(Win_NoteBook_04.this);

if (n == JFileChooser.APPROVE_OPTION)

{

File selectedFile = jfc.getSelectedFile();

area.append("儲存檔案:");

area.append(selectedFile.getName() + "\n");

}

}

if(e.getActionCommand()=="離開(X)")

System.exit(0);

}

}


  • Login