2013-05-06 99 views
2

我看到類似這樣的問題,但它沒有回答我的問題。我在Java方面很新。我試圖從JTextField獲得一些輸入,並將其作爲String返回,以便我可以在不同的課程中使用它進行比較。這是我看到的答案,我希望能夠在班級的任何其他部分使用strJava swing從JTextField獲取輸入

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Container; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JTextArea; 

public class ClassFrame extends JFrame { 

private static final long serialVersionUID = 2451829341034438685L; 

public static JButton inputButton = new JButton("Send"); 
public static JTextArea editTextArea = new JTextArea("Type Here!"); 
public static JTextArea uneditTextArea = new JTextArea(); 

public ClassFrame(String title) { 
    //SET LAYOUT MANAGER (How it arranges components) 
setLayout(new BorderLayout()); 
//////CREATE SWING COMPONENTS//////////// 
//OUTPUT TEXT AREA 
uneditTextArea.setEditable(false); 

//INPUT TEXT AREA 
editTextArea.setBackground(Color.BLUE); 
editTextArea.setForeground(Color.WHITE); 

//SET CONTENT PANE 
Container c = getContentPane(); 

//ADD COMPONENTS TO CONTENT PANE   
c.add(uneditTextArea, BorderLayout.CENTER); 
c.add(editTextArea, BorderLayout.SOUTH); 
c.add(inputButton, BorderLayout.WEST); 

ClassFrame.inputButton.addActionListener(new ActionListener() { 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     String str = editTextArea.getText(); 
     editTextArea.setText(" "); 
     System.out.println(str);     
    } 
}); 
} 
} 
+2

不要使用靜態變量! – camickr 2013-05-06 00:37:09

+0

一個簡單的'actionListener'和'field.getSource()'會爲你創造奇蹟。 – Tdorno 2013-05-06 00:48:09

回答

5

查看我的評論。

package applet; 

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Container; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 


import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JTextArea; 


public class ClassFrame extends JFrame { 


    private static final long serialVersionUID = 2451829341034438685L; 

    public static JButton inputButton = new JButton("Send"); 
    public static JTextArea editTextArea = new JTextArea("Type Here!"); 
    public static JTextArea uneditTextArea = new JTextArea(); 

    // MA - Your String, defined here and usable throughout the class 

    private String myString; 

    public ClassFrame(String title) { 

     // MA - Indent your code properly so that it's more readable to both you 
     // and others 

     //SET LAYOUT MANAGER (How it arranges components) 
     setLayout(new BorderLayout()); 
     //////CREATE SWING COMPONENTS//////////// 
     //OUTPUT TEXT AREA 
     uneditTextArea.setEditable(false); 

     //INPUT TEXT AREA 
     editTextArea.setBackground(Color.BLUE); 
     editTextArea.setForeground(Color.WHITE); 

     //SET CONTENT PANE 
     Container c = getContentPane(); 

     //ADD COMPONENTS TO CONTENT PANE   
     c.add(uneditTextArea, BorderLayout.CENTER); 
     c.add(editTextArea, BorderLayout.SOUTH); 
     c.add(inputButton, BorderLayout.WEST); 

     ClassFrame.inputButton.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 

       // MA - Using the class field myString to receive text from text area 

       myString = editTextArea.getText(); 

       // MA - Don't use a space when you actually want an empty string. 
       // As it stands, your code will test for a single space character. 
       // You really want it to test whether the text area is empty. 

       //editTextArea.setText(" "); 

       // MA - Do this instead. An empty string means the text area has 
       // no input at all. 

       editTextArea.setText(""); 

       System.out.println(myString);     
      } 
     }); 
    } 
} 

我建議你做一些關於Java變量範圍的閱讀。你可以谷歌它。

+0

uneditTextArea在這裏做什麼?在這個代碼示例中顯示無關。 – 2017-08-26 17:23:03

+0

我按照原始海報的要求對代碼示例進行了更改,以演示如何從Swing控件拖動文本。爲此,我既不知道也不關心'uneditTextArea'是什麼。你應該問提供原始代碼樣本的人(假設他在四年後還記得)。 – MarsAtomic 2017-08-27 02:39:03

2

從我對這個問題的理解中,下面是一個比較字符串並將輸出附加到JTextArea的簡單示例。

enter image description here

public class Test 
{ 
    private static String ENTER = "Enter"; 
    static JButton enterButton; 
    public static JTextArea output; 
    public static JTextField input; 
    static JFrame frame; 
    static JPanel panel; 
    public static String testString = "test"; 

    public static void main(String... args) 
    { 
     try 
     { 
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
     } 
     catch (Exception ex) 
     { 
      ex.printStackTrace(); 
     } 
     createFrame(); 
    } 

    public static void createFrame() 
    { 
     frame = new JFrame("Test"); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     panel = new JPanel(); 
     panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); 
     panel.setOpaque(true); 
     ButtonListener buttonListener = new ButtonListener(); 
     output = new JTextArea(15, 50); 
     output.setWrapStyleWord(true); 
     output.setEditable(false); 
     JScrollPane scroller = new JScrollPane(output); 
     scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); 
     scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); 
     JPanel inputpanel = new JPanel(); 
     inputpanel.setLayout(new FlowLayout()); 
     input = new JTextField(20); 
     enterButton = new JButton("Enter"); 
     enterButton.setActionCommand(ENTER); 
     enterButton.addActionListener(buttonListener); 
     // enterButton.setEnabled(false); 
     input.setActionCommand(ENTER); 
     input.addActionListener(buttonListener); 
     DefaultCaret caret = (DefaultCaret) output.getCaret(); 
     caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE); 
     panel.add(scroller); 
     inputpanel.add(input); 
     inputpanel.add(enterButton); 
     panel.add(inputpanel); 
     frame.getContentPane().add(BorderLayout.CENTER, panel); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     // Center of screen 
     // frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
     frame.setResizable(false); 
     input.requestFocus(); 
    } 

    public static class ButtonListener implements ActionListener 
    { 

     public void actionPerformed(final ActionEvent ev) 
     { 
      if (!input.getText().trim().equals("")) 
      { 
       String cmd = ev.getActionCommand(); 
       if (ENTER.equals(cmd)) 
       { 
        output.append(input.getText()); 
        if (input.getText().trim().equals(testString)) output.append(" = " + testString); 
        else output.append(" != " + testString); 
        output.append("\n"); 
       } 
      } 
      input.setText(""); 
      input.requestFocus(); 
     } 
    } 
} 
+1

'JTextArea#append()'在Java 7中不再是線程安全的。爲什麼總是使用'Thread'? – trashgod 2013-05-06 00:58:38

+1

@trashgod我從中提取的項目需要使用線程。我已經刪除它,因爲它不再需要。 – syb0rg 2013-05-06 01:02:00