Professional Documents
Culture Documents
Pgina 1 de 5
Este artigo foi traduzido por mquina. Coloque o ponteiro do mouse sobre as frases do artigo para ver o texto original. Mais informaes.
Traduo
Original
http://msdn.microsoft.com/pt-br/library/ebfe4038(v=vs.80).aspx
02/03/2013
Pgina 2 de 5
class Editor extends Frame implemen ts ActionListener { TextArea textArea = new TextAre a(); //Set up the menu on the defaul t CTOR. Editor() { super("Text AWT Editor"); setLayout(new BorderLayout( )); add("Center", textArea); Menu menu = new Menu("File" ); menu.add(makeMenuItem("Open ")); menu.add(makeMenuItem("Save ")); menu.add(makeMenuItem("Quit ")); MenuBar menuBar = new MenuB ar(); menuBar.add(menu); setMenuBar(menuBar); pack(); } private MenuItem makeMenuItem(S tring name) { MenuItem m = new MenuItem(n ame); m.addActionListener(this); return m; } public static void main(String[ ] s) { new Editor().show(); }
A palavra-chave super define a barra de ttulo para o quadro em linha, super("Text AWT Editor"); O layout do quadro est definido para BorderLayout, que constri uma janela sem espaos entre componentes.Outros formatos quadro Incluir CardLayout e GridBagLayout. As novas Menu("File") e menu.add mtodos Criar um Soltar-Para Baixo menu com itens para o usurio para selecionar. 4. Implementar o manipulador de eventos para cada item de menu Soltar-Para Baixo, inserindo o cdigo a seguir:
http://msdn.microsoft.com/pt-br/library/ebfe4038(v=vs.80).aspx
02/03/2013
Pgina 3 de 5
public void actionPerformed(Act ionEvent e) { String command = e.getActio nCommand(); if (command.equals("Quit")) dispose(); else if (command.equals("Op en")) openFile(); else if (command.equals("Sa ve")) saveFile(); }
5. Implementar mtodos para gerenciar abrindo e salvando arquivos inserindo o cdigo a seguir:
private void openFile() { //Show the Open File dialog box to the user. FileDialog fd = new FileDia log(this, "Open File", FileDialog.L OAD); fd.show(); //Get the file path. StringBuffer sbPath = new S tringBuffer(fd.getDirectory()); sbPath.append("\\"); String fileName = fd.getFil e(); //Cancel if null: if (fileName == null) { return; } else sbPath.append(fileName ); // Open and fill the input stream; paint the TextArea. try { FileInputStream fs = ne w FileInputStream(sbPath.ToString() ); byte[] data = new byte [ sbPath.length() ]; fs.read(data); textArea.setText(new St ring(data)); }
http://msdn.microsoft.com/pt-br/library/ebfe4038(v=vs.80).aspx
02/03/2013
Pgina 4 de 5
catch (IOException e) { textArea.setText(e.toSt ring()); } } private void saveFile() { try { // Show the dialog box to the user. FileDialog fd = new Fil eDialog(this, "Save File", FileDial og.SAVE); fd.show(); //Get the path and file name. StringBuffer sbPath = n ew StringBuffer(fd.getDirectory()); sbPath.append("\\"); // Get the file name to create: sbPath.append(fd.getFil e()); FileOutputStream fo = n ew FileOutputStream(sbPath.ToString ()); // Parse the data: String strdata = textAr ea.getText(); byte[] data = new byte [strdata.length()]; for (int idx = 0; idx < strdata.length(); idx++) { data[idx] = (byte)s trdata.charAt(idx); } // Write the array to t he file. fo.write(data); } catch (IOException ex) { textArea.setText(ex.toS tring()); } } }
6. Press F5 to Compilar and the aplicativo executar. O editor de texto abre e permite que voc comece a digitar.Quando voc Concluir, voc pode salvar o seu
http://msdn.microsoft.com/pt-br/library/ebfe4038(v=vs.80).aspx
02/03/2013
Pgina 5 de 5
Consulte tambm
Outros recursos
Como: Criar um novo aplicativo giro
Contribuies da c omunidade
2013 Microsoft. Todos os direitos reservados.
http://msdn.microsoft.com/pt-br/library/ebfe4038(v=vs.80).aspx
02/03/2013