Sie sind auf Seite 1von 13

import javax.swing.

*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.filechooser.*;

import java.io.*;

class Notepad implements ActionListener

static int tabCount;

static String selectedText;

JFrame frame;

JPanel panel;

JMenuBar menubar;

JMenu mFile,mEdit,mBuild;

JPopupMenu pop1;

JMenuItem mNew,mOpen,mSave,mSaveAs,mExit;

JMenuItem mCut,mCopy,mPaste,mFind,mFindReplace;

JMenuItem mCompile,mRun;

JTextArea textArea,textArea2;

JSplitPane splitPane;

JScrollPane scrollPane1,scrollPane2;
JTabbedPane tabbedPane;

ImageIcon icon;

Font font = new Font("ARIAL",Font.PLAIN,20);

static String currentDir = "";

Notepad()

frame = new JFrame("Notepad");

textArea = new JTextArea();

textArea.setFont(font);

textArea2 = new JTextArea();

menubar = new JMenuBar();

tabbedPane = new JTabbedPane(JTabbedPane.TOP); // Default is also top

tabbedPane.addTab("Untitled - File",new JScrollPane(textArea)); // Add text Area into


JscrollPane and then add this JscrollPane into TAB

scrollPane1 = new JScrollPane(textArea2); // Add textArea2 into second


ScrollPane
splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,tabbedPane,scrollPane1);

splitPane.setDividerLocation(500); // Divide the vertical pane

splitPane.setOneTouchExpandable(true);

frame.add(menubar);

frame.setJMenuBar(menubar); // SET the MENU BAR at


TOP

mFile = new JMenu("File");

mEdit = new JMenu("Edit"); // Menus

mBuild = new JMenu("Build");

mNew = new JMenuItem("New");

System.out.println (mNew);

mOpen = new JMenuItem("Open");

mSave = new JMenuItem("Save"); // Menu Items


For FIle

mSaveAs = new JMenuItem("Save As");

mExit = new JMenuItem("Exit");

mCut = new JMenuItem("Cut");

mCopy = new JMenuItem("Copy");

mPaste = new JMenuItem("Paste"); // Menu Items For Edit

mFind = new JMenuItem("Find");


mFindReplace = new JMenuItem("Find & Replace");

mCompile = new JMenuItem("Compile");

mRun = new JMenuItem("Run"); //


Menu Items For BUILD

mFile.add(mNew);

mFile.add(mOpen);

mFile.add(mSave); // ADD menus items(of FILE) to


MENU

mFile.add(mSaveAs);

mFile.add(mExit);

mEdit.add(mCut);

mEdit.add(mCopy);

mEdit.add(mPaste); // ADD menus items(of EDIT) to MENU

mEdit.add(mFind);

mEdit.add(mFindReplace);

mBuild.add(mCompile);

mBuild.add(mRun); // ADD menus items(of BUILD) to MENU


menubar.add(mFile);

menubar.add(mEdit); // ADD Menus to "MENU BAR"

menubar.add(mBuild);

frame.add(splitPane); // ADD SPLIT PANE to FRAME

frame.setVisible(true);

// tabCount = tabbedPane.getTabCount();

// System.out.println (tabCount);

//-------------------------Add Action Listners-------------------------------------------------

mNew.addActionListener(this);

mOpen.addActionListener(this);

mSave.addActionListener(this);

mSaveAs.addActionListener(this);

mExit.addActionListener(this);

mCut.addActionListener(this);

mCopy.addActionListener(this);

mPaste.addActionListener(this);

mFind.addActionListener(this);

mFindReplace.addActionListener(this);
mRun.addActionListener(this);

mCompile.addActionListener(this);

//--------------------------------------------------------------------------------------------

public void actionPerformed(ActionEvent ae)

try

//Methods inherited from class java.util.EventObject -- getSource()

System.out.println ("get source" +ae.getSource());

System.out.println (" get ID" +ae.getID());

JMenuItem mi = (JMenuItem)ae.getSource();

JFileChooser fileChooser = new


JFileChooser("c:\\Users\\ANKIT\\Desktop\\ankit");

tabCount = tabbedPane.getTabCount();
//---------------Here getting the view port and current
view----------------------

