gpt4 book ai didi

java - Swing 撤消选项

转载 作者:行者123 更新时间:2023-12-01 10:21:08 25 4
gpt4 key购买 nike

我一直在观看有关撤消/重做选项的教程用于 Java Swing GUI。我不明白我做错了什么。以下代码是另一个类中的内部类。提交按钮工作正常。单击撤消选项时不会撤消。有人可以让我知道我做错了什么吗?谢谢。

private class listen implements ActionListener
{
UndoManager manager;
Document doc;
@Override
public void actionPerformed( ActionEvent e )
{
int result;
Calendar times = Calendar.getInstance();
createFile file = new createFile();
manager = new UndoManager();
doc = area.getDocument();
String output_1, output_2, output_3, fileName;
search find = new search();
JPanel panel = new JPanel();
JTextField input_1 = new JTextField(15);
JTextField input_2 = new JTextField(15);
JTextField input_3 = new JTextField(15);
JLabel word_1 = new JLabel("KeyWord:");
JLabel word_2 = new JLabel("KeyWord:");
JLabel word_3 = new JLabel("KeyWord:");
panel.add(word_1);
panel.add(input_1);
panel.add(Box.createHorizontalStrut(20));
panel.add(word_2);
panel.add(input_2);
panel.add(Box.createHorizontalStrut(20));
panel.add(word_3);
panel.add(input_3);

if(e.getSource() == submit)
{
do
{
result = JOptionPane.showConfirmDialog(null, panel,
"KeyWords", JOptionPane.OK_CANCEL_OPTION);
output_1 = input_1.getText();
output_2 = input_2.getText();
output_3 = input_3.getText();
if( result == JOptionPane.OK_OPTION
&& !find.isValidWord(output_1, output_2, output_3) )
{
JOptionPane.showMessageDialog(null, "<html>You must "
+ "enter atleast 1 KeyWord, and also all<br> "
+ "valid KeyWords must contain atleast one "
+ "alphabetic letter,</html>");
}
}
while(result == JOptionPane.OK_OPTION &&
!find.isValidWord(output_1, output_2, output_3));
if(result == JOptionPane.OK_OPTION)
{
fileName = times.get(Calendar.MONTH) + "_" +
times.get(Calendar.DAY_OF_MONTH + 1) + "_" +
times.get(Calendar.YEAR) + "_" + output_1 + "_" +
output_2 + "_" + output_3 + "_";
file.file(fileName, area.getText());
}
}
else if(e.getSource() == undo)
{
doc.addUndoableEditListener( new UndoableEditListener()
{
@Override
public void undoableEditHappened( UndoableEditEvent e1 )
{
manager.addEdit(e1.getEdit());
update();
}
});
try
{
if( manager.canUndo() )
manager.undo();
} catch(CannotUndoException exp) {}
}
}
public void update()
{
undo.setEnabled(manager.canUndo());
}
}

最佳答案

I have been watching tutorials -

不用观看教程,而是从可从 Implementing Undo/Redo 上的 Swing 教程下载的工作代码开始。 .

我猜你需要在创建文本组件时将 UndoableEditListener 添加到文档中,而不是在 ActionListener 中。否则,在添加/删除文本时不会生成编辑。

关于java - Swing 撤消选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35613124/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com