메인 메소드, 생성자 및 initialize() 메소드가있는 MainWindow 클래스가 있습니다. initialize() 메소드는 프레임, Jbutton 및 최종 Jtextarea를가집니다. actionPerformed()는 ActionListener를 처리하는 다른 클래스 Data에 있습니다. MainWindow 클래스의 전용 변수 프레임 안에있는 Jtextfield에서 버튼을 누른 후 텍스트를 표시하고 싶습니다. 응용 프로그램 논리를 언급하지 않았고 GUI와 상호 작용할 수 있도록 도와줍니다. 감사합니다 !!!! ! GUI와 WindowBuilder interaction java의 응용 프로그램 논리?
MainWindow를 클래스 :public class MainWindow {
private JFrame frame;
public Data data;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainWindow window = new MainWindow();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public MainWindow() {
this.data = new Data();
initialize();
}
private void initialize(){
frame = new JFrame();
frame.setBounds(100, 100, 396, 469);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
final JTextArea textarea = new JTextArea();
textarea.setFont(new Font("Dialog", Font.PLAIN, 75));
textarea.setTabSize(15);
textarea.setBounds(12, 28, 370, 85);
frame.getContentPane().add(textarea);
JButton button7 = new JButton("7");
button7.addActionListener(this.data); // Data data class has the actionperformed() method
button7.setActionCommand("7");
button7.setBounds(12, 125, 65, 73);
frame.getContentPane().add(button7);
}
}
그런 다음 클래스 데이터는 다음과 같습니다
public class Data implements ActionListener {
public String s;
public Data(){
//constructor
}
public void actionPerformed(ActionEvent e) {
// this will set string s with some string
// that has to be returned to be displayed
// in the Jtextarea of the frame in MainWindow
}
public string returnString(){
return s;
}
난 그냥
String을 Data 클래스에 설정할 수 있지만 실제 문제는 MainWindow 클래스의 JTextArea 텍스트 영역에서 문자열을 반환하고 설정하는 것입니다. JTextArea를 반환하고 MainWindow 클래스의 JFrame 프레임에 추가하는 것을 도와주십시오 ... 감사합니다 !!! –
생각해 봅시다. 논리적으로 ActionListener는 actionPerformed 내에서 액션을 잡아 코드를 실행하므로 MainWindow 내부에서 코드를 처리하려면 ** MainWindow 내부에서 메소드를 생성하고 ActionListener **에서 호출해야합니다. – larensolarsuss