帮忙看下java代码,同时处理两个事件,怎么实现
importjava.awt.*;importjavax.swing.*;importjava.awt.event.*;importjavax.swing.event.*...
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.applet.*;
public class Player extends JApplet implements ActionListener,ListSelectionListener
{
private JButton jb1=new JButton("replay");
private JButton jb2=new JButton("play");
private JButton jb3=new JButton("stop");
private AudioClip sound;
private JList<String>list;
private int temp;
private String currentMusic=new String("真的爱你.mid");
private String[] music={"someone like you","make you feel my love",
"don't you remember","hai.mid"};
public Player()
{
JFrame frame=new JFrame();
frame.setSize(250,400);
frame.setLocation(new Point(100,300));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar bar=new JMenuBar();
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
frame.setJMenuBar(bar);
Container con1=frame.getContentPane();
con1.setLayout(new BorderLayout());
panel1.setLayout(new GridLayout(1,3));
panel2.setLayout(new BorderLayout());
JScrollPane jsp;
list=new JList<String>(music);
jsp=new JScrollPane(list,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
frame.setTitle("music");
bar.add(new JMenu(" "));
panel1.add(jb1);
panel1.add(jb2);
panel1.add(jb3);
panel2.add(panel1,BorderLayout.NORTH);
panel2.add(jsp,BorderLayout.CENTER);
con1.add(panel2,BorderLayout.CENTER);
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
frame.setVisible(true);
}
public void valueChanged(ListSelectionEvent e)
{
temp=list.getSelectedIndex();
currentMusic=music[temp];
}
public void actionPerformed (ActionEvent e)
{
sound=getAudioClip(getCodeBase(),currentMusic);
if(e.getSource()==jb1)
sound.play();
else if(e.getSource()==jb2)
sound.loop();
else
sound.stop();
}
public static void main(String[] args)
{
new Player();
}
} 展开
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.applet.*;
public class Player extends JApplet implements ActionListener,ListSelectionListener
{
private JButton jb1=new JButton("replay");
private JButton jb2=new JButton("play");
private JButton jb3=new JButton("stop");
private AudioClip sound;
private JList<String>list;
private int temp;
private String currentMusic=new String("真的爱你.mid");
private String[] music={"someone like you","make you feel my love",
"don't you remember","hai.mid"};
public Player()
{
JFrame frame=new JFrame();
frame.setSize(250,400);
frame.setLocation(new Point(100,300));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar bar=new JMenuBar();
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
frame.setJMenuBar(bar);
Container con1=frame.getContentPane();
con1.setLayout(new BorderLayout());
panel1.setLayout(new GridLayout(1,3));
panel2.setLayout(new BorderLayout());
JScrollPane jsp;
list=new JList<String>(music);
jsp=new JScrollPane(list,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
frame.setTitle("music");
bar.add(new JMenu(" "));
panel1.add(jb1);
panel1.add(jb2);
panel1.add(jb3);
panel2.add(panel1,BorderLayout.NORTH);
panel2.add(jsp,BorderLayout.CENTER);
con1.add(panel2,BorderLayout.CENTER);
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
frame.setVisible(true);
}
public void valueChanged(ListSelectionEvent e)
{
temp=list.getSelectedIndex();
currentMusic=music[temp];
}
public void actionPerformed (ActionEvent e)
{
sound=getAudioClip(getCodeBase(),currentMusic);
if(e.getSource()==jb1)
sound.play();
else if(e.getSource()==jb2)
sound.loop();
else
sound.stop();
}
public static void main(String[] args)
{
new Player();
}
} 展开
3个回答
展开全部
并且有相应的处理,并且也能控制,我最近写了个两人玩的俄罗斯方块,就是这问题。 你可以设置不同的下键 第一个是下 第二个事s 不能两个人都用U
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用线程吧。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
恩,同上
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询