2014-05-14 28 views
0

我已經得到了一些幫助,但現在當我按下按鈕時,什麼也沒有發生。我想要單擊按鈕時顯示文本。如果是佈局問題,我應該使用哪一個? FlowLayout不適用於此程序,因爲它會扭曲按鈕。在ActionEvent中使用多個按鈕

import java.awt.*; 
import java.awt.event.*; 

public class Option3 extends Frame implements WindowListener,ActionListener 
{ 
    Label l1; 
    Label l2; 
    Label l3; 
    Button b1; 
    Button b2; 
    Button b3; 
    public static void main(String[] args) 
    { 
     Option3 o3 = new Option3("Press a Button"); 
     o3.setSize(700,350); 
     o3.setVisible(true); 
    } 
    public Option3(String option3) 
    { 
     super(option3); 
     setLayout(null); 
     addWindowListener(this); 

     Label l1 = new Label(); 
     l1 = new Label(); 
     l1.setBounds(50,150,125,50); 
     l1.setVisible(true); 
     add(l1); 

     Label l2 = new Label(); 
     l2 = new Label(); 
     l2.setBounds(275,150,125,50); 
     l2.setVisible(true); 
     add(l2); 

     Label l3 = new Label(); 
     l3 = new Label(); 
     l3.setBounds(500,150,125,50); 
     l3.setVisible(true); 
     add(l3); 

     Button b1 = new Button(); 
     b1 = new Button(); 
     b1.addActionListener(this); 
     b1.setBounds(25,100,175,175); 
     add(b1); 

     Button b2 = new Button(); 
     b2 = new Button(); 
     addWindowListener(this); 
     b2.addActionListener(this); 
     b2.setBounds(250,100,175,175); 
     add(b2); 

     Button b3 = new Button(); 
     b3 = new Button(); 
     b3.addActionListener(this); 
     b3.setBounds(475,100,175,175); 
     add(b3); 
    } 
    public void actionPerformed(ActionEvent e) { 
     if(e.getSource() == b1) 
     { 
      l1.setText("You pressed button 1."); 
     } 
     else if (e.getSource() == b2) 
     { 
      l2.setText("You pressed button 2."); 
     } 
     else if (e.getSource() == b3) 
     { 
      l3.setText("You pressed button 3."); 
     } 
    } 
    public void windowClosing(WindowEvent e) 
    { 
     dispose(); 
     System.exit(0); 
    } 
    public void windowActivated(WindowEvent e) {} 
    public void windowClosed(WindowEvent e) {} 
    public void windowDeactivated(WindowEvent e) {} 
    public void windowDeiconified(WindowEvent e) {} 
    public void windowIconified(WindowEvent e) {} 
    public void windowOpened(WindowEvent e) {} 
} 

回答

1
public void actionPerformed(ActionEvent e) { 
    if (e.getSource() == b1) { 
     // do stuff 
    } else if (e.getSource() == b2) { 
     // do other stuff 
    } 
} 

e.getSource()返回觸發事件的對象引用。

+0

我這樣做,但它仍然沒有顯示標籤 –

+0

首先,如果你沒有改變它,你需要添加一個動作監聽器到你的按鈕,例如'b1.addActionListener(本);'。其次,所有的類變量仍然是'null',因爲你正在創建本地對象'Button b1 = new Button();'而不是初始化類變量'b1 = new Button();'。第三,你將標籤的寬度和高度設置爲0,0。用l1.setBounds(10,10,50,50);'x,y = 10,10和width,height = 50,50調整它的大小。你可以玩這些值。 – yate

+0

我發佈了我的更新代碼 - 也許你可以幫助一旦你看到整個事情。 –

1

爲此,您應該使用actionCommand:

//after object creation: 
firstButton.setActionCommand("upper"); 
firstButton.addActionListener(this);// if this object is the listener 
secondButton.addActionListener(this);// if this object is the listener 
secondButton.setActionCommand("lower"); 

然後在您的actionPerformed():

所有的
public void actionPerformed(ActionEvent e) { 
    String command = event.getActionCommand(); 
    if("upper".equals(command)){ 
    //Do something 
    } else if("lower".equals(command)){ 
    //Do something 
    } 
} 
+0

這解決了它,但下面有一個更簡單的答案。不管怎麼說,還是要謝謝你。 –

+0

@YoungDeezie不同之處在於,如果使用我的解決方案,則必須使用getSource()解決方案將按鈕保持爲全局,而您可以在需要的位置定義按鈕。但它也可以工作! – desperateCoder

+0

你永遠不會在「actionPerformed-方法中跳躍,你必須在每個按鈕上註冊監聽器,像這樣:\t \t b3.addActionListener(this); – pL4Gu33

0

首先,我更喜歡搖擺AWT。以下代碼顯示帶有3個按鈕的JFrame。如果你點擊一個按鈕,它會顯示一個對話框,告訴你點擊了哪個按鈕。

我已使用Lambda表達式。所以,你必須讓jdk8運行 以下代碼。如果此代碼不適用於您,請勿降級。 剛走&搶jdk8。

如果您對Lambda Expression不滿意。然後按照這些說明: -

做一個內部類ListenForButton implements ActionListener
在重寫方法定義中寫入邏輯。那麼這個監聽器添加到您的按鈕
b1.addActionListener(new ListenForButton())


import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 

public class Option3 extends JFrame { 

    JButton b1, b2, b3; 
    JPanel jp1; 

    private void initComponents() { 
     jp1 = new JPanel(); 
     add(jp1); 

     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     b1 = new JButton("Button1"); 
     b1.addActionListener(e -> { 
      JOptionPane.showMessageDialog(this, "Button1 clicked"); 
     }); 
     jp1.add(b1); 

     b2 = new JButton("Button2"); 
     b2.addActionListener(e -> { 
      JOptionPane.showMessageDialog(this, "Button2 clicked"); 
     }); 
     jp1.add(b2); 

     b3 = new JButton("Button3"); 
     b3.addActionListener(e -> { 
      JOptionPane.showMessageDialog(this, "Button3 clicked"); 
     }); 
     jp1.add(b3); 
     pack(); 

    } 

    public static void main(String[] args) { 
     new Option3("Press a Button").setVisible(true); 
    } 

    public Option3(String option3) { 
     super(option3); 
     initComponents(); 
     this.setLocationRelativeTo(null); 
    } 
}