Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Текстовый редактор
- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- class MyFrame extends Frame implements ItemListener{
- TextArea v;
- Choice fonts, sizes;
- Checkbox bold, italic;
- Font font;
- String FontList[] = {"Dialog", "DialogInput", "Serif"};
- Menu file;
- MenuItem open, exit, kill;
- MenuBar mbar;
- public MyFrame(String s) {
- addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent we) {
- System.exit(0);
- }
- });
- setLayout(null);
- setTitle(s);
- setSize(new Dimension(800, 350));
- setLocation(200, 200);
- setMinimumSize(new Dimension(390, 150));
- setFont(new Font ("Dialog", 0, 12));
- setVisible(true);
- v = new TextArea();
- v.setBounds(20, 50, 760, 240);
- v.setEditable(false);
- fonts = new Choice();
- sizes = new Choice();
- for (int i = 0; i < FontList.length; ++i) fonts.add(FontList[i]);
- for (int i = 8; i < 73; ++i) sizes.add("" + i);
- fonts.setSize(200, 20);
- sizes.select(6);
- bold = new Checkbox("Bold");
- italic = new Checkbox("Italic");
- bold.setSize(45, 20);
- italic.setSize(45, 20);
- mbar = new MenuBar();
- file = new Menu("Файл");
- exit = new MenuItem("Закрыть");
- open = new MenuItem("Открыть");
- kill = new MenuItem("Не нажимать");
- file.add(open);
- file.add(kill);
- file.add(exit);
- mbar.add(file);
- MyMenuHandler handler = new MyMenuHandler(this);
- setMenuBar(mbar);
- open.addActionListener(handler);
- exit.addActionListener(handler);
- kill.addActionListener(handler);
- bold.addItemListener(this);
- italic.addItemListener(this);
- sizes.addItemListener(this);
- fonts.addItemListener(this);
- add(v);
- add(fonts);
- add(sizes);
- add(bold);
- add(italic);
- }
- public void itemStateChanged(ItemEvent ie) {
- repaint();
- }
- public void paint(Graphics g) {
- font = new Font (fonts.getSelectedItem(), (italic.getState()?Font.ITALIC:0)|(bold.getState()?Font.BOLD:0), sizes.getSelectedIndex() + 8);
- v.setFont(font);
- int width = getWidth(), height = getHeight();
- setSize(width, height);
- v.setSize(width - 40, height - 90);
- fonts.setLocation(20, height - 35);
- sizes.setLocation(230, height - 35);
- bold.setLocation(280, height - 35);
- italic.setLocation(335, height - 35);
- }
- }
- class MyDialog extends Dialog implements ActionListener{
- public MyDialog (Frame parent, String title) {
- super(parent, title, true);
- addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent we) {
- dispose();
- }
- });
- setLayout(null);
- setSize(60, 80);
- setResizable(false);
- Button button = new Button ("Да");
- Label label = new Label ("Ты слепой??");
- button.addActionListener(this);
- label.setBounds(20, 30, 100, 20);
- button.setBounds(30, 50, 40, 20);
- add(label);
- add(button);
- setLocation(parent.getLocation().x + 300, parent.getLocation().y + 150);
- setVisible(true);
- }
- public void actionPerformed (ActionEvent ae) {
- dispose();
- }
- }
- class MyMenuHandler implements ActionListener {
- MyFrame frame;
- public MyMenuHandler(MyFrame frame) {
- this.frame = frame;
- }
- public void actionPerformed(ActionEvent ae){
- String name = ae.getActionCommand();
- if (name.equals("Закрыть")) System.exit(0);
- if (name.equals("Не нажимать")) new MyDialog(frame, "Error");
- if (name.equals("Открыть")) {
- FileDialog fd = new FileDialog(frame, "Выберите файл");
- FileInputStream fin;
- int c;
- String msg ="";
- fd.setVisible(true);
- String file = fd.getDirectory() + fd.getFile();
- try {
- fin = new FileInputStream (file);
- do {
- c = fin.read();
- if (c != -1) msg += (char) c;
- } while (c != -1);
- fin.close();
- } catch (IOException e) {}
- frame.v.setText(msg);
- }
- }
- }
- public class sample8 {
- public static void main(String[] args) {
- MyFrame f = new MyFrame("I'm a name");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment