Java AWT向现有框架添加元素

时间:2015-12-08 10:44:07

标签: java awt element

我想在现有的Frame中添加一个按钮和一个TextField。 我想隐藏TextArea而不是添加它们,但我失败了。线112-117,任何帮助将不胜感激(我知道AWT已被弃用......)。

import java.awt.*;
import java.awt.event.*;
import java.io.*;

@SuppressWarnings("serial")

class Notepad extends Frame{
  TextArea text;
  Notepad(String name){
    super(name);
    text = new TextArea(" ", 10, 30);
    add(text);
    MenuBar mbar = new MenuBar();
    setMenuBar(mbar);
    Menu file = new Menu("File");
    MenuItem Open, Save, Close;
    file.add(Open = new MenuItem("Open"));
    file.add(Save = new MenuItem("Save"));
    file.add(Close = new MenuItem("Close"));
    mbar.add(file);
    Menu edit = new Menu("Edit");
    MenuItem Find, Replace, ReplaceAll;
    edit.add(Find = new MenuItem("Find"));
    edit.add(Replace = new MenuItem("Replace/ReplaceAll"));
    mbar.add(edit);
    FileHandler fileHandler = new FileHandler(this);
    Open.addActionListener(fileHandler);
    Save.addActionListener(fileHandler);
    Close.addActionListener(fileHandler);
    EditHandler editHandler = new EditHandler(this);
    Find.addActionListener(editHandler);
    Replace.addActionListener(editHandler);
    MyWindowAdapter w_listener = new MyWindowAdapter(this);
    addWindowListener(w_listener);
  }

  public static void main(String[] foo){
    Notepad notepad = new Notepad("Notepad");
    notepad.setSize(500, 500);
    notepad.setVisible(true);
  }


  class MyWindowAdapter extends WindowAdapter{
    Notepad notepad;
    MyWindowAdapter(Notepad notepad){
      this.notepad = notepad;
    }

    public void windowClosing(WindowEvent we){
      notepad.setVisible(false);
      System.exit(0);
    }
  }

  class FileHandler implements ActionListener{
    Notepad notepad;

    public FileHandler(Notepad notepad){
      this.notepad = notepad;
    }
    public void actionPerformed(ActionEvent ae){
      String arg = ae.getActionCommand();
      if(arg.equals("Close")){
        notepad.setVisible(false);
        System.exit(0);
      }
      try{
        if(arg.equals("Open")){
          FileDialog fd = new FileDialog(notepad, "Open File", FileDialog.LOAD);
          fd.setFile(".txt");
          fd.setVisible(true);
          String filename = fd.getDirectory() + fd.getFile(), temp="";
          FileReader fin = new FileReader(filename);
          int i;
          char c;
          while((i = fin.read())!=-1 ){
            c = (char)i;
            temp+=c;
          }
          fin.close();
          notepad.text.setText(temp);
        }
        else if(arg.equals("Save")){
          FileDialog fd = new FileDialog(notepad, "Save File", FileDialog.SAVE);
          fd.setFile(".txt");
          fd.setVisible(true);
          String filename = fd.getDirectory() + fd.getFile();
          FileWriter fout = new FileWriter(filename);
          fout.write(notepad.text.getText());
          System.out.println(notepad.text.getText());
          fout.close();
        }
      }catch(IOException e){
        ;
      }
    }
  }

  class EditHandler implements ActionListener{
    Notepad notepad;
    TextField find, replace;

    public EditHandler(Notepad notepad){
      this.notepad = notepad;
    }

    public void actionPerformed(ActionEvent ae){
      String arg = ae.getActionCommand();
      DialogHandler dh = new DialogHandler(this);
      if(arg.equals("Find")){
        text.setVisible(false);
        TextField find_field = new TextField();
        Button find = new Button("Find");
        notepad.add(find_field);
        notepad.add(find);
        //NOT SHOWING BUTTON OR TEXTFIELD
      }
      else if(arg.equals("Replace/ReplaceAll")){
        text.setVisible(false);
      }
    }
  }

  class DialogHandler implements ActionListener{
    EditHandler eh;
    DialogHandler(EditHandler eh){
      this.eh = eh;
    }
    public void actionPerformed(ActionEvent ae){
      String arg = ae.getActionCommand();
      if(arg.equals("Find")){
        String search = eh.find.getText();
        String notepad_search = text.getText();
      }
      else if(arg.equals("Replace/ReplaceAll")){
        String search =eh.find.getText();
        String notepad_search = text.getText();
      }
      dispose();
    }
  }
}

1 个答案:

答案 0 :(得分:1)

添加新组件后,您必须验证帧。

class EditHandler implements ActionListener{
    ...
    public void actionPerformed(ActionEvent ae){
        ...
        notepad.add(find_field);
        notepad.add(find);        
        notepad.validate();  // forces the Frame to refresh with new Elements

在面板中总结一组新组件总是一个好主意。这使得将它放在某处更容易。我想你想做的事情会是这样的:

Panel findPanel = new Panel();
TextField find_field = new TextField(); 
find_field.setPreferredSize(new Dimension(150,25));
Button find = new Button("Find");
findPanel.add(find_field);
findPanel.add(find);
notepad.add(findPanel, BorderLayout.EAST);
notepad.validate();