private Component createContent() {
final Image image = requestImage();
JPanel panel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, null);
}
};
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
for (String label : new String[]{"BlackJack", "Poker", "Go-Fish"}) {
JButton button = new JButton(label);
button.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(Box.createRigidArea(new Dimension(15, 15)));
panel.add(button);
}
panel.setPreferredSize(new Dimension(500, 500));
return panel;
}
这是我使用的示例。我的其余代码仅显示窗口和背景图像。我正在尝试当用户单击按钮时图像和按钮以及按钮的位置。
最佳答案
嗯,您必须向按钮添加一些操作监听器,以调用它们触发的方法。
button.addActionListener(监听器)
作为 ActionListener 接口(interface)的监听器对象的实现(您必须定义 de actionPerfomed(ActionEvent e) 方法)
https://docs.oracle.com/javase/7/docs/api/java/awt/event/ActionListener.html
请记住,Java 中的 switch 语句只能与基元一起使用,并且从 JDK 7 开始只能使用字符串文字,但仅此而已。
希望这有帮助。
关于java - 我正在寻找一个 switch 语句来根据用户按下的按钮添加不同的页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60469300/