2014-10-18 143 views
-2

我想用監聽器編寫一個項目,我的按鈕正在工作,他們改變顏色,現在我需要我的mouseLIsteners來打印鼠標正在做什麼的文本。例如:「鼠標進入黃色區域,鼠標退出黃色區域,鼠標點擊/釋放黃色區域等。」 我有他們實現,但沒有任何工作,以獲得文本打印出來。這裏是我的代碼:回調和監聽器; MouseListener

  import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 

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

import java.awt.GridLayout; 
import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.geom.Line2D; 
import java.awt.geom.Point2D; 

import javax.swing.JComponent; 

public class SwingLab 
{ 

// frame properties 
private static final int FRAME_WIDTH = 400; private static final int FRAME_HEIGHT = 400; 

public static void main(String[] args) 
{ 
// Instantiate a frame (the main window) 
JFrame frame = new JFrame(); 

// The buttons (one for each color) 
final JButton bRed = new JButton("Red"); 
JButton bYellow = new JButton("Yellow"); 
JButton bBlue = new JButton("Blue"); 


// Here we create a panel consisting of other panels (layed out in a 
// Grid) to support the buttons and "Art" instance 

final JPanel container = new JPanel(new GridLayout(2,1)); 
final JPanel panel = new JPanel(new GridLayout(1,1)); 
final JPanel buttonPanel = new JPanel(new GridLayout(3,1)); 

// An instance of a special class for you to play with (Art is defined 
// below) 
Art artBox=new Art(); 
panel.add(artBox); 

// add the buttons to the panel 
buttonPanel.add(bRed); 
buttonPanel.add(bYellow); 
buttonPanel.add(bBlue); 

// put the panels together and add them to the frame 
container.add(panel); 
container.add(buttonPanel); 
frame.add(container); 


/* YOUR CODE GOES HERE */ 

// declare your listener classes and add them to the buttons 
// here. 
// you are going to call addActionListener and 
// addMouseListener for each button 
// you want to deal with the JPanel named "panel" declared 
// above 


/* END YOUR CODE */ 
class RedButtonListener implements ActionListener, MouseListener 

{ 

public void actionPerformed(ActionEvent event) 

{ 
panel.setBackground(Color.RED); 
} 


@Override 
public void mouseClicked(MouseEvent e) { 
    bRed.addMouseListener(this); 
    addMouseListener(this); 
    // TODO Auto-generated method stub 

} 
+0

您的代碼不顯示您的問題,除了你有很多不必要的冗餘,但再次,這不是你的錯誤的原因。爲了幫助我們,您需要展示足夠的代碼,以便我們能夠理解您的問題,但不要使用太多的代碼,以致於無法處理與手頭問題無關的太多代碼。最好是如果你可以花時間來創建和發佈一個[最小示例程序](http://stackoverflow.com/help/mcve) – 2014-10-18 01:55:51

+0

我是新來的java,所以這可能是爲什麼冗餘。問題是我有聽衆和事件實施,但當我在輸入事件中寫入System.out.println(「鼠標已進入黃色區域」)時,我沒有texg – 2014-10-18 01:57:36

+0

我們如何猜測可能是什麼問題,如果我們沒有看到你如何使用你的聽衆,你如何將它們添加到你的GUI?例如,我無處看到'addMouseListener(...)'。那麼它在哪裏?事實上,**是**你添加任何MouseListeners任何東西? – 2014-10-18 01:58:10

回答

6

,除非你把它添加到的東西,除非它確實聽的東西的XxxxListener將無法正常工作。要使MouseListener響應,需要通過someComponent.addMouseListener(myMouseListener)將其添加到收聽組件。你會想要閱讀聽衆的教程,以獲得細節。第一次谷歌擊中Java Swing MouseListener TutorialMouseListener Tutorial

我對你的問題的主要批評是,你問如何做一些事情,而不先試一試,對不起,但這不是你將如何學習編程。有教程可用於此,並且您的指尖上有一個計算機編程實驗室,請使用它。試驗,播放,編寫代碼,運行它,改變它,把它推到極限,然後超越,找出哪些是行不通的。相信我,你不會炸燬你的電腦,你不會爲此付出任何代價和詛咒。對於可以通過測試回答的簡單問題,不要問我們在這裏 - 找出自己。這就是學習和編程的全部內容。


編輯

OK,你想要添加您的MouseListener 內的MouseListener,這是行不通的。爲什麼不把它添加到你的類的構造函數或其他初始化或設置方法?另外,你的編譯器會抱怨,因爲你實現MouseListener的類沒有實現MouseListener接口的所有方法,這是不允許的(除非類是抽象的,這不是你想要的)。所以給它剩下的MouseListener方法。

例如:

public class Foo extends JPanel { 
    private JButton button = new JButton("Button"); 

    public Foo() { 
     MyListener myListener = new MyListener(); 
     button.addActionListener(myListener); 
     button.addMouseListener(myListener); 

     add(button); 
    } 

class MyListener implements MouseListener, ActionListener { 
    //..... bunch of code here 
} 
+0

我一直在嘗試這一整天,我越來越接近它,這是我不能完全得到的最後一件事。我早些時候閱讀了教程,通過嘗試,我已經得到了很多,但我只是卡住了。我似乎無法找到一個非錯誤的位置來添加.Mouselistener。我每次都會收到一個錯誤,甚至當我用短語來形容教程時,我沒有文字 – 2014-10-18 02:13:29

+0

@ThomasNorman:如果您在嘗試中遇到錯誤,您是不是應該向我們展示這個錯誤?我們怎麼能猜到你可能會做錯什麼呢? – 2014-10-18 02:16:33

+0

@ThomasNorman:請發佈一個小的可編譯和可運行的程序,或者最接近你可以來到這個,正如我在你的問題的第一個評論中提到的,[mcve](http://stackoverflow.com/help/mcve) 。 – 2014-10-18 02:20:04

3

您要添加的偵聽器只爲被從該接口實現正在一個ActionListener這樣的方法,但你有沒有增加一條,作爲一個MouseListener,所以它不會收到這些事件的回調。但是我同意HoverCraft的觀點,你將不會從你的教授那裏學到任何東西,而是讓你的代碼進行調整,然後我們爲你做。實際上,如果你想學習Swing,你應該自己實現這個整個程序。

0

你的程序有一堆錯誤。首先,您不更新EDT中的窗口小部件,您需要將main()的全部內容放入invokeLater可運行的窗口中。

其次,你需要定義你的聽衆,然後將它們安裝到按鈕上,然後將它們顯示在屏幕上,而不是在聽衆本身中。下面的程序將在點擊按鈕時切換顏色,並在鼠標退出並進入按鈕時更改按鈕文本。

我已經使用MouseAdapter而不是mouseListener,因爲它已經爲MouseListener的所有必需方法定義了默認的,無所事事的方法。因此,代碼得到一點點混亂:

package swing; 

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.SwingUtilities; 

public class testFrame { 

    public static void showFrame() { 
     JFrame frame = new JFrame(); 
     frame.setLocation(500, 500); 
     frame.setSize(100, 100); 
     final JButton button = new JButton("Test"); 
     button.setMaximumSize(new Dimension(80, 30)); 
     button.setSize(80, 20); 
     frame.getContentPane().add(button); 
     button.addMouseListener(new MouseAdapter() { 

      boolean colourToggle = false; 

      @Override 
      public void mouseExited(MouseEvent e) { 
       button.setText("Mouse Out"); 
      } 

      @Override 
      public void mouseEntered(MouseEvent e) { 
       button.setText("Mouse In"); 
      } 

      @Override 
      public void mouseClicked(MouseEvent e) { 
       if (colourToggle) { 
        button.setBackground(new Color(100, 200, 100)); 
       } else { 
        button.setBackground(new Color(255, 0, 150)); 
       } 
       colourToggle = !colourToggle; 
      } 
     }); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       showFrame(); 
      } 
     }); 
    } 
}