gpt4 book ai didi

java - 如何将第二个 Action 监听器放入对象中?

转载 作者:行者123 更新时间:2023-12-02 08:51:27 25 4
gpt4 key购买 nike

目前,我正在尝试创建一个具有菜单栏和弹出菜单的记事本。问题是我不能在构造函数中有 2 个 Action 监听器。我知道我必须在对象中创建第二个 Action 监听器,但是,我不确定如何创建。我标记了遇到错误的地方。预先感谢您!

我收到的错误消息如下:不兼容的类型:无法转换为操作监听器 - 在构造函数中泄漏 this

public Notepad1)
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

String Mlabs[] = { "File", "Edit", "Format", "View" };
String Elabs0[] = { "New", "Open", "Save", "Save as...", "Exit" };
String Elabs1[] = { "Select", "Copy", "Paste"};
String Elabs2[] = { "Word", "Zoom"};
String Elabs3[] = { "View Help", "About Notepad"};

JMenuBar jmb = new JMenuBar();

JMenu jm1 = new JMenu(Mlabs[0]);
jm1.setMnemonic(Mlabs[0].charAt(0));
for (int j=0; j<Elabs0.length; j++)
{
JMenuItem jmi = new JMenuItem(Elabs0[j]);
jm1.add(jmi);

}
jmb.add(jm1);

JMenu jm2 = new JMenu(Mlabs[1]);
jm2.setMnemonic(Mlabs[1].charAt(1));
for (int j=0; j<Elabs1.length; j++)
{
JMenuItem jmi = new JMenuItem(Elabs1[j]);
jm2.add(jmi);
jmi.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
JComponent jc = (JComponent)ae.getSource();
if(ae.getActionCommand().equals("Select"))
{
jta.selectAll();
}
if(ae.getActionCommand().equals("Copy"))
{
copied = jta.getSelectedText();
}
if(ae.getActionCommand().equals("Paste"))
{
jta.setText(jta.getText() + copied);
}
}
});
}
jmb.add(jm2);

JMenu jm3 = new JMenu(Mlabs[2]);
jm3.setMnemonic(Mlabs[2].charAt(2));
for (int j=0; j<Elabs2.length; j++)
{
JMenuItem jmi = new JMenuItem(Elabs2[j]);
jm3.add(jmi);
}
jm3.addSeparator();
jmb.add(jm3);

JMenu jm4 = new JMenu(Mlabs[3]);
jm4.setMnemonic(Mlabs[3].charAt(3));
for (int j=0; j<Elabs3.length; j++)
{
JMenuItem jmi = new JMenuItem(Elabs3[j]);
jm4.add(jmi);
}
jmb.add(jm4);

frame.setJMenuBar(jmb);

JComponent comp = (JComponent)frame.getContentPane();
comp.setLayout(new GridLayout(1, 1));
jta.setFont(new Font("TimesRoman", Font.BOLD, 18));
comp.add(jta);

jpm = new JPopupMenu();
String miLab[] = { "Select", "Copy", "Paste" };
for (int i=0; i<miLab.length; i++)
{
JMenuItem jmi = new JMenuItem(miLab[i]);
jmi.setActionCommand(miLab[i]);
jmi.addActionListener(this); // <-- ERROR 1
jpm.add(jmi);
}

JMenu newElem = new JMenu("Edit");
for (int i=0; i<miLab.length; i++)
{
JMenuItem item = new JMenuItem("rozwijka "+miLab[i]);
newElem.add(item);
item.addActionListener(this); // <-- ERROR 2
}
jpm.add(newElem);

frame.setSize(1000,500);
frame.setVisible(true);
}


MouseAdapter ma = new MouseAdapter()
{
public void mouseReleased(MouseEvent me)
{
if(me.isPopupTrigger())
{
compMenu = me.getComponent();
jpm.show( compMenu, me.getX(), me.getY() );
}
}
};

最佳答案

    item.addActionListener(this); // <-- ERROR 2

就像你做另一件事一样:

    item.addActionListener(new Actionlistener() {
public void actionPerformed(ActionEvent ae) {
//...;
}
});

关于java - 如何将第二个 Action 监听器放入对象中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60740697/

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