Java 从不同的ActionListener对同一对象/列表执行操作
我有这个GUI设置。tl;dr is:它是一个gui,一个菜单项调用myActionListener 这个类中还有一个对象o。 我希望myActionListener以及myActionListener2等可以访问此对象。。 但我甚至不能调用任何对象方法Java 从不同的ActionListener对同一对象/列表执行操作,java,swing,user-interface,Java,Swing,User Interface,我有这个GUI设置。tl;dr is:它是一个gui,一个菜单项调用myActionListener 这个类中还有一个对象o。 我希望myActionListener以及myActionListener2等可以访问此对象。。 但我甚至不能调用任何对象方法 public class MenuDemo implements ActionListener,ItemListener{ // My Object Object o = new Object(); o.ad
public class MenuDemo implements ActionListener,ItemListener{
// My Object
Object o = new Object();
o.addParam();// wont work
public JMenuBar createMenuBar() {
JMenuBar menuBar;
JMenu menu, submenu;
JMenuItem menuItem;
menu = new JMenu("A Menu");
menuBar.add(menu);
menuItem = new JMenuItem("Title");
menuItem.addActionListener(new myListener());
menu.add(menuItem);
return menuBar;
}
public Container createContentPane() {
//Create the content-pane-to-be.
JPanel contentPane = new JPanel(new BorderLayout());
contentPane.setOpaque(true);
//Create a scrolled text area.
output = new JTextArea(5, 30);
output.setEditable(false);
scrollPane = new JScrollPane(output);
//Add the text area to the content pane.
contentPane.add(scrollPane, BorderLayout.CENTER);
return contentPane;
}
public Container createContentPane() {
//Create the content-pane-to-be.
JPanel contentPane = new JPanel(new BorderLayout());
contentPane.setOpaque(true);
//Create a scrolled text area.
output = new JTextArea(5, 30);
output.setEditable(false);
scrollPane = new JScrollPane(output);
//Add the text area to the content pane.
contentPane.add(scrollPane, BorderLayout.CENTER);
return contentPane;
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
Database db = new Database();
final int test = 5;
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
我想弄明白这一点已经有一段时间了,但似乎我找不到任何帮助,或者我可能用了错误的方法 正如我在评论中所说,您可以将
对象的引用传递给ActionListener
类型/子类型的不同对象:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
public class JButtonListener implements ActionListener
{
private Object _obj;
public JButtonListener(Object obj)
{
_obj = obj;
}
@Override
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null, _obj.toString());
}
}
主要类别:
import java.awt.EventQueue;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Main extends JFrame
{
private JButton btn;
Object o;
public Main()
{
setLayout(new FlowLayout());
o = new String("Hello Beautiful!");
btn = new JButton("Click!");
//Passing the reference `o` to the constructor
btn.addActionListener(new JButtonListener(o));
add(btn);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
pack();
setLocationRelativeTo(null);
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable() {
public void run()
{
new Test1();
}
});
}
}
实现ActionListener
的类:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
public class JButtonListener implements ActionListener
{
private Object _obj;
public JButtonListener(Object obj)
{
_obj = obj;
}
@Override
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null, _obj.toString());
}
}
将其引用作为参数传递给myListener(对象o),或使myListener成为内部类java.lang.Object
没有addParam()
方法。为什么要编译它?不能在静态块或初始化器块的方法外调用对象方法。。。