Ⅰ java中想要点击一个button然后jlabel上就显示出图片,button事件监听该怎么写。
给个例子你参考一下:给按钮bntOk添加监听事件
bntOk.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e1) {
label2= new JLabel();
frame.add(label2);
label2.setBounds(50, 50, 400, 200);
label2.setIcon(new ImageIcon("d:\love.JPG"));//可以换成任意你想要的图片
}
});
点击按钮前,是这样的
完整的程序如下:
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
import java.awt.*;
public class wuziqi extends JFrame{
private JPanel frame;
private JButton bntOk;
private JLabel label2;
private ImageIcon asdIcon = new ImageIcon("d:\love.JPG"); //换成你要显示的图片
public static void main(String[] args){
wuziqi mb=new wuziqi();
}
public wuziqi(){//构造函数,搭建显示界面
frame= new JPanel();
getContentPane().add(frame, BorderLayout.CENTER);
frame.setLayout(null);
frame.setBounds(100, 100, 576, 440);
this.setSize(500, 500);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("点击按钮,显示图片");
this.setVisible(true);
bntOk = new JButton();
frame.add(bntOk);
bntOk.setText("显示");
bntOk.setLocation(10, 10);
bntOk.setBounds(150, 300, 66, 30);
bntOk.addMouseListener(new MouseAdapter()//给按钮加上监听事件
{
public void mouseClicked(MouseEvent e1) {
label2= new JLabel();
frame.add(label2);
label2.setBounds(50, 50, 400, 200);
label2.setIcon(asdIcon);//label内容为图片
}
});
}
}
Ⅱ JAVA中如何给按钮做监听
你a[i][j].addActionListener(this); 这句就是给按钮加监听啊,你想给哪些按钮加就在哪些按钮上调用addActionListener(this);方法。
你的类还实现ActionListener接口,并补全actionPerformed方法,添加监听的方法才不会报错。
有问题的话再问,把问题描述的具体些。
Ⅲ java初学者,为什么我button中添加的监听器,点击按钮的时候是不能用的啊
你的ActionListener的实现类(假设叫MyAction)应该作为一个单独的私有类放在public class MyApplet里面,把你的actionPerformed方法放在里面:
public class MyApplet extends Applet
{
。。。
private class MyAction implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
。。。。
}
}
}
然后在主类的init方法里,首先创建MyAction的实例ma,然后用addActionListener方法将实例绑定到button
Ⅳ java如何监听所有按钮
先定义一个监听器al:
ActionListener al = new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
String str = btn1.getText();
field.setText(str);
}
};
然后所有button都调用addActionListener(al);这样al就可以监听所有button的点击事件了.通过e.getSource()获取哪个按钮,然后获取按钮上的数字即可.
Ⅳ JAVA按钮监听问题,为什么我点了按钮后没反应啊
目测你代码从拷贝的不全吧,,按你的先法你的类要实现ActionListener接口
implements ActionListener
然后所有buttion addActionListener(this);
后面的方法才生效
Ⅵ java button监听器
把这两句submitButton.addActionListener(null);
resetButton.addActionListener(null);
改成
submitButton.addActionListener(this);
resetButton.addActionListener(this);
看行不行。
Ⅶ JAVA添加按钮监听器
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class ShowButton implements ActionListener{
private JTextField textField = null;
private JButton button = null;
private JFrame frame = null;
public ShowButton(){
frame = new JFrame("显示按钮文字");
button = new JButton("3");
textField = new JTextField("",20);
frame.setLayout(new FlowLayout(FlowLayout.CENTER));
frame.add(textField);
button.addActionListener(this);
frame.add(button);
frame.setSize(400, 100);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
textField.setText("");
textField.setText(button.getActionCommand());
System.out.println(button.getActionCommand()+"===");
}
public static void main(String[] args) {
new ShowButton();
}
}
Ⅷ java Jbutton时间监听问题
button[i][j].setBounds(i*len,j*len,len,len);
这行的问题吧,改成setBounds(j*len, i*len, len,len);试试,我没验证
问题可能是你放按钮的时候不是一行一行放的,而是一列一列的
Ⅸ java中按钮监听处理问题
//定义的按钮,要全局的变量
private JButton submit= new JButton("确定");
private JButton cancle = new JButton("取消");
...
监听的方法
public void actionPerformed(ActionEvent e) {
//获取事件源
JButton button ==(JButton)e.getSource());
if(button == submit){
//点击的是确定按钮
}
if(button == cancle ){
//点击的是取消按钮
}
}
e.getSource()是获取事件的源,就是从哪里发生的点击事件。
这样应该可以处理你的问题吧
Ⅹ Java按钮监听
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.math.BigDecimal;
importjava.util.Scanner;
importjavax.swing.JFrame;
importjava.awt.Button;
importjava.awt.Label;
importjava.awt.TextField;
importjava.awt.Frame;
importjava.awt.Panel;
importjava.awt.Color;
importjava.awt.*;
publicclassPanelTest{
publicstaticvoidmain(Stringargs[]){
/*Scannersc=newScanner(System.in);doublepi=3.14,s;doubler;r=sc.nextDouble();s=pi*r*r;System.out.println("s等于"+s);*/
EventQueue.invokeLater(newRunnable(){
publicvoidrun(){
CricleFrameframe=newCricleFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
classCricleFrameextendsJFrame{
Panelp=newPanel();
TextFieldt=newTextField();
Buttonb=newButton("确定");
Labela=newLabel("请在此输入半径");
TextFieldresult=newTextField();
publicCricleFrame(){
add(a);
add(t);
add(b);
add(result);
add(p);
setVisible(true);
p.setBackground(Color.black);
a.setBackground(Color.yellow);
t.setBackground(Color.white);
result.setBackground(Color.white);
b.setBackground(Color.cyan);
setSize(300,300);
setTitle("圆的面积");
a.setBounds(105,45,90,25);
t.setBounds(100,80,100,25);
result.setBounds(100,180,100,25);
b.setBounds(111,120,80,40);
b.addActionListener(newActionListener(){//按钮点击事件监听
publicvoidactionPerformed(ActionEventevent){
Doubler=0.0;
try{
r=Double.parseDouble(t.getText());
}catch(Exceptione){
System.out.println(e.getMessage());
}
BigDecimaltmp=newBigDecimal(r*r*Math.PI);
Doublearea=tmp.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();//保留2位小数
result.setText(""+area);
}
});
}
}
在你的基础上改了一下,界面什么的没有改