【问题标题】:What is the getText() method getting from my JTextField?从我的 JTextField 获取的 getText() 方法是什么?
【发布时间】:2015-01-11 23:14:39
【问题描述】:

我告诉一个按钮的文本更改为name,而不是文本更改为:

javax.swing.JTextField[,3,140,200x25,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@2d5921cd,flags=296,maximumSize=,minimumSize=,preferredSize=,caretColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],disabledTextColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],selectionColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],columns=0,columnWidth=0,command=,horizontalAlignment=LEADING]'s turn.

为什么,这是什么,我该如何解决? 这是代码:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;


public class Project {
static JTextField name = new JTextField();
static JTextField name2 = new JTextField();
static String name1 = name.getText();
static String nameTwo = name2.getText();
static JFrame frame = new JFrame("frame");
static JFrame frame2 = new JFrame("2nd frame");
static JButton button = new JButton("Submit");
static JButton button2 = new JButton(name1 + "'s turn.");
static JButton button3 = new JButton("Press");
static boolean a = true;

//actionlistener for 'button3'
static class Press implements ActionListener{
    public void actionPerformed(ActionEvent e){
        if(a==true){
        button2.setText(name2 + "'s turn.");
        a=false;
        } else {
            button2.setText(name + "'s turn");
            a=true;
        }
        //getting the button's text so I could copy it because it wouldn't fit on the button.
        System.out.println(button2.getText());
    }
}

//actionlistener for 'button'
static class Submit implements ActionListener{
    public void actionPerformed(ActionEvent e){
        frame.setVisible(false);
        frame2.setVisible(true);
        name1 = name.getText();
        nameTwo = name2.getText();
        button2.setText(name1 + "'s turn.");

    }
}

public static void main(String[] args){
    //setting up 'frame'
    frame.setLayout(null);
    frame.setSize(400, 400);
    frame.setVisible(true);
    //setting up 'frame2'
    frame2.setLayout(null);
    frame2.setSize(400, 400);
    //setting bounds
    name.setBounds(3, 70, 200, 25);
    name2.setBounds(3, 140, 200, 25);
    button.setBounds(220, 70, 100, 90);
    button2.setBounds(3, 200, 300, 75);
    button3.setBounds(0, 0, 300, 75);
    //adding actionlisteners
    button.addActionListener(new Submit());
    button3.addActionListener(new Press());
    //adding buttons to 'frame2'
    frame2.add(button3);
    frame2.add(button2);
    //adding buttons to 'frame'
    frame.add(name);
    frame.add(name2);
    frame.add(button);
}
}

【问题讨论】:

  • 从删除 static 引用开始。使用事件发生时该字段的实际值,否则将一无所获

标签: java jframe jbutton jtextfield


【解决方案1】:

name2nameJTextField 对象,因此您将按钮文本设置为来自此处的对象内存地址信息:

button2.setText(name2 + "'s turn.");

应该是button2.setText(name2.getText() + "'s turn."); 从您输入的 JTextField 中获取实际文本。

【讨论】:

  • 还建议清理变量名,那一团混乱:P
  • @MadProgrammer 是的,很抱歉。
  • @MadProgrammer,你知道我答案顶部的信息是什么吗?
  • @CadeLikesToCode 它将自动调用该对象的 tostring 方法。 Returns a string representation of this component and its values.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-05
  • 2013-02-10
  • 2023-02-10
  • 2021-05-04
  • 2020-10-16
  • 2014-06-01
  • 1970-01-01
相关资源
最近更新 更多