Ⅰ 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);
}
});
}
}
在你的基礎上改了一下,界面什麼的沒有改