我的任務是創建 2 個 JPanel,一個用于 2d 陣列,6 行 5 個空 JTextField,另一個用于 JButton,應該有點類似于螢屏鍵盤。我用空的 JTextFields 和 JButtons 創建了 JPanel,現在我需要一種方法,這樣當我按下帶有字母表中的字母的 JButton 時,它會將該字母分配給第一個可用行上的第一個可用 JTextField 并在 a 處移動一列直到整行都充滿字母的時間(嘗試將字母添加到整行應該什么都不做)。我還需要創建一個退格按鈕,我擁有該按鈕將洗掉最后一個字母(在空行上按退格鍵應該什么都不做)。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Wordle extends JFrame implements ActionListener
{
private JPanel p1;
private JPanel p2;
private JTextField [][] g;
public Wordle()
{
setSize(500,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p1 = new JPanel();
p1.setLayout(new GridLayout(6, 5));
g=new JTextField [5][6];
for(int r=0; r<g.length; r )
{
for(int c=0; c<g[r].length; c )
{
g[r][c]= new JTextField();
getContentPane().add(g[r][c]);
p1.add(g[r][c]);
}
}
p2 = new JPanel();
p2.setLayout(new GridLayout(4, 7));
JButton a= new JButton("A");
a.addActionListener(this);
p2.add(a);
JButton b= new JButton("B");
b.addActionListener(this);
p2.add(b);
JButton c= new JButton("C");
c.addActionListener(this);
p2.add(c);
JButton d= new JButton("D");
d.addActionListener(this);
p2.add(d);
JButton e= new JButton("E");
e.addActionListener(this);
p2.add(e);
JButton f= new JButton("F");
f.addActionListener(this);
p2.add(f);
JButton g= new JButton("G");
g.addActionListener(this);
p2.add(g);
JButton h= new JButton("H");
h.addActionListener(this);
p2.add(h);
JButton i= new JButton("I");
i.addActionListener(this);
p2.add(i);
JButton j= new JButton("J");
j.addActionListener(this);
p2.add(j);
JButton k= new JButton("K");
k.addActionListener(this);
p2.add(k);
JButton l= new JButton("L");
l.addActionListener(this);
p2.add(l);
JButton m= new JButton("M");
m.addActionListener(this);
p2.add(m);
JButton n= new JButton("N");
n.addActionListener(this);
p2.add(n);
JButton o= new JButton("O");
o.addActionListener(this);
p2.add(o);
JButton p= new JButton("P");
p.addActionListener(this);
p2.add(p);
JButton q= new JButton("Q");
q.addActionListener(this);
p2.add(q);
JButton r= new JButton("R");
r.addActionListener(this);
p2.add(r);
JButton s= new JButton("S");
s.addActionListener(this);
p2.add(s);
JButton t= new JButton("T");
t.addActionListener(this);
p2.add(t);
JButton u= new JButton("U");
u.addActionListener(this);
p2.add(u);
JButton v= new JButton("V");
v.addActionListener(this);
p2.add(v);
JButton w= new JButton("W");
w.addActionListener(this);
p2.add(w);
JButton x= new JButton("X");
x.addActionListener(this);
p2.add(x);
JButton y= new JButton("Y");
y.addActionListener(this);
p2.add(y);
JButton z= new JButton("Z");
z.addActionListener(this);
p2.add(z);
JButton BackSpace= new JButton("<-");
BackSpace.addActionListener(this);
p2.add(BackSpace);
JButton Enter= new JButton("[");
Enter.addActionListener(this);
p2.add(Enter);
this.getContentPane().add(p1,BorderLayout.NORTH);
this.getContentPane().add(p2,BorderLayout.SOUTH);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) //need help with
{
if(e.getSource().equals("A"))
{
for(int r=0; r<g.length; r )
{
for(int c=0; c<g[r].length; c )
{
g[r][c].setText("A");
}
}
}
}
public static void main(String[] args)
{
new Wordle();
}
}
這是我的代碼,我的主要問題是使用 actionlistener 在 JTextFields 的 2d 陣列中設定文本,我不介意為每個字母單獨執行每個 if 回圈,只要它有意義并且按照我的預期作業,順便說一句如果您還沒有注意到這是我正在嘗試制作的游戲“Wordle”,我仍然是一個新手程式員,所有這些對我來說都是新的,因此非常感謝某種直觀的解釋。
uj5u.com熱心網友回復:
這條線if(e.getSource().equals("A"))是你的第一個問題。它根本不起作用,因為e.getSource()回傳一個永遠不會相等的物件"A"。
相反,您需要首先將源轉換為正確的物件型別,如下所示:
public void actionPerformed(ActionEvent e) //need help with
{
JButton clicked = (JButton)e.getSource();
現在我們可以使用getText()這樣的用法從按鈕中獲取文本(盡管我們可以在下面使用更智能的解決方案):
public void actionPerformed(ActionEvent e) //need help with
{
JButton clicked = (JButton)e.getSource();
if(clicked.getText().equals("A")) {
...
您面臨的下一個問題是如何存盤資料,您需要使用類變數來跟蹤當前單元格,而不是僅僅填充整個陣列:
private int row = 0;
private int column = 0;
那么所有的變化可能看起來有點像這樣:
//Class variables
private int row = 0;
private int column = 0;
public void actionPerformed(ActionEvent e) //need help with
{
//Cast the event source to a button
JButton clicked = (JButton)e.getSource();
//Handle backspace
if(clicked.getText().equals("<-"))
{
//Decrease the tracking number
column--;
//Make sure that only valid cells are back spaced
if(column < 0)
{
column = 0;
}
//Set the cell to be blank
g[row][column].setText("");
}
//Handle enter
else if(clicked.getText().equals("["))
{
//Add your behaviour here
....
//Finally move to the next row
row ;
}
//Handle the rest of the letter buttons here
else
{
//We don't need `for` loops, we can just directly add the letter from
//the clicked button to the correct place in the array like so
g[row][column].setText(clicked.getText);
//Increment the tracking numbers
column ;
//Move to the next row when the column value is over 5:
if(column > 4)
{
//Move to the next row
row ;
//Reset the column number to 0
column = 0;
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/477988.html
上一篇:超出面板的按鈕問題(JAVA)
