我需要将JOptionPaneMessageDialog置于e.getSource的父元素中。问题是我有两个不同的类。一个侦听器类和另一个使用该侦听器的类 这是我需要改变的界限
JOptionPane.showMessageDialog((Component)e.getSource(), "pulsado");
package excepciones;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
class ButtonListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog((Component)e.getSource(), "pulsado");
}
}
public class UseActionListener {
public static void main(String[] args) {
JFrame frame = new JFrame("Popup JComboBox");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton source = new JButton("Ring the bell!");
source.addActionListener(new ButtonListener());
frame.add(source, BorderLayout.SOUTH);
source.addActionListener(new ButtonListener());
frame.setSize(300, 200);
frame.setVisible(true);
}
}
答案 0 :(得分:2)
JOptionPane.showMessageDialog(((Component) e.getSource()).getParent(), "pulsado")
;
答案 1 :(得分:1)
您是否尝试通过setLocation(x, y)
方法设置位置?查看How to set the location of “JOptionPane.showMessageDialog”