GUI开发的MVC-->静夜思转移
/*
* DemoFrameGUI.java
*
* Created on 2007-8-21, 10:41:58
*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author vlinux
*/
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class DemoFrameGUI extends JFrame {
protected JPanel panel;
protected JLabel label;
protected JButton button;
public DemoFrameGUI() {
panel = new JPanel();
label = new JLabel( "false" );
button = new JButton( "Click Me!" );
panel.add(label);
panel.add(button);
this.getContentPane().add(panel);
this.setBounds(100, 200, 300, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
/**
* this is a test for view the GUI
*/
public static void main( String... args ) {
new DemoFrameGUI();
}
}
* DemoFrameGUI.java
*
* Created on 2007-8-21, 10:41:58
*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author vlinux
*/
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class DemoFrameGUI extends JFrame {
protected JPanel panel;
protected JLabel label;
protected JButton button;
public DemoFrameGUI() {
panel = new JPanel();
label = new JLabel( "false" );
button = new JButton( "Click Me!" );
panel.add(label);
panel.add(button);
this.getContentPane().add(panel);
this.setBounds(100, 200, 300, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
/**
* this is a test for view the GUI
*/
public static void main( String... args ) {
new DemoFrameGUI();
}
}
在GUI类中构造好界面,然后用一个类去继承,并且添加Listener,这样我们就实现界面和动作的分离了。
这只是一个设计的模式而已,我在学习JAVA中总结出来的,希望对想学JAVA GUI的朋友有所帮助
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/*
* DemoFrame.java
*
* Created on 2007-8-21, 10:48:19
*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author vlinux
*/
public class DemoFrame extends DemoFrameGUI {
public DemoFrame() {
button.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent event) {
if( label.getText().equals("false") ) {
label.setText("true");
} else {
label.setText("false");
}
}
});
}
public static void main( String... args ) {
new DemoFrame();
}
}
import java.awt.event.ActionListener;
/*
* DemoFrame.java
*
* Created on 2007-8-21, 10:48:19
*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author vlinux
*/
public class DemoFrame extends DemoFrameGUI {
public DemoFrame() {
button.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent event) {
if( label.getText().equals("false") ) {
label.setText("true");
} else {
label.setText("false");
}
}
});
}
public static void main( String... args ) {
new DemoFrame();
}
}