Posted on 2008-02-09 18:55 
kooyee 阅读(1697) 
评论(1)  编辑  收藏  所属分类: 
Swing/Applet 
			 
			
		 
		使用eclipse的插件,首先设计form。然后可以把自己的code,后台操作的代码写入到form的code中.
但是要注意:

 /** *//** This method is called from within the constructor to
/** *//** This method is called from within the constructor to
 * initialize the form.
     * initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is
     * WARNING: Do NOT modify this code. The content of this method is
 * always regenerated by the Form Editor.
     * always regenerated by the Form Editor.
 */
     */提醒不能修改的地方是不能加入自己的code去修改界面的外观, 即使加入,也不会有什么效果。因为matisse的编辑器回重新生成代码。但可以加入控制代码。而且event响应事件的代码是在这些代码之前生成. 对于起始要调用的代码可以加到form的init()或constructor method中. 对于后台要access到前台界面的control时,可以用setter/getter方法调用属性为private的controls
例子

 /**//*
/**//*
 * Test1.java
 * Test1.java
 *
 *
 * Created on __DATE__, __TIME__
 * Created on __DATE__, __TIME__
 */
 */


 /** *//**
/** *//**
 *
 *
 * @author  kooyee
 * @author  kooyee
 */
 */

 public class Test1 extends javax.swing.JApplet
public class Test1 extends javax.swing.JApplet  {
{


 /** *//** Initializes the applet Test1 */
    /** *//** Initializes the applet Test1 */

 public void init()
    public void init()  {
{

 try
        try  {
{
 //init the app class
            //init the app class
 testApp = new Test1App(this);
            testApp = new Test1App(this);

 java.awt.EventQueue.invokeAndWait(new Runnable()
            java.awt.EventQueue.invokeAndWait(new Runnable()  {
{

 public void run()
                public void run()  {
{
 initComponents();
                    initComponents();
 testApp.init();//call init method in app class
                    testApp.init();//call init method in app class
 }
                }
 });
            });

 } catch (Exception ex)
        } catch (Exception ex)  {
{
 ex.printStackTrace();
            ex.printStackTrace();
 }
        }
 }
    }


 /** *//** This method is called from within the init() method to
    /** *//** This method is called from within the init() method to
 * initialize the form.
     * initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is
     * WARNING: Do NOT modify this code. The content of this method is
 * always regenerated by the Form Editor.
     * always regenerated by the Form Editor.
 */
     */
 //GEN-BEGIN:initComponents
    //GEN-BEGIN:initComponents
 // <editor-fold defaultstate="collapsed" desc=" Generated Code ">
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">

