ActionListener listener = ...;
JButton button = new JButton("OK");
button.addActionListener(listener);
class MyListener implements ActionListener
{
...;
public void actionPerformed(ActionEvent event)
{
//reaction to button click goes here
}
}
JButton yellowButton = new JButton("Yellow");
JButton blueButton = new JButton(new ImageIcon("blue-ball.gif"));
JButton yellowButton = new JButton("Yellow");
JButton blueButton = new JButton("Blue");
JButton redButton = new JButton("Red");
buttonPanel.add(yellowButton);
buttonPanel.add(blueButton);
buttonPanel.add(redButton);
class ColorAction implements ActionListener
{
public ColorAction(Color c)
{
backgroundColor = c;
}
public void actionPerformed(actionEvent event)
{
//set panel background color
}
private Color backgroundColor;
}
ColorAction yelloAction = new ColorAction(Color.YELLOW); ColorAction blueAction = new ColorAction(Color.BLUE); ColorAction redAction = new ColorAction(Color.RED); yellowButton.addActionListener(yellowAction); blueButton.addActionListener(blueAction); redButton.addActionListener(redAction);
class ButtonFrame extends JFrame
{
...
private class ColorAction implents ActionListener
{
...
public void actionPerformed(ActionEvent event)
{
buttonPanel.setBackground(backgroundColor);
}
private Color backgroundColor;
}
private Jpanel buttonPanel;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有