import java.awt.*; import java.awt.event.*; import java.awt.font.*; import javax.swing.*; import java.io.*; public class Texted extends JFrame implements ActionListener { JMenuItem menuSave, menuCount; JTextArea editArea, textField; //JButton button; static Texted frame; Texted() { JMenuBar menubar=new JMenuBar(); setJMenuBar(menubar); JMenu menuFile=new JMenu("File", false); menubar.add(menuFile); menuSave=new JMenuItem("Save"); menuCount=new JMenuItem("Set Count"); menuFile.add(menuSave); menuFile.add(menuCount); JPanel panel=new JPanel(); panel.setLayout(new BorderLayout()); panel.setBackground(Color.white); getContentPane().add(panel, "Center"); editArea=new JTextArea(); editArea.setFont(new Font("Arial", Font.PLAIN, 12)); editArea.setLineWrap(true); editArea.setWrapStyleWord(true); editArea.setEditable(true); JScrollPane areaScrollPane=new JScrollPane(editArea); areaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); areaScrollPane.setPreferredSize(new Dimension(500, 300)); areaScrollPane.setBorder(BorderFactory.createTitledBorder("Editor area")); panel.add(areaScrollPane); //JPanel panel2=new JPanel(); //panel2.setLayout(new BorderLayout()); //textField=new JTextArea(); //JScrollPane areaScrollPane2=new JScrollPane(textField); //areaScrollPane2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); //areaScrollPane2.setPreferredSize(new Dimension(500, 100)); //areaScrollPane2.setBorder(BorderFactory.createTitledBorder("Preview area")); //panel2.add(areaScrollPane2, BorderLayout.CENTER); //panel.add(panel2, BorderLayout.SOUTH); menuSave.addActionListener(this); menuCount.addActionListener(this); } public void actionPerformed(ActionEvent event) { Object source=event.getSource(); String returned=null; FileOutputStream out=null; String fileName="fail.txt"; int n; if(source==menuCount) { String inputValue=JOptionPane.showInputDialog("Enter number:"); int iVal=Integer.valueOf(inputValue).intValue(); byte xEndLine[] = new String("\n").getBytes(); try{ out=new FileOutputStream(fileName); int iStart = 0; int iLen = editArea.getText().length(); String szLine; while(true){ if(iStart > iLen) break; editArea.setSelectionStart(iStart); editArea.setSelectionEnd(iStart + iVal); iStart += iVal; szLine = editArea.getSelectedText(); byte b[]=szLine.getBytes(); out.write(b); out.write(xEndLine); } } catch(IOException e){} if(out!=null) { try{ out.close(); } catch(IOException e){} } } if(source==menuSave) { returned=textField.getText(); if(returned!=null) { try { byte b[]=returned.getBytes(); out=new FileOutputStream(fileName); out.write(b); } catch(IOException e) {} finally { if(out!=null) { try{out.close();} catch(IOException e) {} } } } else { System.out.print("Tekst puudub!"); } } } public static void main(String[] args) { frame=new Texted(); frame.setTitle("Tekstieditor"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } }