Java

mac2024-07-21  59

题目效果: 实现代码: 窗口类:

package com.event.window; import java.awt.FlowLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextField; public class Win extends JFrame{ private JTextField textField; private JButton transferButton; private Win otherWin; public Win() { this.setLayout(new FlowLayout()); textField=new JTextField(15); transferButton=new JButton("转换"); transferButton.addActionListener(new ButtonListener(this)); this.add(textField); this.add(transferButton); this.setSize(200, 200); this.setLocation(100, 100); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public JTextField getTextField() { return textField; } public void setTextField(JTextField textField) { this.textField = textField; } public JButton getTransferButton() { return transferButton; } public void setTransferButton(JButton transferButton) { this.transferButton = transferButton; } public Win getOtherWin() { return otherWin; } public void setOtherWin(Win otherWin) { this.otherWin = otherWin; } }

按钮监听类:

package com.event.window; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class ButtonListener implements ActionListener{ private Win win; public ButtonListener(Win win) { this.win = win; } @Override public void actionPerformed(ActionEvent e) { String str=win.getTextField().getText(); win.getOtherWin().getTextField().setText(str); } }

测试类

package com.event.window; public class Test { public static void main(String[] args) { Win win1=new Win(); Win win2=new Win(); win1.setOtherWin(win2); win2.setOtherWin(win1); } }
最新回复(0)