小弟正在用java写一个记事本,遇到一下几个问题,求解!谢谢
1.在文本区中如何获取鼠标选中的文本,选中区域的开始位置和结束位置
2.如何设置默认选择单选按钮。如,有向上,向下两个单选按钮,要默认选中向下这个单选按钮
3.如何让对话框单击了按钮后不消失
4.如何返回文件对话框中输入的字符创。如弹出一个Save对话框,要按输入的文件名保存,如何得到那个文件名呢?
------解决方案--------------------------------------------------------
1 getSelectionStart getSelectionEnd getSelectedText
2 requestFocusInWindow
4 JFileChooser 的保存对话框可以得到你的输入
------解决方案--------------------------------------------------------
话不多说 直接上 记事本 源码 嘿嘿
- Java code
import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.io.*;import javax.swing.undo.*;import javax.swing.event.*;public class NotePad extends JFrame { private UndoManager manager; private JFileChooser choose; private File selectedfile; private JTextArea text; private java.awt.Container container; private JMenuBar menuBar; private JTextField status; private boolean newPage = false; // File菜单 private JMenu menu; private JMenuItem newText; private JMenuItem open; private JCheckBoxMenuItem readOnly; private JMenuItem save; private JMenuItem saveAs; private JMenuItem exit; // Edit菜单 private JMenu edit; private JMenuItem undo; private JMenuItem redo; private JMenuItem cut; private JMenuItem copy; private JMenuItem copyAll; private JMenuItem paste; private JMenuItem delete; private JMenuItem deleteAll; private JMenuItem selectAll; private JMenuItem searchAndReplace; // 视图菜单 private JMenu view; private JCheckBoxMenuItem wrap; private JCheckBoxMenuItem onTop; private JMenuItem zoomIn; private JMenuItem zoomOut; // 工具菜单 private JMenu tool; private JMenuItem fontSum; private JMenuItem setcolor; private JMenuItem setFontColor; // 帮助菜单 private JMenu help; private JMenuItem about; // 右键菜单 private JPopupMenu pop; private JMenuItem popUndo; private JMenuItem popRedo; private JMenuItem popCut; private JMenuItem popCopy; private JMenuItem popSelectAll; private JMenuItem popCopyAll; private JMenuItem popPaste; private JMenuItem popDelete; private JMenuItem popClear; public NotePad() { super("记事本"); try{ UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); }catch(Exception e){} container = getContentPane(); container.setLayout(new BorderLayout(0,-2)); manager = new UndoManager(); text = new JTextArea(); text.setFont(new Font("宋体",Font.PLAIN,14)); text.getDocument().addUndoableEditListener(manager); text.setTabSize(2); text.setSelectedTextColor(Color.WHITE); text.setSelectionColor(Color.DARK_GRAY); text.setLineWrap(true); text.setWrapStyleWord(true); text.setCaretColor(Color.RED); text.setMargin(new Insets(3, 10, 3, 7)); text.setDragEnabled(true); container.add(new JScrollPane(text,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED)); menuBar = new JMenuBar(); // File菜单 menu = new JMenu("文件(F)"); menu.setMnemonic('F'); newText = new JMenuItem("新建(N)"); newText.setMnemonic('N'); newText.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { if(selectedfile == null && !text.getText().equals("") && JOptionPane.showConfirmDialog(container, "是否保存", "是否保存", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION){ choose = new JFileChooser(); int state = choose.showSaveDialog(container); if (state == JFileChooser.APPROVE_OPTION) { try { File file = choose.getSelectedFile(); FileWriter os = new FileWriter(file); os.write(text.getText(), 0, text.getText().length()); os.flush(); file.createNewFile(); os.close(); } catch (IOException e) { JOptionPane.showMessageDialog(container, "保存文件失败","ERROR", JOptionPane.ERROR_MESSAGE); } } }else if(selectedfile != null && JOptionPane.showConfirmDialog(container, "是否保存", "是否保存", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION){ try { FileWriter os = new FileWriter(selectedfile); os.write(text.getText(), 0, text.getText().length()); os.flush(); os.close(); } catch (IOException e) {} } text.setText(""); newPage = true; selectedfile = null; status.setText(""); } }); open = new JMenuItem("打开(O)"); open.setMnemonic('O'); open.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { if(selectedfile!=null && JOptionPane.showConfirmDialog(container, "是否保存", "是否保存", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_NO_OPTION){ try{ FileWriter os = new FileWriter(selectedfile); os.write(text.getText(), 0, text.getText().length()); os.flush(); os.close(); newPage = false; }catch(IOException e){} }else if(selectedfile==null && !text.getText().equals("") && JOptionPane.showConfirmDialog(container, "是否保存", "是否保存", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_NO_OPTION){ choose = new JFileChooser(); int state = choose.showSaveDialog(container); if (state == JFileChooser.APPROVE_OPTION) { try { File file = choose.getSelectedFile(); FileWriter os = new FileWriter(file); os.write(text.getText(), 0, text.getText().length()); os.flush(); file.createNewFile(); os.close(); newPage = false; } catch (IOException e) { JOptionPane.showMessageDialog(container, "保存文件失败", "ERROR", JOptionPane.ERROR_MESSAGE); } } } choose = new JFileChooser(); choose.setFileFilter(new filter()); int state = choose.showOpenDialog(container); if (state == JFileChooser.APPROVE_OPTION) { status.setText(choose.getSelectedFile().toString()); try { selectedfile = choose.getSelectedFile(); FileReader isr = new FileReader(selectedfile); text.read(isr, ""); isr.close(); newPage = false; } catch (IOException e) { JOptionPane.showMessageDialog(container, "打开文件失败", "ERROR", JOptionPane.ERROR_MESSAGE); } } } }); readOnly = new JCheckBoxMenuItem("只读模式(R)"); readOnly.setMnemonic('R'); readOnly.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent event) { if(readOnly.isSelected()){ text.setEditable(false); cut.setEnabled(false); paste.setEnabled(false); delete.setEnabled(false); deleteAll.setEnabled(false); redo.setEnabled(false); undo.setEnabled(false); searchAndReplace.setEnabled(false); popClear.setEnabled(false); popCut.setEnabled(false); popDelete.setEnabled(false); popPaste.setEnabled(false); popRedo.setEnabled(false); popUndo.setEnabled(false); } else{ text.setEditable(true); cut.setEnabled(true); paste.setEnabled(true); delete.setEnabled(true); deleteAll.setEnabled(true); redo.setEnabled(true); undo.setEnabled(true); searchAndReplace.setEnabled(true); popClear.setEnabled(true); popCut.setEnabled(true); popDelete.setEnabled(true); popPaste.setEnabled(true); popRedo.setEnabled(true); popUndo.setEnabled(true); } } }); save = new JMenuItem("保存(S)"); save.setMnemonic('S'); save.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { if (newPage == true || selectedfile == null) { choose = new JFileChooser(); int state = choose.showSaveDialog(container); if (state == JFileChooser.APPROVE_OPTION) { try { File file = choose.getSelectedFile(); FileWriter os = new FileWriter(file); os.write(text.getText(), 0, text.getText().length()); os.flush(); file.createNewFile(); os.close(); newPage = false; } catch (IOException e) { JOptionPane.showMessageDialog(container, "保存文件失败","ERROR", JOptionPane.ERROR_MESSAGE); } } } else if(newPage == false && selectedfile != null){ try { FileWriter os = new FileWriter(selectedfile); os.write(text.getText(), 0, text.getText().length()); os.flush(); os.close(); } catch (IOException e) { JOptionPane.showMessageDialog(container, "保存文件失败","ERROR", JOptionPane.ERROR_MESSAGE); } } } }); saveAs = new JMenuItem("另存为(A)"); saveAs.setMnemonic('A'); saveAs.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { choose = new JFileChooser(); int state = choose.showSaveDialog(container); if (state == JFileChooser.APPROVE_OPTION) { try { File file = choose.getSelectedFile(); FileWriter os = new FileWriter(file); os.write(text.getText(), 0, text.getText().length()); os.flush(); file.createNewFile(); os.close(); newPage = false; } catch (IOException e) { JOptionPane.showMessageDialog(container, "保存文件失败","ERROR", JOptionPane.ERROR_MESSAGE); } } } }); exit = new JMenuItem("退出(Q)"); exit.setMnemonic('Q'); exit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { System.exit(0); } }); menu.add(newText); menu.add(open); menu.add(readOnly); menu.add(save); menu.add(saveAs); menu.add(exit);