2015-01-11 117 views
0

我告訴一個按鈕的文字更改爲name,取而代之的是文字更改爲:什麼是從我的JTextField getText()方法?

javax.swing.JTextField[,3,140,200x25,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,bo[email protected]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); 
} 
} 
+2

從擺脫靜態引用開始。在事件發生時使用該字段的實際值,否則你什麼也得不到 – MadProgrammer

回答

3

name2name的JTextField對象爲此要設置從這裏的按鈕文本對象的內存地址信息:

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

應該button2.setText(name2.getText() + "'s turn.");從您輸入的JTextField中獲取實際文本。

+0

也建議清理變量名稱,即一個混亂的混亂:P – MadProgrammer

+0

@MadProgrammer對不起。 – CadeLikesToCode

+0

@MadProgrammer,你知道我回答頂部的那條消息是什麼嗎? – CadeLikesToCode

-3

我剛剛意識到我應該寫button2.setText(nameTwo + "'s turn");,因爲nameTwo = name2.getText();。我使用了錯誤的變量。 @Rod_Algonquin,你幫我意識到這一點,但你沒有完全的答案。

+0

如果我將自己的答案格式化爲錯誤,我很抱歉。我不確定是否應該像我自己或其他人那樣寫它。 – CadeLikesToCode

相關問題