站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2SE 专题按钮的actionPerformed问题

按钮的actionPerformed问题

摘要: 按钮的actionPerformed问题


小弟刚接触Swing,问的问题可能比较弱,还请大家不吝赐教。
我想实现的功能是:在panel上有四个按钮,每点击一个按钮就在panel上重新绘制一张指定的美女图片。
现在的状况是:在点击按钮前,panel上已经把最后声明的BgAction oneAction = new BgAction(-#34;330618.jpg-#34;)图片显示出来了,然后再点击按钮,没有任何反映。

class getPanel extends JPanel{
public getPanel(){
JButton oneButton = new JButton(-#34;one-#34;);
JButton twoButton = new JButton(-#34;two-#34;);
JButton threeButton = new JButton(-#34;three-#34;);
JButton fourButton = new JButton(-#34;four-#34;);

add(oneButton);
add(twoButton);
add(threeButton);
add(fourButton);

BgAction twoAction = new BgAction(-#34;330620.jpg-#34;);
BgAction fourAction = new BgAction(-#34;0001711786.jpg-#34;);
BgAction threeAction = new BgAction(-#34;330621.jpg-#34;);
BgAction oneAction = new BgAction(-#34;330618.jpg-#34;);

oneButton.addActionListener(oneAction);
twoButton.addActionListener(twoAction);
threeButton.addActionListener(threeAction);
fourButton.addActionListener(fourAction);
}


public void paintComponent(Graphics g){
super.paintComponent(g);
try{
Image image = ImageIO.read(new File(imgURL));
int w=image.getWidth(this),h=image.getHeight(this);
if(w-#62;getWidth())w=getWidth();
if(h-#62;getHeight())h=getHeight();

g.drawImage(image,0,0,w,h,null);

}catch(Exception e){}

}

private class BgAction implements ActionListener{
public BgAction(String name){
imgURL = -#34;./ico/-#34;+name;

}

public void actionPerformed(ActionEvent event){
//g.drawImage(image,0,0,null);
//setBackground(Color.BLUE);
//repaint();
/*try{

Image image = ImageIO.read(new File(imgURL));
Graphics gg = getGraphics();


gg.drawImage(image,0,0,null);

}catch(Exception e){}
*/


//Graphics gg=getGraphics();
repaint();



}

private Image img;


}
private String imgURL;
}


PS:昨天在Eclipse板块发的帖子,今天有人指出我发错了板块。故转到这里来.......


点击按钮后,按钮是事件源,产生一个ActionEvent类型的对象e,传递给监听器,执行actionPerformed(e),问题就出在你把 imgURL初始化放在了public BgAction(String name)中,而事件源产生的是ActionEvent类型的对象e,点击按钮后根本不会去执行构造器BgAction(String name),所以imgURL的值不会变


我自己按你的意思写了一个,可以运行,请指教
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.imageio.*;
import java.io.*;


public class DrawBackgound
{
public static void main(String[] args)
{
DrawBackgoundFrame aFrame = new DrawBackgoundFrame();
aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
aFrame.setVisible(true);
}
}


class DrawBackgoundFrame extends JFrame
{
public DrawBackgoundFrame()
{
setTitle(-#34;欣赏美女-#34;);
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension d = kit.getScreenSize();
int screenWidth = d.width;
int screenHeight = d.height;
setBounds(0,0,screenWidth,screenHeight);

DrawBackgoundPanel aPanel = new DrawBackgoundPanel();
getContentPane().add(aPanel);
}
}


class DrawBackgoundPanel extends JPanel
{
public DrawBackgoundPanel()
{
addButton(-#34;one-#34;, -#34;1.jpg-#34;);
addButton(-#34;two-#34;, -#34;2.jpg-#34;);
addButton(-#34;three-#34;, -#34;3.jpg-#34;);
addButton(-#34;four-#34;, -#34;4.jpg-#34;);
}


private void addButton(String buttonLabel, final String imageLocation)
{
JButton aButton = new JButton(buttonLabel);
add(aButton);
aButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
imgURL = imageLocation;
repaint();
}
});
}

public void paintComponent(Graphics g)
{
super.paintComponent(g);
try{
Image image = ImageIO.read(new File(imgURL));
int w=image.getWidth(this),h=image.getHeight(this);
if(w-#62;getWidth())w=getWidth();
if(h-#62;getHeight())h=getHeight();

g.drawImage(image,0,0,w,h,null);

}catch(Exception e){}

}

private String imgURL ;
}




多谢weijieut() 大大
不光人好,指出了我的错误,还附加说明错误的原因,更附上正确的程序
且人还很谦虚
呵呵,敬礼#


↑返回目录
前一篇: 新人提问,想了2天没想通的问题【处子贴】
后一篇: 如何在一个JDesktopPane 只能存在一个JInternalFrame?