gpt4 book ai didi

java - 为 Jbutton 设置 ActionListener "class"

转载 作者:行者123 更新时间:2023-12-02 05:01:32 25 4
gpt4 key购买 nike

我正在为我的一款游戏重新制作菜单,我使用一种名为 addButton**(text, Image, pressedImage, container) 的方法来制作按钮。 。我的问题是我想使用 addActionListener方法并允许每个按钮都有自己的操作,但我无法这样做。有任何想法吗?

这是菜单代码:

public Menu(Component component) {

JFrame frame = new JFrame("CityBlock 2d Launcher");
frame.setSize(new Dimension(1050, 700));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(component);

// Set up the content pane.
try {
frame.setContentPane(new JLabel(new ImageIcon(ImageIO
.read(new File("res/menuBackground.png")))));
} catch (IOException e) {
e.printStackTrace();
}
addComponentsToPane(frame.getContentPane());

// Display the window.
frame.pack();
frame.setVisible(true);
}

public static void addComponentsToPane(Container pane) {
pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));

addAButton("SP", "res/Singleplayer.png",
"res/Singleplayer_pressed.png", pane);

addAButton("MP", "res/Multiplayer.png", "res/Multiplayer_Pressed.png",
pane);

addAButton("HTP", "res/HowToPlay.png", "res/HowToPlay_Pressed.png",
pane);

addAButton("O", "res/Options.png", "res/Options_Pressed.png", pane);

addAButton("Q", "res/Quit.png", "res/Quit_Pressed.png", pane);

}

private static void addAButton(final String text, String BtnIcon,
String PressBtnIcon, Container container) {

ImageIcon myImage = new ImageIcon(BtnIcon);

final JButton button = new JButton(/* myImage */text);

button.setIcon(myImage);

button.setOpaque(false);
button.setContentAreaFilled(false);
button.setBorderPainted(false);

button.setAlignmentX(Component.LEFT_ALIGNMENT);
button.setAlignmentY(Component.TOP_ALIGNMENT);

button.setPressedIcon(new ImageIcon(PressBtnIcon));
container.add(button);

button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (button.equals(text)) { <------ **Text** could probably be used to separate the different buttons????
// Execute when button is pressed
System.exit(0);
}
}

});
}
}

最佳答案

一种方法是将 ActionListener 传递给 addAButton(),即

private static void addAButton(final String text, String BtnIcon,
String PressBtnIcon, Container container, ActionListener actionListener) {

然后调用

addAButton("SP", "res/Singleplayer.png",
"res/Singleplayer_pressed.png", pane,
new ActionListener() {
public void actionPerformed(ActionEvent e) {
// do stuff
}
});

addAButton("MP", "res/Multiplayer.png", "res/Multiplayer_Pressed.png",, pane,
new ActionListener() {
public void actionPerformed(ActionEvent e) {
// do different stuff
}
});

另一种方法是完全拥抱 Swing 并使用 java.swing.Action,用它​​来定义图标、按钮文本等,然后使用 setAction() 在 JButton 上进行设置

JButton button = new JButton();
Action foo = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
// do stuff
}
};
foo.putValue(Action.NAME, "Button text");
foo.putValue(Action.LARGE_ICON_KEY, new ImageIcon("res/foo.png"));
button.setAction(foo);

关于java - 为 Jbutton 设置 ActionListener "class",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28245145/

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