- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个关于制作按钮程序的问题。于是,我制作了一些简单的“猜猜游戏”、“贪吃蛇”、“俄罗斯方 block ”等程序和一个按钮程序。我想制作一个程序,当您单击按钮时,它会运行特定的程序。例如,如果您单击“按钮 1”,它将运行“俄罗斯方 block ”。任何帮助都会很棒!
这是“俄罗斯方 block ”程序(代码)。
package tetris;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Collections;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Tetris extends JPanel
{
private static final long serialVersionUID = -8715353373678321308L;
private final Point[][][] Tetraminos = {
// I-Piece
{
{new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(3, 1)},
{new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(1, 3)},
{new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(3, 1)},
{new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(1, 3)}
},
// J-Piece
{
{new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(2, 0)},
{new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(2, 2)},
{new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(0, 2)},
{new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(0, 0)}
},
// L-Piece
{
{new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(2, 2)},
{new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(0, 2)},
{new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(0, 0)},
{new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(2, 0)}
},
// O-Piece
{
{new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1)},
{new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1)},
{new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1)},
{new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1)}
},
// S-Piece
{
{new Point(1, 0), new Point(2, 0), new Point(0, 1), new Point(1, 1)},
{new Point(0, 0), new Point(0, 1), new Point(1, 1), new Point(1, 2)},
{new Point(1, 0), new Point(2, 0), new Point(0, 1), new Point(1, 1)},
{new Point(0, 0), new Point(0, 1), new Point(1, 1), new Point(1, 2)}
},
// T-Piece
{
{new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(2, 1)},
{new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(1, 2)},
{new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(1, 2)},
{new Point(1, 0), new Point(1, 1), new Point(2, 1), new Point(1, 2)}
},
// Z-Piece
{
{new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1)},
{new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(0, 2)},
{new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1)},
{new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(0, 2)}
}
};
private final Color[] tetraminoColors = {
Color.cyan, Color.blue, Color.orange, Color.yellow, Color.green, Color.pink, Color.red
};
private Point pieceOrigin;
private int currentPiece;
private int rotation;
private ArrayList<Integer> nextPieces = new ArrayList<Integer>();
private long score;
private Color[][] well;
// Creates a border around the well and initializes the dropping piece
private void init()
{
well = new Color[12][24];
for (int i = 0; i < 12; i++)
{
for (int j = 0; j < 23; j++)
{
if (i == 0 || i == 11 || j == 22)
{
well[i][j] = Color.GRAY;
}
else
{
well[i][j] = Color.BLACK;
}
}
}
newPiece();
}
// Put a new, random piece into the dropping position
public void newPiece()
{
pieceOrigin = new Point(5, 2);
rotation = 0;
if (nextPieces.isEmpty())
{
Collections.addAll(nextPieces, 0, 1, 2, 3, 4, 5, 6);
Collections.shuffle(nextPieces);
}
currentPiece = nextPieces.get(0);
nextPieces.remove(0);
}
// Collision test for the dropping piece
private boolean collidesAt (int x, int y, int rotation)
{
for (Point p : Tetraminos[currentPiece][rotation])
{
if (well[p.x + x][p.y + y] != Color.BLACK)
{
return true;
}
}
return false;
}
// Rotate the piece clockwise or counterclockwise
public void rotate (int i)
{
int newRotation = (rotation + i) % 4;
if (newRotation < 0)
{
newRotation = 3;
}
if (!collidesAt(pieceOrigin.x, pieceOrigin.y, newRotation))
{
rotation = newRotation;
}
repaint();
}
// Move the piece left or right
public void move (int i)
{
if (!collidesAt(pieceOrigin.x + i, pieceOrigin.y, rotation))
{
pieceOrigin.x += i;
}
repaint();
}
// Drops the piece one line or fixes it to the well if it can't drop
public void dropDown() {
if (!collidesAt(pieceOrigin.x, pieceOrigin.y + 1, rotation)) {
pieceOrigin.y += 1;
} else {
fixToWell();
}
repaint();
}
// Make the dropping piece part of the well, so it is available for
// collision detection.
public void fixToWell()
{
for (Point p : Tetraminos[currentPiece][rotation])
{
well[pieceOrigin.x + p.x][pieceOrigin.y + p.y] = tetraminoColors[currentPiece];
}
clearRows();
newPiece();
}
public void deleteRow (int row)
{
for (int j = row-1; j > 0; j--)
{
for (int i = 1; i < 11; i++)
{
well[i][j+1] = well[i][j];
}
}
}
// Clear completed rows from the field and award score according to
// the number of simultaneously cleared rows.
public void clearRows()
{
boolean gap;
int numClears = 0;
for (int j = 21; j > 0; j--)
{
gap = false;
for (int i = 1; i < 11; i++)
{
if (well[i][j] == Color.BLACK)
{
gap = true;
break;
}
}
if (!gap)
{
deleteRow(j);
j += 1;
numClears += 1;
}
}
switch (numClears)
{
case 1: score += 100;
break;
case 2: score += 300;
break;
case 3: score += 500;
break;
case 4: score += 800;
break;
}
}
// Draw the falling piece
private void drawPiece (Graphics g)
{
g.setColor(tetraminoColors[currentPiece]);
for (Point p : Tetraminos[currentPiece][rotation])
{
g.fillRect((p.x + pieceOrigin.x) * 26, (p.y + pieceOrigin.y) * 26, 25, 25);
}
}
@Override
public void paintComponent (Graphics g)
{
// Paint the well
g.fillRect(0, 0, 26 * 12, 26 * 23);
for (int i = 0; i < 12; i++)
{
for (int j = 0; j < 23; j++)
{
g.setColor(well[i][j]);
g.fillRect(26 * i, 26 * j, 25, 25);
}
}
// Display the score
g.setColor(Color.WHITE);
g.drawString("" + score, 19 * 12, 25);
// Draw the currently falling piece
drawPiece(g);
}
public static void main(String[] args)
{
JFrame f = new JFrame("Tetris");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(12*26+10, 26*23+25);
f.setVisible(true);
final Tetris game = new Tetris();
game.init();
f.add(game);
// Keyboard controls
f.addKeyListener(new KeyListener()
{
public void keyTyped(KeyEvent e)
{
}
public void keyPressed(KeyEvent e)
{
switch (e.getKeyCode())
{
case KeyEvent.VK_UP: game.rotate(-1);
break;
case KeyEvent.VK_DOWN: game.rotate(+1);
break;
case KeyEvent.VK_LEFT: game.move(-1);
break;
case KeyEvent.VK_RIGHT: game.move(+1);
break;
case KeyEvent.VK_SPACE: game.dropDown();
game.score += 1;
break;
}
}
public void keyReleased (KeyEvent e)
{
}
});
// Make the falling piece drop every second
new Thread()
{
@Override
public void run()
{
while (true)
{
try
{
Thread.sleep(1000);
game.dropDown();
}
catch ( InterruptedException e )
{
}
}
}
}.start();
}
}
这是“猜谜游戏”程序(代码)。
package guessinggame;
import java.util.Scanner;
public class GuessingGame
{
private int upperBound;
public GuessingGame(int stop)
{
upperBound = stop;
}
public void playGame()
{
Scanner keyboard = new Scanner(System.in);
int num = (int)(Math.random() * upperBound) + 1;
int guesses = 0;
System.out.print("Enter a number between 1 - " + upperBound + ": ");
int i = 0;
do
{
i = keyboard.nextInt();
if (i > num)
{
System.out.print("Too high. Try another number. :: ");
}
if (i < num)
{
System.out.print("Too low. Try another number. :: ");
}
guesses++;
}
while (i != num);
System.out.println("Correct!");
double percentage;
percentage = (double)(guesses) / upperBound;
int percen = (int)(percentage * 100);
System.out.println("It took " + guesses + " guesses to guess the number " + num + ".\n" +
"You guessed " + percen + "% of the possible values.");
}
}
package guessinggame;
import java.util.Scanner;
public class GuessingGameRunner
{
public static void main(String args[])
{
Scanner keyboard = new Scanner(System.in);
String response;
int top;
do
{
System.out.print("Guessing Game. Range of highest number? :: ");
top = keyboard.nextInt();
GuessingGame run = new GuessingGame(top);
run.playGame();
System.out.print("Do you want to play again? (y, n) :: ");
response = keyboard.next();
}
while (response.equals("y"));
}
}
这是按钮程序(代码)。
package button;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Buttons extends Frame
{
public Buttons()
{
super("");
this.setLayout(new BorderLayout());
//this.add("North", new Label("North", Label.LEFT));
Panel centerPanel = new Panel(new GridLayout(2, 2, 5, 5));
centerPanel.add(new Button("Button 0"));
centerPanel.add(new Button("Button 1"));
centerPanel.add(new Button("Button 2"));
centerPanel.add(new Button("Button 3"));
this.add("Center", centerPanel);
Panel southPanel = new Panel(new GridLayout(1, 2));
//Label test = new Label("Test");
//test.setBackground(Color.YELLOW);
//southPanel.add(test);
Panel panel = new Panel(new GridLayout(2, 1));
Label test1 = new Label("Test1");
test1.setBackground(Color.RED);
Label test2 = new Label("Test2");
test2.setBackground(Color.GREEN);
panel.add(test1);
panel.add(test2);
//southPanel.add("South", southPanel);
this.add("South", southPanel);
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
this.setSize(400, 200);
this.setVisible(true);
}
public static void main(String[] args)
{
new Buttons();
}
}
所以我希望在单击“按钮 0”时运行第一个程序,在单击“按钮 1”时运行第二个程序。顺便说一句,我正在使用 Eclipse。
谢谢!!!
最佳答案
由于每个游戏类都有其自己的 main 方法
,因此您只需使用要启动的游戏的相应 main 方法即可。
由于main方法是静态的
不需要任何初始化,只需 -
GuessingGame.main();
或
Tetris.main();
关于java - 如何制作一个按钮程序? ( java ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48365280/
我想在一个页面上做一个按钮,可以在同一页面调用一个JS函数。该函数将需要创建(打开)新窗口,其 HTML 代码由 JS 函数本身提供。我该怎么做? 这样做的目的是从特定页面生成一个打印友好的页面。 请
我一直在用 php 开发这个项目。该项目的一半是使用 mysql_query 完成的,最新的模块是使用 mysqli 制作的。有很多模块,我不想更改代码。如果是这样的话会不会产生问题。或者我应该将其全
我安装了好几次 qt creator,但它从来没有像我现在的 PC 那样花钱;首先,我使用我的 Pendrive(Qt 5.8 的)上一直有的安装程序,告诉我我无法下载一些存储库,我下载了相同安装程序
我安装了 Qt Creator 5.10.1,当我构建项目时出现错误:“无法确定要运行哪个”make“命令。检查构建配置中的”make“步骤。”。 我已经在另一台 PC 上安装了 Qt,我看到了这个问
看看这个 makefile,它有某种原始的进度指示(可能是一个进度条)。 请给我建议/意见! # BUILD 最初是未定义的 ifndef 构建 # max 等于 256 个 x 十六:= x x x
这个问题会有点长,对此我很抱歉:) 我花了几天时间寻找最好的解决方案,以在 asp mvc 和 JQuery 中制作图像库。 主要问题是当用户点击拇指时显示图像。 我想让整个浏览器 View 变成黑色
我是Python方面的 super 高手。我一直在努力寻找适当的解决方案。这是列表,L = [0, 0, 0, 3, 4, 5, 6, 0, 0, 0, 0, 11, 12, 13, 14, 0, 0
让我们考虑两个简化的 CMakeLists.txt set(GTEST "/usr/local/lib/libgtest.a") set(GMOCK "/usr/local/lib/libgmock.
我如何制作 Makefile,因为这是按源代码分发程序的最佳方式。请记住,这是针对 C++ 程序的,而我是从 C 开发领域开始的。但是可以为我的 Python 程序制作 Makefile 吗? 最佳答
由于 Ord 是 Eq 的子类,我发现很难理解创建该类的新类型实例的样子。 我已经设法做到了: newtype NT1 = NT1 Integer instance Eq NT1 wh
在 PowerShell 中,我想编写一个函数,它接受不同的选项作为参数。没关系,如果它接收多个参数,但它必须接收至少一个参数。我想通过参数定义而不是之后的代码来强制执行它。我可以使用以下代码让它工作
我正在通过构建包使用 enable-ssl 在 heroku (ubuntu) 上安装 ffmpeg。我能够一直构建到这些错误: install: cannot create regular file
我是 FFmpeg 的新手,但作为一个学习一些 mysql 数据库的项目,我正在尝试创建一个视频上传网站。 当我尝试使用此代码制作缩略图时: shell_exec("/usr/local/bin/ff
我想要一个绘制可绘制对象的 Actor ,但将其剪辑为 Actor 的大小。我从 Widget 派生这个类,并使用一些硬编码的值作为一个简单的测试: public class MyWidget ext
我一直在查看 Faxien+Sinan 和 Rebar,Erlang OTP 的基本理念似乎是,在单个 Erlang 镜像实例上安装应用程序和版本。保持发布自包含的最佳实践是什么?有没有办法打包发布,
我正在尝试克隆存储库,但它应该是彼此独立的副本。这背后有什么魔法吗,或者只是使用 svn 客户端并克隆它? 谢谢 最佳答案 试试 svnadmin hotcopy .您可以在 repo mainten
我想做一个这样的菜单: Item 1 Item 2 Item 3 Subitem 1 Subitem 2 但我得到了这个:
为 Yii 创建扩展的最佳方式是什么? 这是我到目前为止所做的 我希望它可以通过 composer 安装,所以我为它创建了一个 github repo。 我在文件夹 vendor/githubname
我尝试制作一个ActionListener,但它给了我一个错误。我导入了事件,但它仍然不起作用。这是我的代码: send.addActionListener(new jj); private clas
我需要能够将 div 内的 HTML 代码恢复为页面就绪状态。我需要这个,因为我想在页面准备好后对 HTML 代码进行一些更改,然后在需要时将其恢复到页面准备好时的状态.. 我想使用克隆,但是如何只复
我是一名优秀的程序员,十分优秀!