JAVA SWING:无法更改JTextField的文本
我正在开发一个程序,使用SwingGUI在java中生成和解决数独难题。我遇到了一个问题,在JTextField上使用.setText()方法,但是文本没有更新 这是我的密码: 主要类别:JAVA SWING:无法更改JTextField的文本,java,swing,user-interface,jtextfield,Java,Swing,User Interface,Jtextfield,我正在开发一个程序,使用SwingGUI在java中生成和解决数独难题。我遇到了一个问题,在JTextField上使用.setText()方法,但是文本没有更新 这是我的密码: 主要类别: package sudoku; public class SudokuSolver { public static void main(String[] args) { GUI gui = new GUI(); gui.setVisible(true);
package sudoku;
public class SudokuSolver {
public static void main(String[] args) {
GUI gui = new GUI();
gui.setVisible(true);
}
}
GUI类:
package sudoku;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.Window.Type;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GUI extends JFrame implements ActionListener {
private JPanel contentPane;
private Puzzle puzzle = new Puzzle();
private Board board = new Board();
private int[][] puzz = new int[9][9];
// GUI Constructor
public GUI() {
// set up window
setResizable(false);
setTitle("Sudoku Solver");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 300, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
// set up button panel
JPanel buttons = new JPanel();
contentPane.add(buttons, BorderLayout.SOUTH);
// set up generate button
JButton genButton = new JButton("Generate");
genButton.setMnemonic('g');
buttons.add(genButton);
genButton.addActionListener(this);
// set up solve button
JButton solveButton = new JButton("Solve");
solveButton.setMnemonic('s');
buttons.add(solveButton);
solveButton.addActionListener(this);
// set up board
contentPane.add(board, BorderLayout.CENTER);
}
// Button listener
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (cmd == "Generate") {
// generate puzzle
puzz = puzzle.generate();
} else if (cmd == "Solve") {
// solve puzzle
puzz = puzzle.solve(puzz);
}
// display puzzle on the board
board.fill(puzz);
}
}
董事会级别:
package sudoku;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
public class Board extends JPanel {
// 9x9 board sections
private BoardSection nw = new BoardSection();
private BoardSection n = new BoardSection();
private BoardSection ne = new BoardSection();
private BoardSection w = new BoardSection();
private BoardSection c = new BoardSection();
private BoardSection e = new BoardSection();
private BoardSection sw = new BoardSection();
private BoardSection s = new BoardSection();
private BoardSection se = new BoardSection();
// array of sections
private BoardSection[] sections = { nw, n, ne, w, c, e, sw, s, se };
// Board Constructor
public Board() {
// 3x3 grid layout
setLayout(new GridLayout(3, 3));
// border
setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
// add board sections to board
for (int i = 0; i < sections.length; i++) {
sections[i] = new BoardSection();
add(sections[i]);
}
}
// fill the board with data
public void fill(int[][] data) {
// create data sections
String[][] nwData = new String[3][3];
String[][] nData = new String[3][3];
String[][] neData = new String[3][3];
String[][] wData = new String[3][3];
String[][] cData = new String[3][3];
String[][] eData = new String[3][3];
String[][] swData = new String[3][3];
String[][] sData = new String[3][3];
String[][] seData = new String[3][3];
// break data into data sections
nwData = createSection(data, 0, 0);
nData = createSection(data, 3, 0);
neData = createSection(data, 6, 0);
wData = createSection(data, 0, 3);
cData = createSection(data, 3, 3);
eData = createSection(data, 6, 3);
swData = createSection(data, 0, 6);
sData = createSection(data, 3, 6);
seData = createSection(data, 6, 6);
// fill board section with data section
nw.fillSection(nwData);
n.fillSection(nData);
ne.fillSection(neData);
w.fillSection(wData);
c.fillSection(cData);
e.fillSection(eData);
sw.fillSection(swData);
s.fillSection(sData);
se.fillSection(seData);
}
// split data into 3x3 section with 0,0 starting at x, y then convert to
// string
private String[][] createSection(int[][] data, int x, int y) {
int[][] intSection = new int[3][3];
String[][] strSection = new String[3][3];
// break into section
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
intSection[i][j] = data[i + x][j + y];
}
}
// convert section to string
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
strSection[i][j] = Integer.toString(intSection[i][j]);
}
}
return strSection;
}
}
我没有收到任何错误消息,但是它没有更新文本。我试着用“Z”替换数据[I][j],看看问题是否出在我传递的数据上,但仅仅用一个简单的字符串还是不行。但是,我确实尝试将下面的行放入BoardSection构造函数方法内部的for循环中,它确实显示了出来
fields[i].setText("0");
有人能解释为什么它在构造函数中工作,而不是在调用fillSection()方法时工作吗?调用setText()
只会更新JTextField
的模型。换句话说,JTextField
存储一个内部字符串,其中包含您发送的值。但是,视图(即屏幕上显示的内容)不会更新。这实际上是一件好事,因为绘画速度很慢。如果每次在数独程序中调用setText()
时都会更新UI,那么它必须更新81次
您可以通过对组件的任何子类调用invalidate()
来强制更新视图。在您的情况下,您可能应该在主JFrame
或包含所有JTextField
的JPanel
上调用invalidate()
。这将导致GUI刷新一次,而不是前面提到的81次
另外,有关更多信息,我建议您研究模型-视图-控制器设计模式。您的意思是只填写第一个字段吗?您从未将x
变量更新为0
以外的任何变量
public void fillSection(String[][] data) {
data = new String[3][3];
int x = 0; // This never changes?
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
fields[x].setText(data[i][j]);
}
}
}
或者相应地修改x
参数
已更新…
您遇到的另一个问题是对传递给fillSection
方法的数据变量进行阴影处理
public void fillSection(String[][] data) {
// Oh look, I've overridden every thing
// you just gave with my own copy of the data!!
data = new String[3][3];
//...
}
已更新…
不知道这是否相关,但这仍然是一个问题
您的String
比较是错误的…=
检查两个对象是否共享相同的内存空间,这在String
中永远不会是正确的(以这种方式)
相反,您应该使用String#equals
来比较对象的内容
if ("Generate".equals(cmd)) {
这意味着puzz
从未更新过,因此当您将其传递到字段时,它可能是空的
已更新…
还有一个问题;)
在Board
类中,您正在定义9个Board部分
private BoardSection nw = new BoardSection();
private BoardSection n = new BoardSection();
private BoardSection ne = new BoardSection();
private BoardSection w = new BoardSection();
private BoardSection c = new BoardSection();
private BoardSection e = new BoardSection();
private BoardSection sw = new BoardSection();
private BoardSection s = new BoardSection();
private BoardSection se = new BoardSection();
// array of sections
private BoardSection[] sections = {nw, n, ne, w, c, e, sw, s, se};
但是在构造器中,您正在重新编译它们(在部分中)
数组中)
首先,repaitmanager
将重新绘制请求合并为尽可能少的调用,因此您可以随时调用repaint
,并且repaitmanager
将决定何时以及重新绘制多少。第二,我不确定你的答案是否成立,即使是在最简单的测试中,调用setText
也会立即在屏幕上更新…invalidate
只处理组件的布局,它会产生副作用。@madProgrammergah!我被使用invalidate()
搞混了……我想在回答这些问题之前我需要好好研究一下。谢谢,我错过了。我在setText()之后添加了“x++;”。但是没有解决问题。也解决了阴影问题,谢谢。我不知道我在想什么,加上这个。但仍然没有解决问题。我用你的补丁编辑了我的主要帖子。轰!上次的更新就是问题所在。现在一切正常。非常感谢。作为提示,调试器是您的朋友;)-我建议您启动一个调试器,在程序运行时查看代码中变量的值。
public void fillSection(String[][] data) {
// Oh look, I've overridden every thing
// you just gave with my own copy of the data!!
data = new String[3][3];
//...
}
if (cmd == "Generate") {
if ("Generate".equals(cmd)) {
private BoardSection nw = new BoardSection();
private BoardSection n = new BoardSection();
private BoardSection ne = new BoardSection();
private BoardSection w = new BoardSection();
private BoardSection c = new BoardSection();
private BoardSection e = new BoardSection();
private BoardSection sw = new BoardSection();
private BoardSection s = new BoardSection();
private BoardSection se = new BoardSection();
// array of sections
private BoardSection[] sections = {nw, n, ne, w, c, e, sw, s, se};
// Board Constructor
public Board() {
//...//
// add board sections to board
for (int i = 0; i < sections.length; i++) {
// Look ma, new Board!!
sections[i] = new BoardSection();
add(sections[i]);
}
}
public class Board extends JPanel {
// array of sections
private BoardSection[] sections;
// Board Constructor
public Board() {
//...//
// add board sections to board
sections = new BoardSection[9];
for (int i = 0; i < sections.length; i++) {
sections[i] = new BoardSection();
add(sections[i]);
}
}
// fill the board with data
public void fill(int[][] data) {
// create data sections
String[][] nwData = new String[3][3];
String[][] nData = new String[3][3];
String[][] neData = new String[3][3];
String[][] wData = new String[3][3];
String[][] cData = new String[3][3];
String[][] eData = new String[3][3];
String[][] swData = new String[3][3];
String[][] sData = new String[3][3];
String[][] seData = new String[3][3];
// break data into data sections
nwData = createSection(data, 0, 0);
nData = createSection(data, 3, 0);
neData = createSection(data, 6, 0);
wData = createSection(data, 0, 3);
cData = createSection(data, 3, 3);
eData = createSection(data, 6, 3);
swData = createSection(data, 0, 6);
sData = createSection(data, 3, 6);
seData = createSection(data, 6, 6);
// fill board section with data section
sections[0].fillSection(nwData);
sections[1].fillSection(nData);
sections[2].fillSection(neData);
sections[3].fillSection(wData);
sections[4].fillSection(cData);
sections[5].fillSection(eData);
sections[6].fillSection(swData);
sections[7].fillSection(sData);
sections[8].fillSection(seData);
}