当前位置: 代码迷 >> J2SE >> Java等考试题不会做,大家帮忙看看,该怎么解决
  详细解决方案

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

热度:131   发布时间:2016-04-24 02:32:28.0
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 四个方法看方法名字就知道了
  相关解决方案