当前位置: 技术问答>java相关
急~~~~~在线等待!!!
来源: 互联网 发布时间:2015-09-28
本文导语: import java.applet.*; import java.awt.*; import java.awt.event.*; public class MyAppletClass extends Applet implements ActionListener { String CmdPushStr=new String("Welcome Use My Applet Program"); Button cmd1=new Button("Show The Number"); Button c...
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class MyAppletClass extends Applet implements ActionListener
{ String CmdPushStr=new String("Welcome Use My Applet Program");
Button cmd1=new Button("Show The Number");
Button cmd2=new Button("Show the Number");
Button cmd4=new Button("Show the NUmber");
Button cmd5=new Button("Show the NUmber");
Button cmd6=new Button("Show the NUmber");
Button cmd7=new Button("Show the NUmber");
Button cmd8=new Button("Show the NUmber");
Button cmd9=new Button("Show the NUmber");
Button cmd0=new Button("Show the NUmber");
public void init()
{ add(cmd0);add(cmd1);add(cmd2);add(cmd3);add(cmd4);add(cmd5);add(cmd6);add(cmd7);add(cmd8);add(cmd9);
setVisible(true);
cmd0.addActionListener(this);
cmd1.addActionListener(this);
cmd2.addActionListener(this);
cmd3.addActionListener(this);
cmd4.addActionListener(this);
cmd5.addActionListener(this);
cmd6.addActionListener(this);
cmd7.addActionListener(this);
cmd8.addActionListener(this);
cmd9.addActionListener(this);
}
public void paint(Graphics g)
{g.setFont(new Font("arial",Font.BOLD,20));
g.setColor(new Color(255,0,0));
g.drawString(CmdPushStr,50,50);
}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand=="Show The Number") {CmdPushStr="1";repaint(); }
}
}
//问题是现在事件临听是共享的.且只能得到其的标签.请问如何实现.我按下CMD1的时候就画出字符串"1",当我按下CMD2的时候就画出字符串"2",当我按下CMD3的时候3就画出字符串"3".............................请问如何实现?
import java.awt.*;
import java.awt.event.*;
public class MyAppletClass extends Applet implements ActionListener
{ String CmdPushStr=new String("Welcome Use My Applet Program");
Button cmd1=new Button("Show The Number");
Button cmd2=new Button("Show the Number");
Button cmd4=new Button("Show the NUmber");
Button cmd5=new Button("Show the NUmber");
Button cmd6=new Button("Show the NUmber");
Button cmd7=new Button("Show the NUmber");
Button cmd8=new Button("Show the NUmber");
Button cmd9=new Button("Show the NUmber");
Button cmd0=new Button("Show the NUmber");
public void init()
{ add(cmd0);add(cmd1);add(cmd2);add(cmd3);add(cmd4);add(cmd5);add(cmd6);add(cmd7);add(cmd8);add(cmd9);
setVisible(true);
cmd0.addActionListener(this);
cmd1.addActionListener(this);
cmd2.addActionListener(this);
cmd3.addActionListener(this);
cmd4.addActionListener(this);
cmd5.addActionListener(this);
cmd6.addActionListener(this);
cmd7.addActionListener(this);
cmd8.addActionListener(this);
cmd9.addActionListener(this);
}
public void paint(Graphics g)
{g.setFont(new Font("arial",Font.BOLD,20));
g.setColor(new Color(255,0,0));
g.drawString(CmdPushStr,50,50);
}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand=="Show The Number") {CmdPushStr="1";repaint(); }
}
}
//问题是现在事件临听是共享的.且只能得到其的标签.请问如何实现.我按下CMD1的时候就画出字符串"1",当我按下CMD2的时候就画出字符串"2",当我按下CMD3的时候3就画出字符串"3".............................请问如何实现?
|
package ejb;
import java.awt.*;
import java.awt.event.*;
public class ActionTest extends WindowAdapter implements ActionListener
{
Frame mainFrame;
Label label;
Button button;
public ActionTest()
{
mainFrame=new Frame("事件测试");
label=new Label("效果显示");
button=new Button("A");
mainFrame.setLayout(null);
label.setBounds(20,30,200,23);
button.setBounds(20,63,70,23);
mainFrame.add(label);
mainFrame.add(button);
button.addMouseListener(new MouseListener()//这里是你需要参考的地方,就是对每个按牛单独增加事件舰艇器
{
public void mousePressed(MouseEvent me)
{
label.setText("鼠标按下");
}
public void mouseReleased(MouseEvent me)
{
}
public void mouseClicked(MouseEvent me)
{
label.setText("鼠标单击");
}
public void mouseExited(MouseEvent me)
{
label.setText("鼠标离开");
}
public void mouseEntered(MouseEvent me)
{
label.setText("鼠标进入");
}
}
);
mainFrame.addWindowListener(this);
mainFrame.setSize(250,100);
mainFrame.setBackground(new Color(212,208,200));
mainFrame.setVisible(true);
mainFrame.setResizable(false);
}
public static void main(String[]args)
{
ActionTest actiontest=new ActionTest();
}
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
public void actionPerformed(ActionEvent ae)
{
}
}
import java.awt.*;
import java.awt.event.*;
public class ActionTest extends WindowAdapter implements ActionListener
{
Frame mainFrame;
Label label;
Button button;
public ActionTest()
{
mainFrame=new Frame("事件测试");
label=new Label("效果显示");
button=new Button("A");
mainFrame.setLayout(null);
label.setBounds(20,30,200,23);
button.setBounds(20,63,70,23);
mainFrame.add(label);
mainFrame.add(button);
button.addMouseListener(new MouseListener()//这里是你需要参考的地方,就是对每个按牛单独增加事件舰艇器
{
public void mousePressed(MouseEvent me)
{
label.setText("鼠标按下");
}
public void mouseReleased(MouseEvent me)
{
}
public void mouseClicked(MouseEvent me)
{
label.setText("鼠标单击");
}
public void mouseExited(MouseEvent me)
{
label.setText("鼠标离开");
}
public void mouseEntered(MouseEvent me)
{
label.setText("鼠标进入");
}
}
);
mainFrame.addWindowListener(this);
mainFrame.setSize(250,100);
mainFrame.setBackground(new Color(212,208,200));
mainFrame.setVisible(true);
mainFrame.setResizable(false);
}
public static void main(String[]args)
{
ActionTest actiontest=new ActionTest();
}
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
public void actionPerformed(ActionEvent ae)
{
}
}