public class GlassActionListener{
JButton first;
JButton second;
@Override
public void actionPerformed(ActionEvent e) {
if(first == null)
{
first = (JButton) e.getSource();
}
else
{
second = (JButton) e.getSource();
// do something
// clean up
first = null;
second = null;
}
}
}
public class GUIControl {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
showGUI();
}
});
}
public static void showGUI() {
JFrame frame = new JFrame("MyFrame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
JButton glass1 = new JButton("glass1");
JButton glass2 = new JButton("glass2");
JButton glass3 = new JButton("glass3");
frame.getContentPane().add(glass1);
frame.getContentPane().add(glass2);
frame.getContentPane().add(glass3);
frame.pack();
frame.setVisible(true);
glass1.addActionListener(??);
glass2.addActionListener(??);
glass3.addActionListener(??);
// I want to store the first two clicks in these variables
from = "glassX";
to = "glassY";
puzzle.move(from, to);
}
使用者会把苏打水洒在玻璃杯之间。用户在三个可用按钮中单击的第一个J按钮确定"从"初始玻璃,用户在三个中单击的第二个J按钮决定"到"目标玻璃。
如何确定用户单击的特定两个JButton?每次用户单击三个JButton中的两个,我都希望将与这两个JButtons相关的字符串存储在"from"one_answers"to"变量中。
有办法做到这一点吗?
创建一个ActionListener
并使用JButton#addActionListener
将其添加到所有三个按钮中。事件有一个可以使用的源字段。
ActionListener listener = new ActionListener() {
JButton first;
JButton second;
@Override
public void actionPerformed(ActionEvent e) {
if(first == null)
{
first = (JButton) e.getSource();
}
else
{
second = (JButton) e.getSource();
// do something
// clean up
first = null;
second = null;
}
}
};
glass1.addActionListener(listener);
glass2.addActionListener(listener);
glass3.addActionListener(listener);
处理监听器内部的逻辑有点脏,但这就是想法。
firstbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//do something here first button is clicked
}
} );
您可以向这样的按钮添加监听器。并使用类变量来跟踪按钮被单击的次数。
例如:
public class GUIControl {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
showGUI();
}
});
}
public static void showGUI() {
JFrame frame = new JFrame("MyFrame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
JButton glass1 = new JButton("glass1");
JButton glass2 = new JButton("glass2");
JButton glass3 = new JButton("glass3");
String from = "";
String to = "";
int click_count = 0;
glass1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(click_count ==0)
{
from = "glassX";
click_count = 1;
}else if(click_count ==1)
{
from = "glassY";
click_count = 0;
}
}
} );
glass2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(click_count ==0)
{
from = "glassX";
click_count = 1;
}else if(click_count ==1)
{
from = "glassY";
click_count = 0;
}
}
} );
glass3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(click_count ==0)
{
from = "glassX";
click_count = 1;
}else if(click_count ==1)
{
from = "glassY";
click_count = 0;
}
}
} );
frame.getContentPane().add(glass1);
frame.getContentPane().add(glass2);
frame.getContentPane().add(glass3);
frame.pack();
frame.setVisible(true);
// I want to store the first two clicks in these variables
}