📄 moveboxinnercanvas.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import javax.swing.border.*;
public class MoveBoxInnerCanvas
extends JApplet
implements ActionListener
{
private final int BOX_WIDTH = 30;
private final int BOX_HEIGHT = 20;
private final int INC = 4;
private final Color COLOR = Color.red;
private JButton left = new JButton("Left");
private JButton right = new JButton("Right");
private JButton up = new JButton("Up");
private JButton down = new JButton("Down");
private int x = 50, y = 50;
private class MoveBoxCanvas
extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(COLOR);
g.fillRect(x, y, BOX_WIDTH, BOX_HEIGHT);
g.setColor(Color.black);
g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);
}
}
private MoveBoxCanvas drawing = new MoveBoxCanvas();
public void init()
{
JPanel buttons = new JPanel(new FlowLayout());
buttons.add(up);
up.addActionListener(this);
buttons.add(down);
down.addActionListener(this);
buttons.add(left);
left.addActionListener(this);
buttons.add(right);
right.addActionListener(this);
drawing.setBorder(new TitledBorder("The Box Playground"));
buttons.setBorder(new TitledBorder("Move the Box"));
try
{
URL base = getCodeBase();
up.setIcon(new ImageIcon(new URL(base, "up.gif")));
down.setIcon(new ImageIcon(new URL(base, "down.gif")));
left.setIcon(new ImageIcon(new URL(base, "left.gif")));
right.setIcon(new ImageIcon(new URL(base, "right.gif")));
}
catch (MalformedURLException murle)
{
System.err.println("Error");
}
up.setToolTipText("Box up");
down.setToolTipText("Box down");
right.setToolTipText("Box right");
left.setToolTipText("Box left");
getContentPane().setLayout(new BorderLayout());
getContentPane().add("South", buttons);
getContentPane().add("Center", drawing);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == up)
{
y -= INC;
}
else if (e.getSource() == down)
{
y += INC;
}
else if (e.getSource() == left)
{
x -= INC;
}
else if (e.getSource() == right)
{
x += INC;
}
drawing.repaint();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -