Java 从JTextField(字符串到字符)获取输入文本以显示在JButton中
(新重写的代码)。。我只知道“b”永远是我创建的最后一个框,所以我不能在actionPerformed中使用b作为等式。如何包含所有按钮?有人能帮我吗? 导入java.awt。; 导入javax.swing。; 导入java.awt.event.*Java 从JTextField(字符串到字符)获取输入文本以显示在JButton中,java,swing,button,actionlistener,Java,Swing,Button,Actionlistener,(新重写的代码)。。我只知道“b”永远是我创建的最后一个框,所以我不能在actionPerformed中使用b作为等式。如何包含所有按钮?有人能帮我吗? 导入java.awt。; 导入javax.swing。; 导入java.awt.event.* public class Lat1 extends JFrame implements ActionListener { final int ROWS = 12; final int COLS = 12; final st
public class Lat1 extends JFrame implements ActionListener
{
final int ROWS = 12;
final int COLS = 12;
final static int topLeftNum[][]= {
{-1, 1, 0, 2, 0, 0, 3, -1, 4, 0, 5, 0},
{6, 0, 0, 0, -1, -1, 0, -1, -1, -1, 0, -1},
{-1, 0, -1, 0, -1, 7, 0, 0, 8, -1, 0, -1},
{9, 0, 0, 0, 10, -1, -1, -1, 11, 0, 0, -1},
{0, -1, -1, 12, 0, 0, 13, -1, 0, -1, -1, -1},
{0, -1, 14, -1, 0, -1, 0, -1, 15, 0, 0, 16},
{17, 0, 0, 18, 0, -1, 19, 20, 0, -1, -1, 0},
{0, -1, 0, 0, -1, 21, 0, 0, 0, -1, -1, 0},
{22, 23, 0, 0, -1, 0, -1, 0, -1,24, 0, 0},
{-1, 0, -1, 25, 0, 0, -1, 0, -1, 0, -1, -1},
{26, 0, 0, -1, -1, 0, -1, 27, 0, 0, 0, -1},
{-1, -1, -1, -1, -1, 0, -1, 0, -1, 0, -1, -1}
};
Box b;
JTextField t;
char answer;
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run(){ new Lat1(); }
});
}
/*--------------------------------------------------------*/
public Lat1()
{
this.setSize(1000,1000);
this.setVisible(true);
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(ROWS,COLS));
for (int j=0; j<ROWS; j++) {
for (int i=0; i<COLS; i++) {
b = new Box(i, //the boxes index
(topLeftNum[j][i] < 0) ? Color.BLACK : Color.WHITE, //pick the color
topLeftNum[j][i], //the topleft number
answer, //the char inside
this); //the action listener for the button
p1.add(b);
}
}
p1.setVisible(true);
this.getContentPane().add(p1,BorderLayout.CENTER);
this.pack();
JPanel p2 = new JPanel();
t = new JTextField(10);
p2.add(t);
p2.setVisible(true);
this.getContentPane().add(p2,BorderLayout.SOUTH);
}
/*-----------------------------------------------------------*/
public void actionPerformed(ActionEvent e)
{
for (int j=0; j<ROWS; j++) {
for (int i=0; i<COLS; i++) {
String numstr = String.valueOf(topLeftNum[j][i]);
if(e.getSource() == numstr && (t.getText()).length() == 1)
answer = t.getText().charAt(0);
b.setText(""+ answer);
//b.setText(String.valueOf(t.getText().charAt(0));
}
}
}
}
/*------------------------------------------------------------*/
class Box extends JButton
{
public int index;
private String topLeftNum;
public Box(int index, Color color, int topLeftNum, char c, ActionListener al)
{
this.setBackground(color);
if (color != Color.BLACK) {
this.setFont(new Font("SansSerif", Font.ITALIC, 20));
this.setText(""+ c);
this.addActionListener(al);
this.index = index;
if (topLeftNum != 0)
this.topLeftNum = topLeftNum+""; }
else {
this.setText("");
this.setEnabled(false);
return; }
}
public void paintComponent(Graphics g)
{
super.paintComponent(g); // paints background
g.setFont(new Font("SansSerif", Font.PLAIN, 8));
if (topLeftNum != null) g.drawString(topLeftNum, 5, 10);
}
}
/*----------------------------------------------------------*/
公共类Lat1扩展JFrame实现ActionListener
{
最终整数行=12;
最终整数COLS=12;
最终静态int-topLeftNum[]]={
{-1, 1, 0, 2, 0, 0, 3, -1, 4, 0, 5, 0},
{6, 0, 0, 0, -1, -1, 0, -1, -1, -1, 0, -1},
{-1, 0, -1, 0, -1, 7, 0, 0, 8, -1, 0, -1},
{9, 0, 0, 0, 10, -1, -1, -1, 11, 0, 0, -1},
{0, -1, -1, 12, 0, 0, 13, -1, 0, -1, -1, -1},
{0, -1, 14, -1, 0, -1, 0, -1, 15, 0, 0, 16},
{17, 0, 0, 18, 0, -1, 19, 20, 0, -1, -1, 0},
{0, -1, 0, 0, -1, 21, 0, 0, 0, -1, -1, 0},
{22, 23, 0, 0, -1, 0, -1, 0, -1,24, 0, 0},
{-1, 0, -1, 25, 0, 0, -1, 0, -1, 0, -1, -1},
{26, 0, 0, -1, -1, 0, -1, 27, 0, 0, 0, -1},
{-1, -1, -1, -1, -1, 0, -1, 0, -1, 0, -1, -1}
};
方框b;
jtextfieldt;
答案;
公共静态void main(字符串[]args)
{
SwingUtilities.invokeLater(新的Runnable()
{
public void run(){new Lat1();}
});
}
/*--------------------------------------------------------*/
公共图书馆1()
{
此.setSize(10001000);
此.setVisible(true);
JPanel p1=新的JPanel();
p1.设置布局(新网格布局(行、列));
对于(int j=0;j首先,您在主类的构造函数的第59行抛出了NullPointerException。您试图从未初始化的字段中getText
其次,您可以调用repaint方法,因此标签更改代码可能如下所示:
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER) {
Field f = (Field)e.getSource();
Box b = f.box;
b.setText(f.getText());
b.repaint();
this.remove(f);
this.add(f.box, b.index);
}
}
这可能会迫使JButton改变它的外观
(下一篇文章的建议:请在代码中添加缩进)重新绘制可以,但我建议您拨打电话
SwingUtilities.updateComponentTreeUI(b);
这将刷新整个组件和所有子组件
N您应该使用ActionListener,而不是KeyListener。虽然这不是您问题的原因,但使用JTextField时,它是更好的设计。据我所知,您实际上并没有将TextField添加到任何内容中,而且actionPerformed内部的处理由于对象之间的不匹配而无法编译(由“e.getSource()”和int(您的“b.index”值)返回的类)
此外,“b”始终是您创建的最后一个“框”。您离这里的解决方案还有很长的路要走。谢谢您的建议。我一定会记住这一点。我已经用“字段f”对字段进行了初始化;还是我错了?初始化意味着创建字段类的实例并将其分配给f,例如字段f=新字段(新框(…),这);所以,我应该把“字段f=new Field();”?但是,我又犯了一个错误。“无法解析符号构造函数字段()操作类字段”我必须把初始化字段放在哪里?如果您不介意的话,请提供更多详细信息。字段构造函数是公共字段(框框,keylister kl),所以您不能写字段f=new Field();在本例中,您必须提供参数:Box实例和KeyListener。可以将初始化放置在变量声明所在的位置或Lat1类的构造函数中。-1 updateComponentTreeUI(…)用于LAF更改,不应该仅仅因为您不知道根本问题是什么而使用。因为我不熟悉它,所以我不使用它。我的坏。虽然这可以工作,但它不是正确的方法。对于if-else,我使用什么?例如字符串s=e.getActionCommand();if(s.equalas(“?”)我应该用什么替换“?”使用?JButton上甚至没有名字(例如main=new Button(“main”)。有没有办法添加文本字段而不会弄乱gridLayout?因为我已经将gridLayout设置为COLS和ROWS。使用不同的布局制作另一个面板(现在,尝试一些简单的方法,例如BorderLayout);将现有面板添加到它的中心组件中;将文本字段添加为SOUTH,将此新面板粘贴到jframe中,而不是现有面板,然后尝试一下,看看会发生什么。现在我唯一的问题是按钮(框)。我如何才能使其仅使用白色按钮?如果我不能使用b,那么我应该使用什么?