JAVA GUI

基本概念

AWT与SWING的区别

实现原理
轻量级控件和重量级控件

AWT 中的图形函数与操作系统所提供的图形函数之间有着一一对应的关系,我们把它称为“peers”。
标准版的Java中则提倡使用Swing, 也就是通过牺牲速度来实现用程序的功能。

回调函数

是指通过函数参数传递到其它代码的,某一块可执行代码的引用。这一设计允许了底层代码调用在高层定义的子程序。
java中使用接口实现函数的回调。

一、GUI初步——界面内容的设计

参考:

http://blog.csdn.net/lijizh1013/article/details/7989784

先明白awt的组件的继承树:容器和控件
两类容器
1 顶层的window(frame和dialog),可以作为独立的桌面应用显示
2 仅作为下层容器的panel,虽然是容器,但是也必须依附于frame或者dialog。

两种形成界面的方式
a 在main函数中,声明各个组件,将相应的组件添加到容器中,像这样:

public static void main(String args[]){
        //top-level container
       Frame frame=new Frame("myFrame");
       frame.setLayout(new FlowLayout());
       frame.setSize(200,100);
        //控件
       Label lbl=new Label("myLabel");
       lbl.setBackground(Color.pink);
       //各组件 独立创建,通过add关联
       frame.add(lbl);       
       frame.setVisible(true);
      }

b 继承frame,自定义frame或者dialog类中,在构造函数中,组织这个窗口程序的逻辑(较为常用)。

public class MyFrame extends Frame {
    Button myButton = new Button("CLICK");
    Label myLabel = new Label("text");
    public MyFrame() {
        this.setLayout(new FlowLayout());
        this.setSize(200,100);
        this.add(myButton);
        this.add(my label);
    }
    public static void main(String args[]){
    MyFrame tmp = new MyFrame ();

    }

}  

二、用户操作响应——监听

事件监听和处理的根本技术,是回调。
You implement an action listener to define what should be done when an user performs certain operation.
不同的事件监听需要实现不同的接口。
Java添加事件监听的四种方法代码实例
http://www.jb51.net/article/55452.htm

之前我们先了解一下内部类的知识,对于能访问的内容和可见性
http://www.cnblogs.com/dolphin0520/p/3811445.html
几种不同的内部类
一般来说,匿名内部类用于继承其他类或是实现接口,并不需要增加额外的方法,只是对继承方法的实现或是重写。

成员内部类可以无条件访问外部类的所有成员属性和成员方法(包括private成员和静态成员)。
原理性的知识:
在外部类被声明的时候,内部类并没有被创建,所以外部类的函数需要调用内部类的函数,需要new之后才能使用。
(可以百度——类的加载机制~)

代码设计技巧:
设置listener的实现类时,可以添加有参的构造函数,以获取模型的数据和更改相应的窗口部件
btBlue.addActionListener(new ColorEventListener(this));
//把模型类添加到listener中,以便访问操作数据

界面有多个按钮,对于一个监听类,怎么一一识别,赋予不同的操作。
Event类:
public Object getSource()
The object on which the Event initially occurred.
返回的object可以直接和声明的控件做==比较(监听类实现在frame子类自身)

当然,如果使用单独的监听类,可以使用event的getsource然后使用组件的name做字符串比对来实现。(注意类型转换,getSource返回的Object)
控件有如下方法:
public setName()
public getName()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
javaGUI图形界面 public class login extends JFrame { private JComboBox nameJComboBox; private JPanel userJPanel; private JLabel pictureJLabel; private JButton okJButton,cancelJButton; private JLabel nameJLabel,passwordJLabel,note; private JPasswordField passwordJPasswordField; private String name1; private String password1; private String user; private ImageIcon myImageIcon; public login( ) { createUserInterface(); // 调用创建用户界面方法 } private void createUserInterface() { Container contentPane = getContentPane(); contentPane.setLayout( null ); userJPanel = new JPanel(); userJPanel.setBounds( 35, 120, 300, 96 ); userJPanel.setBorder(BorderFactory.createEtchedBorder() ); //显示一圈边儿 userJPanel.setLayout( null ); contentPane.add( userJPanel ); nameJComboBox = new JComboBox(); nameJComboBox.setBounds( 100, 12, 170, 25 ); nameJComboBox.addItem( "admin" ); nameJComboBox.addItem( "aloie" ); nameJComboBox.setSelectedIndex( 0 ); nameJComboBox.setEditable(true); userJPanel.add( nameJComboBox ); pictureJLabel=new JLabel(); pictureJLabel.setBounds(45,0,380,118); pictureJLabel.setIcon(new ImageIcon("pic.gif")); contentPane.add(pictureJLabel); nameJLabel=new JLabel("姓 名:"); nameJLabel.setBounds(20,12,80,25); userJPanel.add(nameJLabel); passwordJPasswordField=new JPasswordField(); passwordJPasswordField.setBounds(100,60,170,25); userJPanel.add(passwordJPasswordField); passwordJLabel=new JLabel("密 码:"); passwordJLabel.setBounds(20,60,80,25); userJPanel.add(passwordJLabel); note=new JLabel("密码与用户名相同"); note.setBounds(0,295,180,25); add(note); okJButton=new JButton("登 陆"); okJButton.setBounds(60,250,80,25); contentPane.add(okJButton); okJButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { okJButtonActionPerformed(event); } } ); cancelJButton=new JButton("取 消"); cancelJButton.setBounds(210,250,80,25); contentPane.add(cancelJButton); cancelJButton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { System.exit(0); //退出登陆 } } ); setTitle( "登陆窗口" ); setSize( 380, 350 ); setResizable( false ); //将最大化按钮设置为不可用 } private void okJButtonActionPerformed( ActionEvent event ) { //okJButton响应事件,检查用户名和密码的匹配 name1= nameJComboBox.getSelectedItem().toString(); if (name1.equals("admin") ) { if (passwordJPasswordField.getText().equals("admin")) { showNewWindow(); setVisible( false); } else { JOptionPane.showMessageDialog( this,"密码错误,拒绝登陆", "密码错误 !", JOptionPane.ERROR_MESSAGE ); } } else if (name1.equals("aloie")) { if ( passwordJPasswordField.getText().equals("aloie") ) { showNewWindow(); setVisible(false); } else { JOptionPane.showMessageDialog( this,"密码错误,拒绝登陆", "密码错误 !", JOptionPane.ERROR_MESSAGE ); } } } public void showNewWindow() { JFrame jf=new JFrame("main Frame"); jf.setSize(500,400); jf.setVisible(true); jf.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } public static void main( String[] args ) { JFrame.setDefaultLookAndFeelDecorated(true); login mylogin = new login( ); mylogin.setVisible( true ); mylogin.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值