【问题标题】:get input Text from JTextField (string to char) to show up in JButton从 JTextField 获取输入文本(字符串到字符)以显示在 JButton 中
【发布时间】:2010-11-24 21:08:00
【问题描述】:

(新重写的代码).. 我只知道“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 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);

    }

}
/*----------------------------------------------------------*/

【问题讨论】:

    标签: java swing button actionlistener


    【解决方案1】:

    首先,您在主类的构造函数的第 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 改变其外观。

    (对您下一篇文章的建议:请在您的代码中添加缩进)

    【讨论】:

    • 感谢您的建议。我一定会记住的。我已经通过输入“Field f;”来初始化该字段还是我错了?
    • Initialized 意味着创建一个 Field 类的实例并将其分配给 f,例如字段 f = new Field(new Box(...), this);
    • 所以,我应该把“Field f = new Field();”?但是后来,我又遇到了一个错误。 "无法解析符号构造函数 Field() 位置类字段"
    • 我必须把初始化字段放在哪里?如果您不介意,请提供更多详细信息。
    • Field构造函数是public Field(Box box, KeyListener kl),所以不能写Field f = new Field();您必须提供参数,在这种情况下:Box 实例和 KeyListener。初始化可以放在变量声明所在的地方,也可以放在 Lat1 类的构造函数中。
    【解决方案2】:

    您应该使用 ActionListener 而不是 KeyListener。虽然这不是您的问题的原因,但使用 JTextField 是更好的设计。

    【讨论】:

    • 我在 if-else 中使用什么?例如字符串 s = e.getActionCommand(); if(s.equalas("???")) 我应该用什么替换 "???"和? JButton 甚至没有名字(例如 main = new Button("main").
    【解决方案3】:

    据我所知,您实际上并没有将 TextField 添加到任何内容 - 由于 Object 之间的不匹配(“e.getSource()”返回的类和 int (你的“b.index”值)。

    此外,“b”始终是您创建的最后一个“盒子”。您离解决方案还有很长的路要走。

    【讨论】:

    • 有没有办法在不弄乱gridLayout的情况下添加TextField?因为我已经将 GridLayout 设置为 COLS 和 ROWS。
    • 用不同的布局制作另一个面板(现在,试试像 BorderLayout 这样简单的东西);将现有面板作为 CENTER 组件添加到其中;将文本字段添加为 SOUTH,将这个新面板粘贴到 jframe 中而不是现有的面板中,然后尝试一下,看看会发生什么。
    • 现在我唯一的问题是按钮(框)。我怎样才能使它只有白色按钮有效?如果我不能使用 b,那我应该使用什么?
    【解决方案4】:

    重绘会起作用,但我建议致电

    SwingUtilities.updateComponentTreeUI(b);
    

    这将刷新整个组件以及所有子组件。

    没有

    【讨论】:

    • -1 updateComponentTreeUI(...) 用于 LAF 更改,不应仅仅因为您不知道根本问题是什么而使用。
    • 因为不熟悉,所以没用过。
    • 我的错。虽然这会起作用,但这不是正确的方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-06
    • 1970-01-01
    相关资源
    最近更新 更多