int i = tabbedPane.getSelectedIndex();

JScrollPane jsp =
(JScrollPane)tabbedPane.getComponentAt(i);

JViewport jvp = jsp.getViewport();

JTextArea jta = (JTextArea)jvp.getView();

//-------------------------------------------------------------------------------

if(mi == mNew)

JTextArea jta2 = new JTextArea();

jta.setFont(font);

tabCount = tabbedPane.getTabCount();

System.out.println (tabCount);

tabbedPane.addTab("Untitled - File",new JScrollPane(jta2));

tabbedPane.setSelectedIndex(tabCount);

else if(mi == mOpen)

tabCount = tabbedPane.getTabCount();

System.out.println (Notepad.this);
int s = fileChooser.showOpenDialog(null);

System.out.println (JFileChooser.APPROVE_OPTION);

System.out.println (s);

if (s == JFileChooser.APPROVE_OPTION)

File file = fileChooser.getSelectedFile();

Reader r = new BufferedReader(new FileReader(file));

char ch[] = new char[(int)file.length()];

System.out.println ("length of characters is "+ch.length);

r.read(ch,0,ch.length);

String str = new String(ch);

tabCount = tabbedPane.getTabCount();

System.out.println (tabCount);

JTextArea jta2 = new JTextArea();

currentDir = ""+ fileChooser.getCurrentDirectory();

tabbedPane.addTab(fileChooser.getName(file),new
JScrollPane(jta2));

jta2.setText(str);

jta2.setFont(font);

//jta.getActions();

tabbedPane.setSelectedIndex(tabCount); //Set
FOCUS to a tab in a JTabbedPane
r.close();

else if(mi == mSave || mi == mSaveAs)

String tabTitle =
tabbedPane.getTitleAt(tabbedPane.getSelectedIndex());

if(!(tabTitle.equals("Untitled - File")) && !(mi == mSaveAs))

System.out.println ("I am here");

char str[] = jta.getText().toCharArray(); // convert string to char


array

FileOutputStream fos = new


FileOutputStream(currentDir+"\\"+tabTitle);

System.out.println (str);

byte b[] = new byte[str.length];

System.out.println (str.length);

for(int j=0;j<b.length;j++)

b[j] = (byte)str[j];
System.out.println ((char)b[j]);

fos.write(b);

fos.flush();

fos.close();

else

JFileChooser saveFileChooser = new JFileChooser();

int s = saveFileChooser.showSaveDialog(frame);

System.out.println (s);

if(s == JFileChooser.APPROVE_OPTION)

System.out.println (jta.getText());

File file = saveFileChooser.getSelectedFile();

Writer wr = new
FileWriter(saveFileChooser.getCurrentDirectory()+"\\"+file.getName());

currentDir =
""+saveFileChooser.getCurrentDirectory();
String str = jta.getText();

wr.write(str);

wr.flush(); // flush the stream

//JTabbedPane tbPane =
(JTabbedPane)tabbedPane.getTabComponentAt(i);

tabbedPane.setTitleAt(tabbedPane.getSelectedIndex(),file.getName());

wr.close();

else if(mi == mExit)

int j = JOptionPane.showConfirmDialog(null,"Are you sure want to


exit","ERROR",JOptionPane.YES_NO_OPTION);

System.out.println (j);

if(i==0)

frame.dispose();

else if(mi == mCopy)

{
selectedText = jta.getSelectedText();

System.out.println (jta.getSelectedText());

else if(mi == mCut)

selectedText = jta.getSelectedText();

jta.replaceRange("",jta.getSelectionStart(),jta.getSelectionEnd());

else if(mi == mPaste)

jta.replaceSelection(selectedText);

else if(mi == mFind)

JOptionPane optionPane = new JOptionPane();

String str = optionPane.showInputDialog(frame,"Find


What","FIND",optionPane.OK_CANCEL_OPTION);

System.out.println (str);

if(!(str.equals("")) || !(str.equals(null)))

String str2 = jta.getText();;

int j = str2.indexOf(str);
System.out.println (j);

jta.getHighlighter();

catch(Exception e)

e.printStackTrace();

public static void main(String[] args)

new Notepad();

Das könnte Ihnen auch gefallen