많은 질문을 많이 받았지만 문제가 해결되지 않았습니다. 나는 자바에서 상당히 새로운 편이다. JTextField
에서 일부 입력을 얻으려고하고 String
으로 반환하므로 다른 클래스에서 비교할 때 사용할 수 있습니다. 이것은 내가 대답으로 보는 것입니다, 나는이 클래스의 다른 부분에서 str
을 사용할 수 있기를 바랍니다.자바 스윙이 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);
}
});
}
}
정적 변수를 사용하지 마십시오! – camickr
간단한'actionListener'와'field.getSource()'는 당신을 놀라게 할 것입니다. – Tdorno