Posted on 2008-02-09 18:55
kooyee 阅读(1673)
评论(1) 编辑 收藏 所属分类:
Swing/Applet
使用eclipse的插件,首先设计form。然后可以把自己的code,后台操作的代码写入到form的code中.
但是要注意:
/** *//** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
提醒不能修改的地方是不能加入自己的code去修改界面的外观, 即使加入,也不会有什么效果。因为matisse的编辑器回重新生成代码。但可以加入控制代码。而且event响应事件的代码是在这些代码之前生成. 对于起始要调用的代码可以加到form的init()或constructor method中. 对于后台要access到前台界面的control时,可以用setter/getter方法调用属性为private的controls
例子
/**//*
* Test1.java
*
* Created on __DATE__, __TIME__
*/
/** *//**
*
* @author kooyee
*/
public class Test1 extends javax.swing.JApplet {
/** *//** Initializes the applet Test1 */
public void init() {
try {
//init the app class
testApp = new Test1App(this);
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
testApp.init();//call init method in app class
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
/** *//** This method is called from within the init() method to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
//GEN-BEGIN:initComponents
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jLabel1.setText("Modify Text");
jTextField1.setText("jTextField1");
jButton1.setText("Change");
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
jButton1MousePressed(evt);
}
});
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
getContentPane());
getContentPane().setLayout(layout);
layout
.setHorizontalGroup(layout
.createParallelGroup(
org.jdesktop.layout.GroupLayout.LEADING)
.add(
layout
.createSequentialGroup()
.add(30, 30, 30)
.add(jLabel1)
.add(16, 16, 16)
.add(
layout
.createParallelGroup(
org.jdesktop.layout.GroupLayout.LEADING)
.add(jButton1)
.add(
jTextField1,
org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
269,
org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addContainerGap(28, Short.MAX_VALUE)));
layout
.setVerticalGroup(layout
.createParallelGroup(
org.jdesktop.layout.GroupLayout.LEADING)
.add(
layout
.createSequentialGroup()
.add(74, 74, 74)
.add(
layout
.createParallelGroup(
org.jdesktop.layout.GroupLayout.BASELINE)
.add(jLabel1)
.add(
jTextField1,
org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(45, 45, 45).add(jButton1)
.addContainerGap(139, Short.MAX_VALUE)));
}// </editor-fold>//GEN-END:initComponents
//GEN-FIRST:event_jButton1MousePressed
private void jButton1MousePressed(java.awt.event.MouseEvent evt) {
/**//*
* this code is my own code to add in here
*/
testApp.modify();//call modify method of app class
}//GEN-LAST:event_jButton1MousePressed
//GEN-BEGIN:variables
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JTextField jTextField1;
// End of variables declaration//GEN-END:variables
//Myself own app class
private Test1App testApp;
//Add setter/getter to access textbox, label and other controls
public void setJTextField1(String text) {
jTextField1.setText(text);
}
public String getJTextField1() {
return jTextField1.getText();
}
}
app 为后台程序
public class Test1App {
private Test1 test1UI;
//inject ui object to this class
public Test1App(Test1 t1){
this.test1UI = t1;
}
public void init(){
test1UI.setJTextField1("Run init()");
}
public void modify(){
String text = test1UI.getJTextField1();
test1UI.setJTextField1("Run modify() change from " + text);
}
}