 private void initComponents()
    private void initComponents()  {
{
 jLabel1 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
 jTextField1 = new javax.swing.JTextField();
        jTextField1 = new javax.swing.JTextField();
 jButton1 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

 jLabel1.setText("Modify Text");
        jLabel1.setText("Modify Text");

 jTextField1.setText("jTextField1");
        jTextField1.setText("jTextField1");

 jButton1.setText("Change");
        jButton1.setText("Change");

 jButton1.addMouseListener(new java.awt.event.MouseAdapter()
        jButton1.addMouseListener(new java.awt.event.MouseAdapter()  {
{

 public void mousePressed(java.awt.event.MouseEvent evt)
            public void mousePressed(java.awt.event.MouseEvent evt)  {
{
 jButton1MousePressed(evt);
                jButton1MousePressed(evt);
 }
            }
 });
        });

 org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
 getContentPane());
                getContentPane());
 getContentPane().setLayout(layout);
        getContentPane().setLayout(layout);
 layout
        layout
 .setHorizontalGroup(layout
                .setHorizontalGroup(layout
 .createParallelGroup(
                        .createParallelGroup(
 org.jdesktop.layout.GroupLayout.LEADING)
                                org.jdesktop.layout.GroupLayout.LEADING)
 .add(
                        .add(
 layout
                                layout
 .createSequentialGroup()
                                        .createSequentialGroup()
 .add(30, 30, 30)
                                        .add(30, 30, 30)
 .add(jLabel1)
                                        .add(jLabel1)
 .add(16, 16, 16)
                                        .add(16, 16, 16)
 .add(
                                        .add(
 layout
                                                layout
 .createParallelGroup(
                                                        .createParallelGroup(
 org.jdesktop.layout.GroupLayout.LEADING)
                                                                org.jdesktop.layout.GroupLayout.LEADING)
 .add(jButton1)
                                                        .add(jButton1)
 .add(
                                                        .add(
 jTextField1,
                                                                jTextField1,
 org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
                                                                org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
 269,
                                                                269,
 org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                                                org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
 .addContainerGap(28, Short.MAX_VALUE)));
                                        .addContainerGap(28, Short.MAX_VALUE)));
 layout
        layout
 .setVerticalGroup(layout
                .setVerticalGroup(layout
 .createParallelGroup(
                        .createParallelGroup(
 org.jdesktop.layout.GroupLayout.LEADING)
                                org.jdesktop.layout.GroupLayout.LEADING)
 .add(
                        .add(
 layout
                                layout
 .createSequentialGroup()
                                        .createSequentialGroup()
 .add(74, 74, 74)
                                        .add(74, 74, 74)
 .add(
                                        .add(
 layout
                                                layout
 .createParallelGroup(
                                                        .createParallelGroup(
 org.jdesktop.layout.GroupLayout.BASELINE)
                                                                org.jdesktop.layout.GroupLayout.BASELINE)
 .add(jLabel1)
                                                        .add(jLabel1)
 .add(
                                                        .add(
 jTextField1,
                                                                jTextField1,
 org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
                                                                org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
 org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                                                                org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
 org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                                                org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
 .add(45, 45, 45).add(jButton1)
                                        .add(45, 45, 45).add(jButton1)
 .addContainerGap(139, Short.MAX_VALUE)));
                                        .addContainerGap(139, Short.MAX_VALUE)));
 }// </editor-fold>//GEN-END:initComponents
    }// </editor-fold>//GEN-END:initComponents

 //GEN-FIRST:event_jButton1MousePressed
    //GEN-FIRST:event_jButton1MousePressed

 private void jButton1MousePressed(java.awt.event.MouseEvent evt)
    private void jButton1MousePressed(java.awt.event.MouseEvent evt)  {
{

 /**//*
        /**//*
 * this code is my own code to add in here
         * this code is my own code to add in here
 */
         */
 testApp.modify();//call modify method of app class
        testApp.modify();//call modify method of app class
 }//GEN-LAST:event_jButton1MousePressed
    }//GEN-LAST:event_jButton1MousePressed

 //GEN-BEGIN:variables
    //GEN-BEGIN:variables
 // Variables declaration - do not modify
    // Variables declaration - do not modify
 private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton1;

 private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel1;

 private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField1;

 // End of variables declaration//GEN-END:variables
    // End of variables declaration//GEN-END:variables

 //Myself own app class
    //Myself own app class
 private Test1App testApp;
    private Test1App testApp;

 //Add setter/getter to access textbox, label and other controls
    //Add setter/getter to access textbox, label and other controls

 public void setJTextField1(String text)
    public void setJTextField1(String text)  {
{
 jTextField1.setText(text);
        jTextField1.setText(text);
 }
    }


 public String getJTextField1()
    public String getJTextField1()  {
{
 return jTextField1.getText();
        return jTextField1.getText();
 }
    }

 }
}app 为后台程序


 public class Test1App
public class Test1App  {
{
 private Test1 test1UI;
    private Test1 test1UI;
 
    
 //inject ui object to this class
    //inject ui object to this class

 public Test1App(Test1 t1)
    public Test1App(Test1 t1) {
{
 this.test1UI = t1;
        this.test1UI = t1;
 }
    }
 
    

 public void init()
    public void init() {
{
 test1UI.setJTextField1("Run init()");
        test1UI.setJTextField1("Run init()");
 }
    }
 
    

 public void modify()
    public void modify() {
{
 String text = test1UI.getJTextField1();
        String text = test1UI.getJTextField1();
 test1UI.setJTextField1("Run modify() change from " + text);
        test1UI.setJTextField1("Run modify() change from " + text);
 }
    }

 }
}