Java ActionListener(actionPerformed)返回什么?
我目前正在努力学习Java的新知识,并决定开始GUI编程。 我用ActionListener和“actionPerformed”方法创建了一个GUI 我的问题是,我是否可以从这个方法(actionPerformed)返回任何东西,它将在哪里着陆?因为这个方法是在我在这个GUI中执行特定操作时调用的 或者我如何给这个actionPerformed方法一个其他参数Java ActionListener(actionPerformed)返回什么?,java,swing,user-interface,methods,actionlistener,Java,Swing,User Interface,Methods,Actionlistener,我目前正在努力学习Java的新知识,并决定开始GUI编程。 我用ActionListener和“actionPerformed”方法创建了一个GUI 我的问题是,我是否可以从这个方法(actionPerformed)返回任何东西,它将在哪里着陆?因为这个方法是在我在这个GUI中执行特定操作时调用的 或者我如何给这个actionPerformed方法一个其他参数 提前感谢;) 您可能已经知道,actionPerformed方法被声明为返回void或nothing,因此不,您不能从中返回任何内容,但
提前感谢;) 您可能已经知道,actionPerformed方法被声明为返回
void
或nothing,因此不,您不能从中返回任何内容,但您可以更改此方法范围内任何可变字段的状态,例如JLabel中显示的文本
例如:
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class ActionListenerTest extends JPanel {
private static final String[] DAYS = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
private int dayIndex = 0;
private JLabel label = new JLabel("", SwingConstants.CENTER);
private JButton button = new JButton("Press Me Please!");
public ActionListenerTest() {
label.setText(DAYS[dayIndex]);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dayIndex++; // advance the index
dayIndex %= DAYS.length; // if index >= the length of the array, make it 0
label.setText(DAYS[dayIndex]);
}
});
setLayout(new GridLayout(2, 1));
add(label);
add(button);
}
private static void createAndShowGui() {
ActionListenerTest mainPanel = new ActionListenerTest();
JFrame frame = new JFrame("ActionListenerTest");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
private Map<String,Object> data = ...;
public void showElements(){
...
button1.addActionListener (new ActionListener()
{
public void actionPerformed (ActionEvent e){
Object info = "Somebody clicked on my button!"//or you can use ActionEvent to extract mode information
data.put("button1",info)
}
})
}
actionPerformed不返回任何内容,但会将dayIndex提前1,如果索引与字符串数组的长度相同,则将其设置为0。然后,该方法使用字符串数组数据设置JLabel的文本。您可能已经知道,actionPerformed方法被声明为返回
void
或nothing,因此不,您不能从中返回任何内容,但您可以更改此方法范围内任何可变字段的状态,例如,JLabel中显示的文本
例如:
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class ActionListenerTest extends JPanel {
private static final String[] DAYS = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
private int dayIndex = 0;
private JLabel label = new JLabel("", SwingConstants.CENTER);
private JButton button = new JButton("Press Me Please!");
public ActionListenerTest() {
label.setText(DAYS[dayIndex]);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dayIndex++; // advance the index
dayIndex %= DAYS.length; // if index >= the length of the array, make it 0
label.setText(DAYS[dayIndex]);
}
});
setLayout(new GridLayout(2, 1));
add(label);
add(button);
}
private static void createAndShowGui() {
ActionListenerTest mainPanel = new ActionListenerTest();
JFrame frame = new JFrame("ActionListenerTest");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
private Map<String,Object> data = ...;
public void showElements(){
...
button1.addActionListener (new ActionListener()
{
public void actionPerformed (ActionEvent e){
Object info = "Somebody clicked on my button!"//or you can use ActionEvent to extract mode information
data.put("button1",info)
}
})
}
actionPerformed不返回任何内容,但会将dayIndex提前1,如果索引与字符串数组的长度相同,则将其设置为0。然后,该方法使用字符串数组数据设置JLabel的文本。没有actionPerformed是回调,因此您可以通过实现actionPerformed方法来处理操作事件。在这里您可以: 1) 更新应用程序中的一些GUI元素 2) 将结果数据保存在视图类的私有字段中,以便将来处理 例如:
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class ActionListenerTest extends JPanel {
private static final String[] DAYS = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
private int dayIndex = 0;
private JLabel label = new JLabel("", SwingConstants.CENTER);
private JButton button = new JButton("Press Me Please!");
public ActionListenerTest() {
label.setText(DAYS[dayIndex]);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dayIndex++; // advance the index
dayIndex %= DAYS.length; // if index >= the length of the array, make it 0
label.setText(DAYS[dayIndex]);
}
});
setLayout(new GridLayout(2, 1));
add(label);
add(button);
}
private static void createAndShowGui() {
ActionListenerTest mainPanel = new ActionListenerTest();
JFrame frame = new JFrame("ActionListenerTest");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
private Map<String,Object> data = ...;
public void showElements(){
...
button1.addActionListener (new ActionListener()
{
public void actionPerformed (ActionEvent e){
Object info = "Somebody clicked on my button!"//or you can use ActionEvent to extract mode information
data.put("button1",info)
}
})
}
私有地图数据=。。。;
公共元素(){
...
button1.addActionListener(新建ActionListener())
{
已执行的公共无效操作(操作事件e){
Object info=“有人点击了我的按钮!”//或者您可以使用ActionEvent提取模式信息
数据输入(“按钮1”,信息)
}
})
}
没有actionPerformed是回调,所以您可以通过实现actionPerformed方法来处理操作事件。在这里您可以:
1) 更新应用程序中的一些GUI元素
2) 将结果数据保存在视图类的私有字段中,以便将来处理
例如:
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class ActionListenerTest extends JPanel {
private static final String[] DAYS = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
private int dayIndex = 0;
private JLabel label = new JLabel("", SwingConstants.CENTER);
private JButton button = new JButton("Press Me Please!");
public ActionListenerTest() {
label.setText(DAYS[dayIndex]);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dayIndex++; // advance the index
dayIndex %= DAYS.length; // if index >= the length of the array, make it 0
label.setText(DAYS[dayIndex]);
}
});
setLayout(new GridLayout(2, 1));
add(label);
add(button);
}
private static void createAndShowGui() {
ActionListenerTest mainPanel = new ActionListenerTest();
JFrame frame = new JFrame("ActionListenerTest");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
private Map<String,Object> data = ...;
public void showElements(){
...
button1.addActionListener (new ActionListener()
{
public void actionPerformed (ActionEvent e){
Object info = "Somebody clicked on my button!"//or you can use ActionEvent to extract mode information
data.put("button1",info)
}
})
}
私有地图数据=。。。;
公共元素(){
...
button1.addActionListener(新建ActionListener())
{
已执行的公共无效操作(操作事件e){
Object info=“有人点击了我的按钮!”//或者您可以使用ActionEvent提取模式信息
数据输入(“按钮1”,信息)
}
})
}