java题不会做怎么办,Java等考试题不会做,大家帮忙看看,该怎么解决

当前位置:我的异常网» J2SE » Java等考试题不会做,大家帮忙看看,该怎么解决

Java等考试题不会做,大家帮忙看看,该怎么解决

www.myexceptions.net  网友分享于:2013-01-19  浏览:8次

Java等考试题不会做,大家帮忙看看

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

//*********Found**********

public class Java_3 extends JFrame implements ActionListener

{

//*********Found**********

private JPopupMenu pop;

private JMenu subPop;

private JMenuItem color;

private JMenuItem exit;

private JMenuItem red;

private JMenuItem blue;

private JTextArea textArea;

private JFrame frame;

public void initGUI()

{

pop=new JPopupMenu();

subPop=new JMenu("color");

//*********Found**********

red=new JMenuItem("red");

red.addActionListener(this);

blue=new JMenuItem("blue");

blue.addActionListener(this);

subPop.add(red);

subPop.add(blue);

exit=new JMenuItem("exit");

exit.addActionListener(this);

pop.add(subPop);

pop.add(exit);

frame=new JFrame("popup frame");

textArea=new JTextArea("",10,10);

textArea.addMouseListener(this); //此行报错,为什么呢?

//*********Found**********

frame.getContentPane().add(textArea);

frame.setSize(300,300);

frame.setVisible(true);

frame.addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

});

}

public void actionPerformed(ActionEvent event)

{

if(event.getSource()==red)

{

//*********Found**********

textArea.setForeground(Color.red);

textArea.setText("red menu is selected");

}

else if(event.getSource()==blue)

{

textArea.setForeground(Color.blue);

textArea.setText("blue menu is selected");

}

else if(event.getSource()==exit)

{

frame.setVisible(false);

System.exit(0);

}

}

public void mousePressed(MouseEvent e)

{

if(e.getModifiers()==e.BUTTON3_MASK)

{

pop.show(e.getComponent(),e.getX(),e.getY());

}

}

public static void main(String args[])

{

Java_3 example=new Java_3();

example.initGUI();

}

}

第40行报错,不知为什么。

------解决方案--------------------

textArea.addMouseListener(new MouseListener() {

public void mouseReleased(MouseEvent e) {

// TODO Auto-generated method stub

}

public void mousePressed(MouseEvent e) {

// TODO Auto-generated method stub

}

public void mouseExited(MouseEvent e) {

// TODO Auto-generated method stub

}

public void mouseEntered(MouseEvent e) {

// TODO Auto-generated method stub

}

public void mouseClicked(MouseEvent e) {

// TODO Auto-generated method stub

}

});

你要textArea添加鼠标点击,里面要实现这个借口ok 四个方法看方法名字就知道了

文章评论

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值