2013-05-31 35 views
-2

所以我創建了一個三個按鈕的菜單,每個都會打開另一個窗口。我將這些按鈕添加到了actionListnener中,我檢查了Source,我做了所有事情。但它仍然是一個虛擬按鈕。actionPerformed不工作

夠了,我認爲如果你們看到代碼會更好。 提前謝謝!

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.border.EmptyBorder; 

public class MainMenu extends JFrame 
{ 
    private JPanel contentPane; 
    private JButton decB; 
    private JButton hexB; 
    private JButton binB; 
    private JLabel label1; 

    public MainMenu() 
    { 
     setTitle("Hello Dear Friend"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setBounds(100, 100, 323, 303); 
     contentPane = new JPanel(); 
     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     setContentPane(contentPane); 
     contentPane.setLayout(null); 
     setLocationRelativeTo(null); 

     JButton decB = new JButton("Decimal"); 
     decB.setFont(new Font("Tahoma", Font.BOLD, 12)); 
     decB.setBounds(79, 85, 146, 42); 
     decB.setBackground(new Color(212, 208, 199)); 
     decB.setFocusPainted(false); 

     JButton hexB = new JButton("Hexadecimal"); 
     hexB.setFont(new Font("Tahoma", Font.BOLD, 12)); 
     hexB.setBounds(79, 138, 146, 42); 
     hexB.setBackground(new Color(212, 208, 199)); 
     hexB.setFocusPainted(false); 

     JButton binB = new JButton("Binary"); 
     binB.setFont(new Font("Tahoma", Font.BOLD, 12)); 
     binB.setBounds(79, 191, 146, 42); 
     binB.setBackground(new Color(212, 208, 199)); 
     binB.setFocusPainted(false); 

     JLabel label1 = new JLabel("Select the base you wish to convert: "); 
     label1.setFont(new Font("Courier New", Font.BOLD, 12)); 
     label1.setBounds(20, 11, 277, 63); 

     contentPane.add(decB); 
     contentPane.add(binB); 
     contentPane.add(hexB); 
     contentPane.add(label1); 

     ButtonHandler bh = new ButtonHandler(); 
     decB.addActionListener(bh); 
     hexB.addActionListener(bh); 
     binB.addActionListener(bh); 
    } 
    private class ButtonHandler implements ActionListener 
    { 
     DecMenu dm = new DecMenu(); 
     BinaryMenu bm = new BinaryMenu(); 
     HexMenu hm = new HexMenu(); 

     public void actionPerformed(ActionEvent event) 
     { 
      setVisible(false); 

      if(event.getSource() == decB) 
       dm.setVisible(true); 

      else if(event.getSource() == hexB) 
       hm.setVisible(true); 

      else if(event.getSource() == binB) 
       bm.setVisible(true); 
     } 
    } 
} 
+0

你期待看到什麼?看起來你並沒有對菜單做任何事情,所以當它們被顯示時,沒有任何東西可以顯示。你是否在動作監聽器代碼中放置了一些打印語句?我敢打賭他們射擊很好。 – thatidiotguy

+0

我期待在單擊按鈕後出現其他框架。是的,我已經在actionlistener中打印了語句,這意味着actionlistener正在工作,但正如我所說的,我沒有看到任何菜單。 – altaha0a

回答

3

用途:

event.getSource().equals(decB); 

或更換:

JButton decB = new JButton("Decimal"); 

有:

this.decB = new JButton("Decimal"); 

等其他字段。


編輯:

在Java對象的==操作檢查,看看他們是否是相同的對象。你的局部變量隱藏字段變量,這樣當你調用

event.getSource() == decB 

你實際上調用

event.getSource() == null 

您可以檢查這通過在代碼中使用System.out.println(this.decB)

+0

我看不出有什麼幫助。它們都是適當的對象實例。 – altaha0a

+0

+1,@ altaha0a,你有類和實例變量。類變量(這是您在ActionListner中檢查的內容)爲空。按照上面的建議擺脫實例變量,然後你將初始化你的類變量。 – camickr

+0

是的。那確實有用!先生非常感謝您。你解決了一個很大的困境。 – altaha0a

1

這些菜單對象並不是隨處添加。一個組件只能添加到容器中才可見。

編輯:

由於@ medPhys-PL指出(+1),全球按鈕是從當地人(在構造函數中)有效地不同。這導致if測試永遠不會通過。

要麼刪除構造函數中的「類型前綴」(推薦,不需要this),要麼使用equals(勸阻)。

+0

請原諒我缺乏專業知識,我在編程方面相對較新。我確實添加了它們,但仍然沒有任何東西。 – altaha0a

+0

@ altaha0a不在您的發佈代碼中... – Mordechai