代码在下面,复制或者剪切以后,再粘贴问题就来了,粘贴的东西都会自动换行的,大家测试一下就知道了,有什么解决办法?public class HTMLDocumentEditor extends JFrame implements ActionListener
{
/** 声明一个网页文档对象变量*/
private HTMLDocument document;
/** 创建一个文本编辑板*/
private JTextPane textPane = new JTextPane();
private boolean debug = false;
/** 声明一个文件对象变量*/
/** 添加剪切侦听器*/
private Action cutAction = new DefaultEditorKit.CutAction();
/** 添加复制侦听器*/
private Action copyAction = new DefaultEditorKit.CopyAction();
/** 添加粘贴侦听器*/
private Action pasteAction = new DefaultEditorKit.PasteAction();
/** 构造方法*/
public HTMLDocumentEditor()
{
/** 设置主窗体标题*/
super("HTMLDocumentEditor");
HTMLEditorKit editorKit = new HTMLEditorKit();
/** 创建默认文档指向网页引用document*/
document = (HTMLDocument)editorKit.createDefaultDocument(); // 强制SWINGSET实现跨平台,不改变风格
try
{
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
// 如果你想用系统的界面风格替代,请注释掉上一行代码,而取消下一行代码的注释:
//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception exc)
{
//产生异常,则显示错误消息:加载L&F错误
System.err.println("Error loading L&F: " + exc);
} //调用初始化方法
init();
} /**主方法,起动程序*/
public static void main(String[] args)
{
//创建一个类的实例,即创建一个网页编辑器
HTMLDocumentEditor editor = new HTMLDocumentEditor();
} /**初始化各组件的方法*/
public void init()
{
//调用自定义继承WindowListener的侦听器FrameListener,给主窗体添加WindowListener
addWindowListener(new FrameListener()); JPanel editorControlPanel = new JPanel();
//editorControlPanel.setLayout(new GridLayout(3,3));
editorControlPanel.setLayout(new FlowLayout()); /* 按钮 */
JButton cutButton = new JButton(cutAction); //创建“剪切”按钮,添加剪切侦听
JButton copyButton = new JButton(copyAction); //创建“复制”按钮,添加复制侦
JButton pasteButton = new JButton(pasteAction); //创建“粘贴”按钮,添加粘贴侦 cutButton.setText("Cut");
copyButton.setText("Copy");
pasteButton.setText("Paste");
editorControlPanel.add(cutButton);
editorControlPanel.add(copyButton);
editorControlPanel.add(pasteButton);
JPanel specialPanel = new JPanel();
specialPanel.setLayout(new FlowLayout()); textPane = new JTextPane(document);
textPane.setContentType("text/html");
JScrollPane scrollPane = new JScrollPane(textPane);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension scrollPaneSize = new Dimension(5*screenSize.width/8,5*screenSize.height/8);
scrollPane.setPreferredSize(scrollPaneSize); //创建工具栏面板,并设置面板布局管理器,添加子面板
JPanel toolPanel = new JPanel();
toolPanel.setLayout(new BorderLayout());
toolPanel.add(editorControlPanel, BorderLayout.NORTH);
toolPanel.add(specialPanel, BorderLayout.CENTER); //向主窗体添加工具栏
getContentPane().add(toolPanel, BorderLayout.CENTER);
//向主窗体添加滚动面板
getContentPane().add(scrollPane, BorderLayout.SOUTH);
pack();
setLocationRelativeTo(null);
show();
} public void actionPerformed(ActionEvent ae)
{
String actionCommand = ae.getActionCommand();
if (debug)
{
int modifier = ae.getModifiers();
long when = ae.getWhen();
String parameter = ae.paramString();
System.out.println("actionCommand: " + actionCommand);
System.out.println("modifier: " + modifier);
System.out.println("when: " + when);
System.out.println("parameter: " + parameter);
}
} /**内部类:自定义继承WindowListener的侦听器FrameListener*/
class FrameListener extends WindowAdapter
{
/**处理点击窗体关闭按钮事件,实现程序的关闭停止*/
public void windowClosing(WindowEvent we)
{
}
}
}
{
/** 声明一个网页文档对象变量*/
private HTMLDocument document;
/** 创建一个文本编辑板*/
private JTextPane textPane = new JTextPane();
private boolean debug = false;
/** 声明一个文件对象变量*/
/** 添加剪切侦听器*/
private Action cutAction = new DefaultEditorKit.CutAction();
/** 添加复制侦听器*/
private Action copyAction = new DefaultEditorKit.CopyAction();
/** 添加粘贴侦听器*/
private Action pasteAction = new DefaultEditorKit.PasteAction();
/** 构造方法*/
public HTMLDocumentEditor()
{
/** 设置主窗体标题*/
super("HTMLDocumentEditor");
HTMLEditorKit editorKit = new HTMLEditorKit();
/** 创建默认文档指向网页引用document*/
document = (HTMLDocument)editorKit.createDefaultDocument(); // 强制SWINGSET实现跨平台,不改变风格
try
{
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
// 如果你想用系统的界面风格替代,请注释掉上一行代码,而取消下一行代码的注释:
//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception exc)
{
//产生异常,则显示错误消息:加载L&F错误
System.err.println("Error loading L&F: " + exc);
} //调用初始化方法
init();
} /**主方法,起动程序*/
public static void main(String[] args)
{
//创建一个类的实例,即创建一个网页编辑器
HTMLDocumentEditor editor = new HTMLDocumentEditor();
} /**初始化各组件的方法*/
public void init()
{
//调用自定义继承WindowListener的侦听器FrameListener,给主窗体添加WindowListener
addWindowListener(new FrameListener()); JPanel editorControlPanel = new JPanel();
//editorControlPanel.setLayout(new GridLayout(3,3));
editorControlPanel.setLayout(new FlowLayout()); /* 按钮 */
JButton cutButton = new JButton(cutAction); //创建“剪切”按钮,添加剪切侦听
JButton copyButton = new JButton(copyAction); //创建“复制”按钮,添加复制侦
JButton pasteButton = new JButton(pasteAction); //创建“粘贴”按钮,添加粘贴侦 cutButton.setText("Cut");
copyButton.setText("Copy");
pasteButton.setText("Paste");
editorControlPanel.add(cutButton);
editorControlPanel.add(copyButton);
editorControlPanel.add(pasteButton);
JPanel specialPanel = new JPanel();
specialPanel.setLayout(new FlowLayout()); textPane = new JTextPane(document);
textPane.setContentType("text/html");
JScrollPane scrollPane = new JScrollPane(textPane);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension scrollPaneSize = new Dimension(5*screenSize.width/8,5*screenSize.height/8);
scrollPane.setPreferredSize(scrollPaneSize); //创建工具栏面板,并设置面板布局管理器,添加子面板
JPanel toolPanel = new JPanel();
toolPanel.setLayout(new BorderLayout());
toolPanel.add(editorControlPanel, BorderLayout.NORTH);
toolPanel.add(specialPanel, BorderLayout.CENTER); //向主窗体添加工具栏
getContentPane().add(toolPanel, BorderLayout.CENTER);
//向主窗体添加滚动面板
getContentPane().add(scrollPane, BorderLayout.SOUTH);
pack();
setLocationRelativeTo(null);
show();
} public void actionPerformed(ActionEvent ae)
{
String actionCommand = ae.getActionCommand();
if (debug)
{
int modifier = ae.getModifiers();
long when = ae.getWhen();
String parameter = ae.paramString();
System.out.println("actionCommand: " + actionCommand);
System.out.println("modifier: " + modifier);
System.out.println("when: " + when);
System.out.println("parameter: " + parameter);
}
} /**内部类:自定义继承WindowListener的侦听器FrameListener*/
class FrameListener extends WindowAdapter
{
/**处理点击窗体关闭按钮事件,实现程序的关闭停止*/
public void windowClosing(WindowEvent we)
{
}
}
}
基本上系统捕获了enter之后是不会保存进去
不知道DefaultEditorKit怎么工作的 lz仔细查下吧