-2 java swing listeners awt callback
我正在尝试用听众编写一个项目,我的按钮在他们改变颜色的地方工作,现在我需要我的mouseLIsteners打印鼠标正在做的文本.例如:"鼠标已进入黄色区域,鼠标已退出黄色区域,鼠标已点击/释放黄色区域等" 我实现了它们,但没有任何工作要打印文本.这是我的代码:
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
}
Run Code Online (Sandbox Code Playgroud)
除非你将它添加到某个东西,否则XxxxListener将无法工作,除非它实际上是在监听某些内容.要使MouseListener做出反应,需要将其添加到已侦听的组件中someComponent.addMouseListener(myMouseListener).您将需要阅读有关侦听器的教程以获取详细信息.首先谷歌搜索Java Swing MouseListener Tutorial:MouseListener教程.
我对你的问题的主要批评是你在没有先尝试的情况下询问如何做某事,对不起,但这不是你将如何学习编程.有可用的教程,你有一个触手可及的计算机编程实验室,所以使用它.试验,玩耍,编写代码,运行它,更改它,将其推到极限然后超越,找出哪些无效.相信我,你不会炸毁你的电脑,你不会带来厄运和诅咒.对于可以通过测试回答的简单问题,请不要在这里问我们 - 自己找出答案.这就是学习和编程的全部内容.
编辑
OK,你将你的MouseListener 内的MouseListener的,这是行不通的.为什么不在类的构造函数或其他init或set方法中添加它?此外,您的编译器会抱怨,因为实现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);
}
Run Code Online (Sandbox Code Playgroud)
哪里
class MyListener implements MouseListener, ActionListener {
//..... bunch of code here
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
771 次 |
| 最近记录: |