Java 嵌套操作已执行()
我正在设计一个程序,允许用户预订飞机航班。当程序第一次运行时,会打开一个带有两个按钮的JFrame。根据单击的按钮,actionPerformed会打开新的JPanel等 我试图在JTabbedPane中创建一个选项卡,该选项卡将显示两个按钮。单击任何一个按钮都会产生不同的图像,但这部分代码已经在actionPerformed方法中。我怎样才能知道单击了哪个按钮 这是我使用的方法:Java 嵌套操作已执行(),java,image,swing,button,user-interface,Java,Image,Swing,Button,User Interface,我正在设计一个程序,允许用户预订飞机航班。当程序第一次运行时,会打开一个带有两个按钮的JFrame。根据单击的按钮,actionPerformed会打开新的JPanel等 我试图在JTabbedPane中创建一个选项卡,该选项卡将显示两个按钮。单击任何一个按钮都会产生不同的图像,但这部分代码已经在actionPerformed方法中。我怎样才能知道单击了哪个按钮 这是我使用的方法: protected JComponent makeImagePanel(String path1, String
protected JComponent makeImagePanel(String path1, String path2)
{
try{
JPanel panel= new JPanel(false);
JButton international= new JButton("International Flights");
JButton domestic= new JButton("Domestic Flights");
international.setActionCommand("login");
domestic.setActionCommand("domestic");
international.setEnabled(true);
international.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
BufferedImage myPicture = ImageIO.read(new File(path1));
JLabel picLabel = new JLabel(new ImageIcon( myPicture ));
}
});
domestic.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
BufferedImage myPicture = ImageIO.read(new File(path2));
JLabel picLabel = new JLabel(new ImageIcon( myPicture ));
}
});
这就是我试图实现它的地方:
JComponent reservation= makeImagePanel("international_1.gif", "domestic_seating.gif");
overview.addTab ("Reserve a Flight", reservation);
overview.setMnemonicAt(1, KeyEvent.VK_2);
提前感谢您的帮助 ActionEvent有一个方法getSource(),它将为您提供触发事件的对象。或者,您可以设置全局变量以确定单击了哪个按钮。有很多方法可以做到这一点 问候,,
拉维谢谢!这帮了大忙:)