62,614
社区成员
发帖
与我相关
我的任务
分享
//窗体类
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
/**
* @author ZhengYesheng
*/
public class F extends JFrame
{
private static final long serialVersionUID = 986609219821815561L;
private JPanel contentPane;
private JTextField textField;
private P p;
/**
* Launch the application.
*/
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
F frame = new F();
frame.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public F()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
p = new P(this);
textField = new JTextField();
textField.setBounds(94, 68, 282, 21);
contentPane.add(textField);
textField.setColumns(10);
JButton btnNewButton = new JButton("开始处理");
btnNewButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
p.action();
}
});
btnNewButton.setBounds(182, 158, 117, 23);
contentPane.add(btnNewButton);
}
public JTextField getTextField()
{
return textField;
}
}
//业务逻辑类
/**
* @author ZhengYesheng
*
*/
public class P
{
private F f;
public P(F f)
{
super();
this.f = f;
}
public void action()
{
f.getTextField().setText("处理结果返回来了。");
}
}
类似上面的结构,如果采用面向接口编程,会更灵活。