Question: i got a notepad code and i need to fix it 1. JMenu hides behind TextArea as It is supposed to be viewable in front
i got a notepad code and i need to fix it
1. JMenu hides behind TextArea as It is supposed to be viewable in front of the TextArea.
2. i need to add accelerator to new (ctrl-N), save(ctrl-S) , bold(ctrl-B), italic(ctrl-I), small(ctrl-s) , medium(ctrl-M) , large (ctrl-L)
and here is my code
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package notepad;
/** * * @author Prit */ import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.io.*; import java.util.*;
public class Notepad extends JFrame implements ActionListener { JMenuBar mbar; JMenu file,edit,format,font,font1,font2; JMenuItem item1,item2,item3,item4; JMenuItem item5,item6,item7,item8,item10; // JMenuItem fstyle2,fstyle3; JCheckBoxMenuItem fstyle2,fstyle3; //JMenuItem fsize1,fsize2,fsize4; JRadioButtonMenuItem fsize1,fsize2,fsize4;
JPanel mainpanel; TextArea text;
Font f;
String command=" "; String str=" ";
String str1=" ",str2=" ",str3=" "; String str4=" ";
String str6=" "; String str7=" ",str8=" ",str9=" ";
int len1;
int i=0; int pos1; int len;
public Notepad(String str) {
super(str);
setTitle("Note Editor");
mainpanel=new JPanel(); mainpanel=(JPanel)getContentPane(); mainpanel.setLayout(new FlowLayout());
mbar=new JMenuBar(); setJMenuBar(mbar);
file=new JMenu("File"); edit=new JMenu("Edit"); format=new JMenu("Format"); font2=new JMenu("Size");
file.add(item1=new JMenuItem("New (Ctrl+N)")); file.add(item2=new JMenuItem("Open (Ctrl+O)")); file.add(item3=new JMenuItem("Save (Ctrl+S)")); file.add(edit); file.add(item4=new JMenuItem("Exit"));
edit.add(item5=new JMenuItem("Cut (Ctrl+X)")); edit.add(item6=new JMenuItem("Copy (Ctrl+C)")); edit.add(item7=new JMenuItem("Paste (Ctrl+V)")); edit.add(item8=new JMenuItem("Delete")); edit.add(item10=new JMenuItem("Select All (Ctrl+A)")); mbar.add(file);
//format.add(fstyle2=new JMenuItem("Bold")); //format.add(fstyle3=new JMenuItem("Italic")); fstyle2 = new JCheckBoxMenuItem("Bold"); format.add(fstyle2); fstyle3 = new JCheckBoxMenuItem("Italic"); format.add(fstyle3); format.add(font2); //font2.add(fsize1=new JMenuItem("Small")); //font2.add(fsize2=new JMenuItem("Medium")); //font2.add(fsize4=new JMenuItem("Large")); fsize1=new JRadioButtonMenuItem("Small"); font2.add(fsize1); fsize2=new JRadioButtonMenuItem("Medium"); font2.add(fsize2); fsize4=new JRadioButtonMenuItem("Large"); font2.add(fsize4);
mbar.add(format);
item1.addActionListener(this); item2.addActionListener(this); item3.addActionListener(this); item4.addActionListener(this); item5.addActionListener(this); item6.addActionListener(this); item7.addActionListener(this); item8.addActionListener(this);
item10.addActionListener(this);
fstyle2.addActionListener(this); fstyle3.addActionListener(this);
fsize1.addActionListener(this); fsize2.addActionListener(this);
fsize4.addActionListener(this);
text=new TextArea(26,60); //text=new TextArea(20,40); mainpanel.add(text);
f=new Font("Monotype Corsiva",Font.PLAIN,15); text.setFont(f); }
public void actionPerformed(ActionEvent ae) {
command=(String)ae.getActionCommand();
if(command.equals("New (Ctrl+N)")) { dispose(); Notepad note1 = new Notepad("Untitled-Notepad"); note1.setSize(500,500); note1.setVisible(true); }
try {
if(command.equals("Open (Ctrl+O)")) {
str4=" ";
FileDialog dialog=new FileDialog(this,"Open"); dialog.setVisible(true);
str1=dialog.getDirectory(); str2=dialog.getFile(); str3=str1+str2; File f=new File(str3); FileInputStream fobj=new FileInputStream(f); len=(int)f.length(); for(int j=0;j } text.setText(str4); } } catch(IOException e) {} try { if(command.equals("Save (Ctrl+S)")) { //item3.setKeyAccelerator ('S'); FileDialog dialog1=new FileDialog(this,"Save As",FileDialog.SAVE); dialog1.setVisible(true); str7=dialog1.getDirectory(); str8=dialog1.getFile(); str9=str7+str8; str6=text.getText(); len1=str6.length(); byte buf[]=str6.getBytes(); File f1=new File(str9); FileOutputStream fobj1=new FileOutputStream(f1); for(int k=0;k this.setTitle(str8); } catch(IOException e){} if(command.equals("Exit")) { System.exit(0); } if(command.equals("Cut (Ctrl+X)")) { str=text.getSelectedText(); i=text.getText().indexOf(str); text.replaceRange(" ",i,i+str.length()); } if(command.equals("Copy (Ctrl+C)")) { str=text.getSelectedText(); } if(command.equals("Paste (Ctrl+V)")) { pos1=text.getCaretPosition(); text.insert(str,pos1); } if(command.equals("Delete")) { String msg=text.getSelectedText(); i=text.getText().indexOf(msg); text.replaceRange(" ",i,i+msg.length()); } if(command.equals("Bold")) { String fontName=f.getName(); String fontFamily=f.getFamily(); int fontSize=f.getSize(); int fontStyle=f.getStyle(); f=new Font(fontName,Font.BOLD,fontSize); text.setFont(f); } if(command.equals("Italic")) { String fontName=f.getName(); String fontFamily=f.getFamily(); int fontSize=f.getSize(); int fontStyle=f.getStyle(); f=new Font(fontName,Font.ITALIC,fontSize); text.setFont(f); } if(command.equals("Small")) { String fontName=f.getName(); String fontFamily=f.getFamily(); int fontSize=f.getSize(); int fontStyle=f.getStyle(); f=new Font(fontName,fontStyle,12); text.setFont(f); } if(command.equals("Medium")) { String fontName=f.getName(); String fontFamily=f.getFamily(); int fontSize=f.getSize(); int fontStyle=f.getStyle(); f=new Font(fontName,fontStyle,16); text.setFont(f); } if(command.equals("Large")) { String fontName=f.getName(); String fontFamily=f.getFamily(); int fontSize=f.getSize(); int fontStyle=f.getStyle(); f=new Font(fontName,fontStyle,20); text.setFont(f); } if(command.equals("Select All (Ctrl+A)")) { String strText=text.getText(); int strLen=strText.length(); text.select(0,strLen); } } public static void main(String args[]) { Notepad note = new Notepad("Untitled-Notepad"); note.setSize(500,500); note.setVisible(true); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
