import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.s
ID: 3636212 • Letter: I
Question
import java.awt.*;import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import java.io.*;
import java.util.Scanner;
public class Editor extends JFrame implements ActionListener{
private JTextArea text1=new JTextArea(15,35);
private JTextArea text2=new JTextArea(15,35);
public Editor() {
super("2in1 NOTEPAD");
setSize(800,400);
// background color
text1.setBackground(Color.LIGHT_GRAY);
text2.setBackground(Color.LIGHT_GRAY);
add(new JScrollPane(text1),"West");
add(new JScrollPane(text2),"East");
JMenu editMenu = createFileMenu();
JMenu styleMenu = createStyleMenu();
JMenuBar mb = new JMenuBar();
mb.add(editMenu);
mb.add(styleMenu);
setJMenuBar(mb);
setVisible(true);
}
protected JMenu createFileMenu() {
JMenu menu = new JMenu("File");
JMenuItem save1 = new JMenuItem("Save Side 1");
menu.add(save1);
save1.addActionListener(this);
JMenuItem save2 = new JMenuItem("Save Side 2");
menu.add(save2);
save2.addActionListener(this);
JMenuItem load1 = new JMenuItem("Load Side 1");
menu.add(load1);
load1.addActionListener(this);
JMenuItem load2 = new JMenuItem("Load Side 2");
menu.add(load2);
load2.addActionListener(this);
JMenuItem exit = new JMenuItem("Exit");
menu.add(exit);
exit.addActionListener(this);
return menu;
}
// style menu.
protected JMenu createStyleMenu() {
JMenu menu = new JMenu("Style");
JMenuItem bold = new JMenuItem("Bold");
menu.add(bold);
bold.addActionListener(this);
JMenuItem italic = new JMenuItem("Italic");
menu.add(italic);
italic.addActionListener(this);
JMenuItem normal = new JMenuItem("Normal");
menu.add(normal);
normal.addActionListener(this);
menu.addSeparator();
JMenuItem size12 = new JMenuItem("Size 12");
menu.add(size12);
size12.addActionListener(this);
JMenuItem size14 = new JMenuItem("Size 14");
menu.add(size14);
size14.addActionListener(this);
JMenuItem size18 = new JMenuItem("Size 18");
menu.add(size18);
size18.addActionListener(this);
menu.addSeparator();
JMenuItem serif = new JMenuItem("Serif");
menu.add(serif);
serif.addActionListener(this);
JMenuItem sansserif = new JMenuItem("SansSerif");
menu.add(sansserif);
sansserif.addActionListener(this);
menu.addSeparator();
JMenuItem red = new JMenuItem("Red");
menu.add(red);
red.addActionListener(this);
JMenuItem green = new JMenuItem("Green");
menu.add(green);
green.addActionListener(this);
JMenuItem blue = new JMenuItem("Blue");
menu.add(blue);
blue.addActionListener(this);
return menu;
}
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
// file menu
if(cmd.equals("Save Side 1"))
saveEditor(text1);
else if(cmd.equals("Save Side 2"))
saveEditor(text2);
else if(cmd.equals("Load Side 1"))
loadEditor(text1);
else if(cmd.equals("Load Side 2"))
loadEditor(text2);
else if(cmd.equals("Exit"))
{
if(text1 != null);
if(text2 != null);
{
int exitOption= JOptionPane.showConfirmDialog
(null,
"Do You Want to Save any Files? ",
"Slit Editor",
JOptionPane.YES_NO_OPTION);
if(exitOption==JOptionPane.NO_OPTION)
System.exit(JOptionPane.NO_OPTION);
}
}
// style menu
else if(cmd.equals("Bold"))
{
Font font = text1.getFont();
String name = font.getFontName();
text1.setFont(new Font(font.getFontName(),Font.BOLD,font.getSize()));
font = text2.getFont();
text2.setFont(new Font(font.getFontName(),Font.BOLD,font.getSize()));
}
else if(cmd.equals("Italic"))
{
Font font = text1.getFont();
text1.setFont(new Font(font.getFontName(),Font.ITALIC,font.getSize()));
font = text2.getFont();
text2.setFont(new Font(font.getFontName(),Font.ITALIC,font.getSize()));
}
else if(cmd.equals("Normal"))
{
Font font = text1.getFont();
text1.setFont(new Font(font.getFontName(),Font.PLAIN,font.getSize()));
font = text2.getFont();
text2.setFont(new Font(font.getFontName(),Font.PLAIN,font.getSize()));
}
else if(cmd.equals("Size 12"))
{
Font font = text1.getFont();
text1.setFont(new Font(font.getFontName(),font.getStyle(),12));
font = text2.getFont();
text2.setFont(new Font(font.getFontName(),font.getStyle(),12));
}
else if(cmd.equals("Size 14"))
{
Font font = text1.getFont();
text1.setFont(new Font(font.getFontName(),font.getStyle(),14));
font = text2.getFont();
text2.setFont(new Font(font.getFontName(),font.getStyle(),14));
}
else if(cmd.equals("Size 18"))
{
Font font = text1.getFont();
text1.setFont(new Font(font.getFontName(),font.getStyle(),16));
font = text2.getFont();
text2.setFont(new Font(font.getFontName(),font.getStyle(),16));
}
else if(cmd.equals("Serif"))
{
Font font = text1.getFont();
text1.setFont(new Font("Serif",font.getStyle(),font.getSize()));
font = text2.getFont();
text2.setFont(new Font("Serif",font.getStyle(),font.getSize()));
}
else if(cmd.equals("SansSerif"))
{
Font font = text1.getFont();
text1.setFont(new Font("Sans Serif",font.getStyle(),16));
font = text2.getFont();
text2.setFont(new Font("SansSerif",font.getStyle(),16));
}
else if(cmd.equals("Red"))
{
text1.setForeground(Color.red);
text2.setForeground(Color.red);
}
else if(cmd.equals("Green"))
{
text1.setForeground(Color.green);
text2.setForeground(Color.green);
}
else if(cmd.equals("Blue"))
{
text1.setForeground(Color.blue);
text2.setForeground(Color.blue);
}
}
// save editor
public void saveEditor(JTextArea text)
{
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showSaveDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION) {
File f = chooser.getSelectedFile();
try{
PrintWriter pw = new PrintWriter(f);
pw.write(text.getText());
pw.close();
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(this,"Cannot save file: " + f.getName());
}
}
}
// load editor
public void loadEditor(JTextArea text)
{
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showOpenDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION) {
File f = chooser.getSelectedFile();
try
{
Scanner fsc = new Scanner(f);
text.setText("");
while(fsc.hasNextLine())
{
String line = fsc.nextLine();
text.append(line + " ");
}
fsc.close();
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(this,"Cannot load file: " + f.getName());
}
}
}
//The standard main method.
public static void main(String[] args) {
new Editor();
}
}
Explanation / Answer
package com.screv; import net.rim.device.api.ui.component.LabelField; import net.rim.device.api.ui.component.*; import net.rim.device.api.ui.container.MainScreen; public class SampleAppScreen extends MainScreen { public SampleAppScreen() { // TODO Auto-generated constructor stub LabelField title = new LabelField("Sample Application", LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH); setTitle(title); add(new RichTextField("Hello World!")); } public SampleAppScreen(long arg0) { super(arg0); // TODO Auto-generated constructor stub } } import net.rim.device.api.ui.UiApplication; import net.rim.device.api.ui.*; import net.rim.device.api.ui.component.*; import net.rim.device.api.ui.container.*; import net.rim.device.api.system.*; import net.rim.device.api.i18n.*; public class SampleApp extends UiApplication { public SampleApp() { // TODO Auto-generated constructor stub pushScreen(new SampleAppScreen()); } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub SampleApp theApp = new SampleApp(); //Create an instance of this class theApp.enterEventDispatcher(); //attach an event Dispatcher } } Display a message when the Hello World application closes The onClose() method is inherited from the Screen class and indicates to a BlackBerry device application that a close event has occurred. The Dialog.alert() method creates an alert dialog box that a BlackBerry device application can use to notify a BlackBerry device user of an event or important information. The dialog displays an exclamation mark bitmap. In the SampleScreen class, override the onClose()method to create an alert dialog box and display text. public boolean onClose() { Dialog.alert("Goodbye!"); System.exit(0); return true; } package com.screv; import net.rim.device.api.ui.component.LabelField; import net.rim.device.api.ui.component.*; import net.rim.device.api.ui.container.MainScreen; public class SampleAppScreen extends MainScreen { public SampleAppScreen() { // TODO Auto-generated constructor stub LabelField title = new LabelField("Sample Application", LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH); setTitle(title); add(new RichTextField("Hello World!")); } public SampleAppScreen(long arg0) { super(arg0); // TODO Auto-generated constructor stub } public boolean onClose() { Dialog.alert("Goodbye!"); System.exit(0); return true; } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.