2014-10-18 144 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(); 
      } 
     }); 
    } 
}