记事本的全选功能怎么实现?为什么只有MenuItem有效?按钮却不行?
我这里只有JMenuItem有效果,按钮All不行 ,但是他们都是一个监听器
//MyMemo.java
import java.awt.BorderLayout;
import javax.swing.*;
import java.awt.event.*;
public class MyMemo extends JFrame {
public static JEditorPane editor = new JEditorPane();
public static JButton btn = new JButton("All");
public static JMenu menuEdit = new JMenu("Edit");
public static JMenuItem itemAll = new JMenuItem("All");
JMenuBar mb = new JMenuBar();
public MyMemo() {
btn.addActionListener(new Find_ActionListener());
itemAll.addActionListener(new Find_ActionListener());
menuEdit.add(itemAll);
mb.add(menuEdit);
setJMenuBar(mb);
JPanel p = new JPanel();
p.add(btn);
add(p, BorderLayout.NORTH);
add(editor);
}
public static void main(String[] args) {
MyMemo memo = new MyMemo();
memo.setBounds(300, 300, 300, 300);
memo.setVisible(true);
}
}
class Find_ActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("all");
MyMemo.editor.setSelectionStart(0);
MyMemo.editor.setSelectionEnd(MyMemo.editor.getText().length());
}
}
------解决方案--------------------------------------------------------
可以,我试过了