因此,我的代码链接到一些名为SkeletonHand
,Cross
和Headstone
的其他类。基本上这部分代码作为所有类的管理器,在我提到的三个类中的每个类中调用draw方法,然后在{{1}上绘制骨架手,十字架或墓碑的图片。 (代表一个墓地)。在底部是几个按钮,我也设置了更改背景颜色。
我的问题,我现在要做的是,如何在创建的最后一个按钮(JFrame
)中创建和事件,以便每按一次按钮就会创建一个新的骨架手?
所以,我想创建一个名为"提升死亡的按钮"每次按下时都会在页面上创建一个新的Skeleton手。手将处于随机X位置。但我知道该怎么做。首先,我需要知道如何创建事件。任何有用的见解?我会发布其他课程,以便显示大图。
HandClicker
------ ------- SkeletonHand
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JButton;
public class ManagerPanel extends JPanel
{
private JButton clicker;
private JButton redClicker;
private JButton greenClicker;
private JButton blueClicker;
private JButton handClicker;
private int red, green, blue;
private SkeletonHand skeletonHand1, skeletonHand2;
private Cross cross1, cross2;
private HeadStone headStone1, headStone2;
//-----------------------------------------------------------------
// Constructor: Creates objects.
//-----------------------------------------------------------------
public ManagerPanel()
{
skeletonHand1 = new SkeletonHand (340);
skeletonHand2 = new SkeletonHand (540);
cross1 = new Cross (150, "Andrew");
cross2 = new Cross (530, "Alexis");
headStone1 = new HeadStone (250, "Jose");
headStone2 = new HeadStone (431, "Alex");
clicker = new JButton("Click here");
clicker.addActionListener (new ClickerListener());
redClicker = new JButton("More red!");
redClicker.addActionListener (new RedListener());
greenClicker = new JButton("More green!");
greenClicker.addActionListener (new GreenListener());
blueClicker = new JButton("More blue!");
blueClicker.addActionListener (new BlueListener());
handClicker = new JButton("Raise the dead");
handClicker.addActionListener (new HandListener());
setBackground (Color.blue);
setPreferredSize (new Dimension(1000, 700));
setFont (new Font("Arial", Font.BOLD, 16));
add (clicker);
add (redClicker);
add (greenClicker);
add (blueClicker);
}
//-----------------------------------------------------------------
// Draws this panel by requesting that each graphic draw itself.
//-----------------------------------------------------------------
public void paintComponent (Graphics page)
{
super.paintComponent(page);
Color brown = new Color(160, 82, 45);
page.setColor (brown);
page.fillRect (0, 630, 1000, 70);
skeletonHand1.draw(page);
skeletonHand2.draw(page);
cross1.draw(page);
cross2.draw(page);
headStone1.draw(page);
headStone2.draw(page);
}
private class ClickerListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
red = 0; green = 0; blue = 0;
setBackground (new Color(red, green, blue));
}
}
private class RedListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
red += 20;
setBackground (new Color(red, green, blue));
}
}
private class GreenListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
green += 20;
setBackground (new Color(red, green, blue));
}
}
private class BlueListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
blue += 20;
setBackground (new Color(red, green, blue));
}
}
private class HandListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
skeletonHand1 = new SkeletonHand (340);
}
}
}
------ -------交叉
import java.awt.*;
public class SkeletonHand
{
private final int BASEY = 630;
private int BASEX;
//-----------------------------------------------------------------
// Constructor: Sets up the Skeleton Hand with the specified values.
//-----------------------------------------------------------------
public SkeletonHand (int x)
{
BASEX = x;
}
//-----------------------------------------------------------------
// Draws the Skeleton Hands in the specified graphics context.
//-----------------------------------------------------------------
public void draw (Graphics page)
{
page.setColor (Color.white);
page.drawLine (BASEX, BASEY-1, BASEX, BASEY-20); // arm
page.drawLine (BASEX, BASEY-20, BASEX-5, BASEY-20); // thumb finger
page.drawLine (BASEX, BASEY-20, BASEX-5, BASEY-27); // index finger
page.drawLine (BASEX, BASEY-20, BASEX-2, BASEY-29); // middle finger
page.drawLine (BASEX, BASEY-20, BASEX+1, BASEY-29); // ring finger
page.drawLine (BASEX, BASEY-20, BASEX+4, BASEY-27); // pinky finger
}
}
------- -------墓石
import java.awt.*;
public class Cross
{
private final int BASEY = 630;
private int BASEX;
private String name;
//-----------------------------------------------------------------
// Constructor: Sets up the Crosses with the specified values.
//-----------------------------------------------------------------
public Cross (int x, String n)
{
BASEX = x;
name = n;
}
//-----------------------------------------------------------------
// Draws the Cross' in the specified graphics context.
//-----------------------------------------------------------------
public void draw (Graphics page)
{
page.setColor (Color.darkGray);
page.fillRect (BASEX, BASEY-120, 30, 120);
page.fillRect (BASEX-30, BASEY-90, 90, 30);
page.setColor (Color.black);
page.drawString (String.valueOf(name), BASEX-10, BASEY-70);
}
}
答案 0 :(得分:1)
如何在创建的最后一个按钮(HandClicker)中创建一个事件,以便每按一次按钮就会创建一个新的骨架手?所以,我想创建一个名为" Raise the Dead"每次按下时都会在页面上创建一个新的Skeleton手。手将处于随机X位置。但我知道该怎么做。首先,我需要知道如何创建事件。任何有用的见解?我会发布其他课程,以便显示大图。
创建List
作为实例字段...
private java.util.List<SkeletonHand> hands = new java.util.ArrayList<>(25);
调用ActionListener
的{{1}}后,将HandClicker
的新实例添加到SkeletonHand
....
List
作为绘画过程的一部分,循环浏览此列表并画出手......
public void actionPerformed (ActionEvent event)
{
hands.add(new SkeletonHand (340));
repaint();
}