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

时间:2015-01-11 23:14:39

标签: java jframe jbutton jtextfield

我告诉一个按钮的文字更改为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);
}
}

1 个答案:

答案 0 :(得分:3)

name2name JTextField对象因此您要将按钮文本设置为对象内存地址信息:

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

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