好的,所以我们制作了一个像俄罗斯方块一样的游戏,所以我得到了如何在JTable上放置一个块(我们使用JTable作为棋盘),现在我需要为游戏设置控件而我不知道怎么做。如何将块移动到左侧或右侧?我应该把它放在哪里?
这是代码
public class TetrisTable extends JFrame {
JFrame frame;
TestPane pane = new TestPane();
char ch;
public TetrisTable() {
pane.setTimer();
}
public void start() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
frame = new JFrame("WORD OF TETRIS");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(pane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JTable table;
private TetrisTabelModel model;
private int currentRow = 0;
private int currentColumn = 0;
private int blockHeight = 1;
private int blockWidth = 0;
public TestPane() {
model = new TetrisTabelModel();
table = new JTable(model);
table.setDefaultRenderer(Integer.class, new TetrisTabelCellRenderer());
table.setRowHeight(48);
Enumeration<TableColumn> columns = table.getColumnModel().getColumns();
while (columns.hasMoreElements()) {
TableColumn column = columns.nextElement();
column.setPreferredWidth(48);
column.setMinWidth(48);
column.setMaxWidth(48);
column.setWidth(48);
}
setLayout(new GridBagLayout());
add(table);
}
public void setTimer() {
Timer timer = new Timer(1500, new ActionListener() { //dropper
@Override
public void actionPerformed(ActionEvent e) {
int col = (model.getColumnCount() - blockWidth) / 2;
int row = currentRow - blockHeight;
if (row + blockHeight >= model.getRowCount()) {
((Timer) e.getSource()).stop();
} else {
drawShape(row, col, ' ');
currentRow++;
row = currentRow - blockHeight;
drawShape(row, col, ch);
System.out.println(ch);
}
}
public void drawShape(int row, int col, char color) { //naggawa ng shape
for (int index = 0; index < blockHeight; index++) {
if (row >= 0 && row < model.getRowCount()) {
switch (index) {
case 0:
case 1:
model.setValueAt(color, row, col);
break;
case 2:
model.setValueAt(color, row, col);
model.setValueAt(color, row, col + 1);
model.setValueAt(color, row, col + 2);
break;
}
}
row++;
}
}
});
timer.setRepeats(true);
timer.setCoalesce(true);
timer.start();
}
public class TetrisTabelModel extends AbstractTableModel {
private char[][] values;
public TetrisTabelModel() {
values = new char[10][5];
}
@Override
public int getRowCount() {
return values.length;
}
@Override
public int getColumnCount() {
return values[0].length;
}
@Override
public Class<?> getColumnClass(int columnIndex) {
return Integer.class;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return values[rowIndex][columnIndex];
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
values[rowIndex][columnIndex] = (char) aValue;
fireTableCellUpdated(rowIndex, columnIndex);
}
}
}
public class TetrisTabelCellRenderer extends DefaultTableCellRenderer { // naguupdate ng table //
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, "", false, false, row, column);
setOpaque(true);
/*
if (value != null) {
if (value == 0) {
setBackground(Color.WHITE);
} else if (value == 1) {
setBackground(Color.RED);
} else if (value == 2) {
setBackground(Color.GREEN);
} else if (value == 3) {
setBackground(Color.BLUE);
} else if (value == 4) {
setBackground(Color.YELLOW);
}
*/
if (value != null) {
if ((char)value == ch) {
setText(value.toString());
setBackground(Color.YELLOW);
}
else if ((char)value == ' ') {
setText(value.toString());
setBackground(Color.WHITE);
}
}
else {
setBackground(Color.DARK_GRAY);
}
return this;
}
}
public void setRand(char ch) {
this.ch = ch;
}
public char getRand() {
return ch;
}
}`
这是GUI(主要)
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Random;
public class Input_GUI extends JFrame
{
private Label lbl1;
private Label lbl2;
private TextField tf1;
private TextField tf2;
private TextField tf3;
private TextField tf4;
private TextField tf5;
private Button btn;
public Input_GUI ()
{
super ("Input_GUI");
getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS)); /* PARA UMAYOS POSITION NG TEXTFIELD */
/* YUNG LABELS */
lbl1 = new Label("Welcome to Word of Tetris");
add(lbl1);
lbl2 = new Label("Input 5 words (5 letter words only):");
add(lbl2);
/* YUNG 5 TEXTBOXES */
tf1 = new TextField("",10); add(tf1);
tf2 = new TextField("",10); add(tf2);
tf3 = new TextField("",10); add(tf3);
tf4 = new TextField("",10); add(tf4);
tf5 = new TextField("",10); add(tf5);
btn = new Button ("Play"); /* BUTTON PARA SA PLAYLISTENER SA PINAKABABA */
add(btn);
btn.addActionListener(new PlayListener());
setTitle("Word Of Tetris");
setSize(200, 250);
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
setVisible(true);
setLocationRelativeTo(null);
setResizable(false);
}
/* PARA PALABASIN YUNG FRAME */
public static void main(String[] args)
{
Input_GUI inpt = new Input_GUI();
}
/* PAPALABASIN YUNG BOARD AT ILALAGAY SA TEMP VARIABLE YUNG CHARACTERS NG 5 STRINGS WITH RANDOM */
private class PlayListener implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
TetrisTable gb = new TetrisTable();
String stf1,stf2, stf3, stf4, stf5;
stf1 = tf1.getText(); System.out.printf("tf1 = %s\n",stf1);
stf2 = tf2.getText(); System.out.printf("tf2 = %s\n",stf2);
stf3 = tf3.getText(); System.out.printf("tf3 = %s\n",stf3);
stf4 = tf4.getText(); System.out.printf("tf4 = %s\n",stf4);
stf5 = tf5.getText(); System.out.printf("tf5 = %s\n",stf5);
/* CONVERTION OF STRING TO CHARACTER */
StringBuilder sb = new StringBuilder();
String tempstring = sb.append(stf1).append(stf2).append(stf3).append(stf4).append(stf5).toString();
char[] characters = tempstring.toCharArray();
/*PARA SA PAGPAPALABAS NG LETTERS, KAYA 47, KASI TINAKE COUNT KO NA KAPAG 46 NA YUNG NAKA
PILE UP NA TILES SA BOARD, WHICH IS GAMEOVER NA */
Random random = new Random();
int index = random.nextInt(25);
gb.setRand(characters[index]);
System.out.println(gb.getRand());
gb.start();
}
}
}
答案 0 :(得分:0)
JFrame使用BorderLayout
。
所以我猜你会把tetris板添加到框架的中心。
然后,对于控件,您可以将包含控件的面板添加到“LINE_START”,“LINE_END”,“PAGE_START”或“PAGE_END”,具体取决于您希望框架的外观。
阅读How to Use BorderLayout上Swing教程中的部分,了解更多信息和工作示例。
另外,不要在Swing应用程序中使用Label,TextField等。这些是AWT组件。相反,在使用Swing时,您应该使用JLabel
,JTextField
等。