我正在從事一項使用 Java 創建 GUI 的任務。目前,我正在為 Minesweeper 制作布局 - 但我對這種方法的說明有點困惑。
說明如下:
mousePressed 方法
- getActionCommand 獲取字串
- 使用 getComponent 并將其轉換為按鈕
- 將 actionCommand 字串分成一行和一列
- 將行和列的按鈕文本設定為“!”
這就是我想要的mousePressed()
public void mousePressed(MouseEvent b)
{
Component a = b.getComponent();
JButton x = (JButton)a;
System.out.println(x.getActionCommand());
String s = x.getActionCommand();
// substring for row and col
// set text
}
最后兩個怎么做?特別是與
將 actionCommand 字串分成一行和一列
將行和列的按鈕文本設定為“!”
With substring, how would I go and "break" the actionCommand String into a row and column? (Break is not meant to be taken literally). Substring returns part of a string with a start index and end index, but what string from actionCommand would I use for the substring? And how would I set the row and column to show text as "!"? Would I need to use substring for this as well?
Here is my entire code for the layout so far.
import java.awt.GridLayout;
import java.awt.Component;
import java.awt.Label;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class GridOfButtons extends JFrame implements MouseListener
{
private JButton [][] grid;
public GridOfButtons()
{
grid = new JButton[10][10];
BuildGUI();
}
public void BuildGUI()
{
setSize(1200, 800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(10, 10));
setVisible(true);
for(int r = 0; r < grid.length; r )
{
for(int c = 0; c < grid[r].length; c )
{
grid[r][c] = new JButton("*");
grid[r][c].setActionCommand(r ":" c);
addMouseListener(this);
getContentPane().add(grid[r][c]);
}
}
}
public void mouseClicked(MouseEvent b)
{
}
public void mouseEntered(MouseEvent b)
{
}
public void mouseExited(MouseEvent b)
{
}
public void mouseReleased(MouseEvent b)
{
}
public void mousePressed(MouseEvent b)
{
Component a = b.getComponent();
JButton x = (JButton)a;
System.out.println(x.getActionCommand());
String s = x.getActionCommand();
// substring for row and col
// set text
}
public static void main(String[] args)
{
new GridOfButtons();
}
}
These are also the instructions I followed:
Create a class that is a subclass of JFrame that uses interface MouseListener with:
Instance variables:
- a 2d array of JButtons
Default constructor:
- sets the size of the array to 10 by 10
- calls a method to build the GUI
GUI builder method:
- set default close
- sets the layout to GridLayout
- use nested for loops to create the button with initial text "*"
- use setActionCommand to add the row ":" column
- add the Mouse listener
- add the button to the JFrame
mousePressed method:
- getActionCommand to get the String
- use getComponent and cast it to a button
- break the actionCommand String into a row and column
- set the button text of the row and column to "!"
I can't strictly adhere to the text (I'm adding other lines of code that aren't clarified in the instructions), but I can't do anything too advanced either since I'm working under constraints.
uj5u.com熱心網友回復:
我知道,在分配作業時,您通常只能使用到目前為止所學的東西。因此,我知道您只能使用substring(類的java.lang.String)方法來操作每個JButton. (我希望你也可以使用方法indexOf,因為我在下面的代碼中使用了它。)
除了更改JButton文本的 [missing] 代碼之外,您的代碼還有兩個問題。
- 呼叫方法
setVisible應該是方法的最后一行BuildGUI。 - 您正在向
MouseListener,JFrame即向 class添加一個GridOfButtons。您需要為MouseListener每個JButton.
這是您更正的代碼,包括更改JButton文本的代碼。如上所述,我移動了這條線setVisible(true);。我還MouseListener為每個JButton. (請參閱下面代碼中的評論CHANGE HERE。)我添加了代碼來更改JButton方法中的文本mousePressed。
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class GridOfButtons extends JFrame implements MouseListener {
private JButton [][] grid;
public GridOfButtons()
{
grid = new JButton[10][10];
BuildGUI();
}
public void BuildGUI()
{
setSize(1200, 800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(10, 10));
for(int r = 0; r < grid.length; r )
{
for(int c = 0; c < grid[r].length; c )
{
grid[r][c] = new JButton("*");
grid[r][c].setActionCommand(r ":" c);
grid[r][c].addMouseListener(this); // CHANGE HERE
getContentPane().add(grid[r][c]);
}
}
setVisible(true);
}
public void mouseClicked(MouseEvent b)
{
}
public void mouseEntered(MouseEvent b)
{
}
public void mouseExited(MouseEvent b)
{
}
public void mouseReleased(MouseEvent b)
{
}
public void mousePressed(MouseEvent b)
{
Component a = b.getComponent();
JButton x = (JButton)a;
System.out.println(x.getActionCommand());
String s = x.getActionCommand();
// substring for row and col
// set text
int ndx = s.indexOf(':');
if (ndx >= 0) {
String row = s.substring(0, ndx);
String col = s.substring(ndx 1);
String text = row '!' col;
x.setText(text);
}
}
public static void main(String[] args)
{
new GridOfButtons();
}
}
關于上述代碼的一些注釋。
- 建議使用
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/441221.html
