import java.awt.BorderLayout;
public class text2 extends JFrame {
private JPanel contentPane;
/** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { text2 frame = new text2(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); }
/** * Create the frame. */ public text2() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPane.setLayout(new BorderLayout(0, 0)); setContentPane(contentPane); JScrollPane scrollPane = new JScrollPane(); contentPane.add(scrollPane, BorderLayout.CENTER); final JTextArea textArea = new JTextArea(); scrollPane.setViewportView(textArea); JMenuBar menuBar = new JMenuBar(); scrollPane.setColumnHeaderView(menuBar); JMenu menu = new JMenu("\u7F16\u8F91"); menuBar.add(menu); JMenuItem mntmCopy = new JMenuItem("copy"); mntmCopy.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textArea.copy(); } }); menu.add(mntmCopy); JMenuItem mntmPaste = new JMenuItem("paste"); mntmPaste.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textArea.paste(); } }); menu.add(mntmPaste); JMenuItem mntmCut = new JMenuItem("cut"); mntmCut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {//mouseclick textArea.cut(); } }); menu.add(mntmCut); }
}
這是改好的,原來的監聽事件為mouseclick,可是就是不起作用,不知原因,請教幫助解答。
類似處的事件都是這樣的:
JMenuItem mntmCut = new JMenuItem("cut"); mntmCut.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { text.cut(); } }); menu.add(mntmCut); 結果觸發不成功,但是想知道原因。。。。。。。。。。。
1 回答

Smart貓小萌
TA貢獻1911條經驗 獲得超7個贊
我猜,MouseEvent是比ActionEvent更低級的事件,后者是在前者的基礎上實現的,控件捕獲低級鼠標事件(以檢測高級ActionEvent?),處理之后就不再觸發鼠標事件的監聽器了。
我又猜,如果你監聽一下鼠標按鍵按下的事件(mousepressed?方法名不記得,現在手機打字,不方便看javadoc),極有可能被調用到。
另外有一個信息,可能對你有用:mouseevent類或者它的某個父類,有一個方法是阻止事件繼續被派發(到其他監聽器),我想你的疑問,答案或許和這個方法有關,也就是這個方法被控件類內部實現調用過了。我現在手機打字,不方便查看apidoc,方法名不記得,不過你自己應該可以找到:-)
添加回答
舉報
0/150
提交
取消