刚学完GUI,做一个小记事本,关于事件监听器..获取事件源有问题....求高手
以下是代码......
先说明一下,界面已经能出来了,也能运行出来,给复制,粘贴,什么的加监听器,获取事件源的时间有问题,貌似获取不了事件源信息啊.....求高手帮忙看看
关于背景音乐的监听部分先空着.....
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.imageio.*;
import java.io.*;
import java.applet.*;
import java.net.*;
class TextPanel extends JPanel {
Color color = new Color(123,122,255);
Font font = new Font("楷体",Font.BOLD,33);
JTextArea texta;
JScrollPane sp;
public TextPanel() {
texta = new JTextArea(10,20);
sp = new JScrollPane(texta);
setLayout(new BorderLayout());
add(sp,BorderLayout.CENTER);
}
}
class Text extends JFrame {
TextPanel pane;
JMenu media = new JMenu("背景音乐");
JMenu FSetting = new JMenu("字体设置");
JMenu FColor = new JMenu("字体颜色");
JMenu FSize = new JMenu("字体形状");
JMenuItem play = new JMenuItem("播放");
JMenuItem stop = new JMenuItem("停止");
JMenuBar menuBar= new JMenuBar();
JPopupMenu pop = new JPopupMenu();
JMenuItem paste = new JMenuItem("粘贴");
JMenuItem copy = new JMenuItem("复制");
JMenuItem delete= new JMenuItem("删除");
JCheckBoxMenuItem st = new JCheckBoxMenuItem("宋体");
JCheckBoxMenuItem kt = new JCheckBoxMenuItem("楷体");
JCheckBoxMenuItem red = new JCheckBoxMenuItem("红色");
JCheckBoxMenuItem blue = new JCheckBoxMenuItem("蓝色");
public Text() {
this.setTitle("MyText");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pane = new TextPanel();
play.addActionListener(new TextAction());
stop.addActionListener(new TextAction());
media.add(play);
media.add(stop);
FColor.add(red);
FColor.add(blue);
FSize.add(st);
FSize.add(kt);
FSetting.add(FColor);
FSetting.add(FSize);
pop.add(paste);
pop.add(copy);
pop.add(delete);
menuBar.add(media);
menuBar.add(FSetting);
paste.addActionListener(new TextAction());
copy.addActionListener(new TextAction());
delete.addActionListener(new TextAction());
st.addActionListener(new TextAction());
kt.addActionListener(new TextAction());
red.addActionListener(new TextAction());
blue.addActionListener(new TextAction());
pane.texta.addMouseListener(new MouseAdapter(){
public void mouseReleased(MouseEvent e) {
if(e.isPopupTrigger())
pop.show(pane.texta,e.getX(),e.getY());
}
} );
Container container = this.getContentPane();
container.add(pane);
setJMenuBar(menuBar);
this.setSize(500,400);
setVisible(true);
}
}
class TextAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(e.getSource()==red) {
pane.texta.setSelectedTextColor(Color.RED);
}
if(e.getSource()==blue) {
pane.texta.setSelectedTextColor(Color.BLUE);
}
if(e.getSource()==kt) {
pane.texta.setFont(new Font("楷体",Font.BOLD,40));