java - getText() 方法从我的 JTextField 获取什么?

标签 java jframe jbutton jtextfield

我告诉一个按钮的文本更改为名称,而文本更改为:

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);
}
}

最佳答案

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

button2.setText(name2 + "轮到了。");

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

关于java - getText() 方法从我的 JTextField 获取什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27893217/

相关文章:

java - 如何在 Java 中获取 JFormattedTextField 的长度?

java - 表格中按钮的翻转效果?

java - 单击按钮的 JDialog Action 监听器

java - Itext 7 设置字体颜色

java - 有没有办法在 Java 中获取对当前框架的引用?

Java gui框架的第二个函数

java - 单击 JButton Java 时更改字体样式

java - Parcelable 在没有正确实现的情况下工作

java - 提取模式中的动态值并将其放入另一个模式中

java - 为什么 Figwheel 不将编译后的应用程序传递到浏览器?