📄 sharkcontrols.java
字号:
import java.net.*;
import java.awt.event.*;
import java.awt.GridLayout;
import javax.swing.*;
public class SharkControls
extends JFrame
implements ActionListener
{
private String icons[] =
{
"stop.gif", "up.gif", "rise.gif",
"left.gif", "swan.gif", "right.gif",
"start.gif", "down.gif", "dive.gif"};
private String tips[] =
{
"Stop game", "Shark up", "Shark rises",
"Shark left", "Help", "Shark right",
"Start game", "Shark down", "Shark dives"};
private SharkAttack applet = null;
private Shark shark = null;
private JButton buttons[] = new JButton[9];
public SharkControls(SharkAttack _applet, Shark _shark)
{
super("Shark Controls");
applet = _applet;
shark = _shark;
JPanel buttonPanel = new JPanel(new GridLayout(3, 3));
try
{
for (int i = 0; i < buttons.length; i++)
{
URL url = new URL(applet.getCodeBase(), "Images/" + icons[i]);
buttons[i] = new JButton(new ImageIcon(url));
buttons[i].addActionListener(this);
buttons[i].setToolTipText(tips[i]);
buttonPanel.add(buttons[i]);
}
}
catch (MalformedURLException murle)
{
System.err.println("Error loading icons: " + murle);
}
getContentPane().add("Center",
new JPanelBox(buttonPanel, "Shark Controls"));
setResizable(false);
validate();
pack();
}
public void actionPerformed(ActionEvent ae)
{
if (ae.getSource() == buttons[0])
{
applet.stop();
}
else if (ae.getSource() == buttons[1])
{
shark.moveUp();
}
else if (ae.getSource() == buttons[2])
{
shark.rise();
}
else if (ae.getSource() == buttons[3])
{
shark.turnLeft();
}
else if (ae.getSource() == buttons[4])
{
showHelp();
}
else if (ae.getSource() == buttons[5])
{
shark.turnRight();
}
else if (ae.getSource() == buttons[6])
{
applet.start();
}
else if (ae.getSource() == buttons[7])
{
shark.moveDown();
}
else if (ae.getSource() == buttons[8])
{
shark.dive();
}
}
private void showHelp()
{
JOptionPane.showMessageDialog(this,
"Shark Attack\n(c) 2003\n",
"Shark Attack",
JOptionPane.INFORMATION_MESSAGE);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -