写了个简单的例子,没有测试过,希望对你有帮助FRAME类: /* * MainFrame.java * * Created on __DATE__, __TIME__ */package learning.swing;/** * * @author __USER__ */ public class MainFrame extends javax.swing.JFrame { private String content="no content"; private ListenerClass listener;//你的监听类
public String getContent() { return content; } public void setContent(String content) { this.content = content; } /** Creates new form MainFrame */ public MainFrame() { initComponents(); }
public MainFrame(ListenerClass listener) { initComponents(); this.listener=listener;//注册 }
public void refresh(String text){ this.setContent(text); jTextArea1.setText(this.content); repaint(); } /** 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. */ //GEN-BEGIN:initComponents // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jScrollPane1 = new javax.swing.JScrollPane(); jTextArea1 = new javax.swing.JTextArea(); jTextArea1.setText(this.content); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jTextArea1.setColumns(20); jTextArea1.setRows(5); jScrollPane1.setViewportView(jTextArea1); getContentPane().add(jScrollPane1,java.awt.BorderLayout.CENTER); pack(); }// </editor-fold> //GEN-END:initComponents /** * @param args the command line arguments */ public static void main(String args[]) { MainFrame testtt=new MainFrame(); ListenerClass test=new ListenerClass(); test.register(testtt); java.awt.EventQueue.invokeLater(new Runnable() { public void run() { testtt.setVisible(true); } }); } //GEN-BEGIN:variables // Variables declaration - do not modify private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTextArea jTextArea1; // End of variables declaration//GEN-END:variables}监听类 public class ListenerClass{ MainFrame frame=null; public ListenerClass(){ } public void register(MainFrame frame){ this.frame=frame; }
public eventOccur(){//事件触发 this.frame.refresh("change text"); } }之后执行eventOccur()就应该可以了
/*
* MainFrame.java
*
* Created on __DATE__, __TIME__
*/package learning.swing;/**
*
* @author __USER__
*/
public class MainFrame extends javax.swing.JFrame { private String content="no content";
private ListenerClass listener;//你的监听类
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
} /** Creates new form MainFrame */
public MainFrame() {
initComponents();
}
public MainFrame(ListenerClass listener) {
initComponents();
this.listener=listener;//注册
}
public void refresh(String text){
this.setContent(text);
jTextArea1.setText(this.content);
repaint();
} /** 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.
*/
//GEN-BEGIN:initComponents
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jTextArea1.setText(this.content); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1); getContentPane().add(jScrollPane1,java.awt.BorderLayout.CENTER); pack();
}// </editor-fold>
//GEN-END:initComponents /**
* @param args the command line arguments
*/
public static void main(String args[]) {
MainFrame testtt=new MainFrame();
ListenerClass test=new ListenerClass();
test.register(testtt);
java.awt.EventQueue.invokeLater(new Runnable() { public void run() {
testtt.setVisible(true);
}
});
} //GEN-BEGIN:variables
// Variables declaration - do not modify
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration//GEN-END:variables}监听类
public class ListenerClass{
MainFrame frame=null; public ListenerClass(){
} public void register(MainFrame frame){
this.frame=frame;
}
public eventOccur(){//事件触发
this.frame.refresh("change text");
}
}之后执行eventOccur()就应该可以了
java api 提供Java.util.Observer接口
System.out.println("一个图形界面!");
} private static JbInit instance = new JbInit(); public static JbInit getInstance() {
return instance;
}
}
调用时 JbInit.getInstance()保证只实现一个JbInit实例。