JAVA GUI设计

9.1AWT和Swing概述

9.1.1AWT概述

图形用户界面GUI(Graphical User Interfaces),给用户提供了一个交互式的直观图形化操作界面。

  • 提供了程序的外观和感觉
  • 每个Java程序利用GUI图形用户界面接受用户的输入,向用户输出程序运行的结果。

Java语言中,为了方便图形用户界面的开发,设计了专门的类库来生成各种标准图形界面元素和处理图形界面的各种事件。用来生成图形用户界面的类库:

  • java.awt包。AWT(abstract window toolkit,抽象窗口工具集)
  • javax.swing包

java.awt包提供了基本的Java程序的GUI设计工具,
包中的主要类或接口之间的继承关系: :
在这里插入图片描述
(1)组件Component
组件是一个以图形化的方式显示在屏幕上并能与用户进行交互的对象,例如Button、Label,组件通常被放在容器中。
Component类是抽象类,定义了所有组件所具有的通用特性和行为,并派生出其他所有的组件。
Compoment类提供的功能:

  • 基本的绘画支持。方法repaint()、paint()、update()等用来在屏幕上绘制组件.
  • 外形控制。包括字体、颜色等。相应的方法有:getFont()、setFont()、setBackground()、SetForeground()等
  • 大小和位置控制。方法有:setSize()、setLocation()等。
  • 图像处理。类Component实现了接口ImageObserver,其中的方法imageUpdate()用来进行图像跟踪。
  • 组件的状态控制。例如:setEnable()控制组件是否接收用户的输入,isEnable(),isVisible()、isValid()返回组件的状态。

(2)容器Container
容器是Component的子类,它具有组件的所有性质,同时又具有容纳其它组件和容器的功能。

  • add()方法向容器添加某个组件,
  • remove()方法从容器中删除某个组件。
    每个容器都与一个布局管理器相联,以确定容器内组件的布局方式。
    容器通过方法setLayout()设置某种布局。

(3)布局管理器LayoutManager
布局管理器管理组件在容器中的布局方式。布局管理器类都实现了接口LayoutManager。
Java系统提供的标准布局管理器类:
FlowLayout、BorderLayout、GridLayout、CardLayout、BoxLayout、GridBagLayout

9.1.2 Swing概述

Swing 组件在javax.swing包中。其特点:
(1) Swing组件是用100%纯Java代码实现的轻量级(light-weight)组件.
没有本地代码,不依赖操作系统的支持,这是它与重量级组件AWT的最大区别。
Swing比AWT组件具有更强的实用性和美观性。
(2) Swing组件的多样化
Swing是AWT的扩展, Swing组件以“J”开头.
有与AWT类似的按钮(JButton)、标签(JLabel)、复选框(JCheckBox)、菜单(JMenu)等基本组件外,
增加了一个丰富的高层组件集合,如表格(JTable)、树(JTree)。
大多数Swing组件从JComponent类继承
JComponent是一个抽象类,,它定义所有子类组件的一般方法,如:
使用setBorder()方法:设置组件外围的边框;
使用setTooltipText()方法:为组件设置对用户有帮助的提示信息。
(3)可插入的外观感觉
Swing使得程序在一个平台上运行时能够有不同的外观。

在这里插入图片描述

  • Swing组件的分类
    从功能上分:
    (1)顶层容器:JFrame,JApplet,JDialog,JWindow共4个
    (2)普通容器:JPanel,JScrollPane,JSplitPane,JToolBar
    (3)特殊容器:在GUI上起特殊作用的中间层JInternalFrame,JLayeredPane,JRootPane.
    (4)基本控件:实现人际交互的组件,如JButton,JComboBox,JList,JMenu,JSlider,JTextField
    (5)向用户显示不可编辑信息的组件.例如JLabel,JProgressBar,ToolTip
    (6)向用户显示能被编辑的格式化信息的组件.如JColorChooser, JFileChooser,JTable,JTextArea

9.2事件处理

在这里插入图片描述
事件处理模型

  1. Java的事件处理模型由三部分组成:
    事件源(Event source):是用户交互的各种GUI组件。
    事件对象(Event object):封装了发生事件的有关信息。
    事件监听器(Event listener):当事件发生时被通知到接受事件的事件监听对象,然后调用事件监听对象中对应的方法响应该事件。
    一个事件监听对象是实现了系统规定的事件监听接口的类的对象。
    事件监听接口中提供了事件处理的抽象方法的描述。
    java.awt.event包定义了Java系统的一组事件监听接口类型,一个监听接口往往声明了一个以上的抽象方法。每个抽象方法对应着要处理的事件动作,由用户实现它。
    在这里插入图片描述
    在这里插入图片描述在这里插入图片描述
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonTest extends JFrame {
   private JButton plainButton, fancyButton;   
   public ButtonTest()                   // set up GUI
   {  super( "JButton的程序设计" );
      Container container = getContentPane();
      container.setLayout( new FlowLayout() );
      plainButton = new JButton( "文本按钮" );
      container.add( plainButton );
      Icon bug1 = new ImageIcon( "bug1.gif" );
      Icon bug2 = new ImageIcon( "bug2.gif" ); 
      fancyButton = new JButton( "文本图像按钮", bug1 ); 
      fancyButton.setRolloverIcon( bug2 ); 
      container.add( fancyButton );
      ButtonHandler handler = new ButtonHandler();
      fancyButton.addActionListener( handler );
      plainButton.addActionListener( handler );
      setSize( 275, 100 );
      setVisible( true );
   } // end ButtonTest constructor
   public static void main( String args[] )
   {   ButtonTest application = new ButtonTest();
       application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
   }
    // inner class for button event handling
   private class ButtonHandler implements ActionListener {
     // handle button event
      public void actionPerformed( ActionEvent event )
     {     JOptionPane.showMessageDialog( ButtonTest.this,
            "你按下的是:  " + event.getActionCommand() );
      }
   }
}

搭建GUI图形界面使用最多的Swing组件:
标签(JLabel)、
单行文本框(JTextField、JTextPassword)
多行文本域(JTextArea)
滚动条面板(JScrollPane)
JLabel标签是用户不能修改只能查看其内容的文本显示区域,它起到信息说明的作用。
JLabel 对象可以显示文本和图像。

在这里插入图片描述

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CheckBoxTest extends JFrame {
   private JTextField field;
   private JCheckBox bold, italic;
   public CheckBoxTest()
   {  super( "JCheckBox 的应用" );
      Container container = getContentPane();
      container.setLayout( new FlowLayout() );
      // set up JTextField and set its font and color
      field = new JTextField( "点击复选框,请观察字体风格的变化", 22 );
      field.setFont( new Font( "Serif", Font.PLAIN, 14 ) );
      field.setForeground(Color.red);
      container.add( field );
      // create checkbox objects
      bold = new JCheckBox( "Bold" );
      container.add( bold );     
      italic = new JCheckBox( "Italic" );
      container.add( italic );
      // register listeners for two JCheckBoxes
      CheckBoxHandler handler = new CheckBoxHandler();
      bold.addItemListener( handler );
      italic.addItemListener( handler ); 
      setSize( 275, 100 );setVisible( true );
   }
   public static void main( String args[] )
   {  CheckBoxTest application = new CheckBoxTest();
      application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );   
   }
   // private inner class for ItemListener event handling
   private class CheckBoxHandler implements ItemListener {
      private int valBold = Font.PLAIN;
      private int valItalic = Font.PLAIN;    
      public void itemStateChanged( ItemEvent event ) 
    {  if ( event.getSource() == bold ) 
         valBold = bold.isSelected() ? Font.BOLD : Font.PLAIN;                        
      if ( event.getSource() == italic ) 
         valItalic = italic.isSelected() ? Font.ITALIC : Font.PLAIN;
      field.setFont( new Font( "Serif", valBold + valItalic, 14 ) );
      }
   }
}

在这里插入图片描述

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class RadioButtonTest extends JFrame {
   private JTextField field;
   private JRadioButton maleButton, womenButton;
   private ButtonGroup radioGroup;   
   public RadioButtonTest()
   {  super( "RadioButton Test" );      
      Container container = getContentPane();
      container.setLayout( new FlowLayout() );
      JLabel label1 = new JLabel( "性别:" );
      container.add( label1  );      
      maleButton = new JRadioButton( "男", true );
      container.add( maleButton );
      womenButton = new JRadioButton( "女", false );
      container.add( womenButton );
      radioGroup = new ButtonGroup();
      radioGroup.add( maleButton );
      radioGroup.add( womenButton );    
      // register events for JRadioButtons
      maleButton.addItemListener( new RadioButtonHandler(  ) );
      womenButton.addItemListener( new RadioButtonHandler(  ) );
      field = new JTextField( "性别初值是: 男" ); 
      container.add( field ); 
      setSize( 300, 100 );
      setVisible( true );
   } 
   public static void main( String args[] )
   {   RadioButtonTest application = new RadioButtonTest();
      application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
   } 
   // private inner class to handle radio button events
   private class RadioButtonHandler implements ItemListener {
      // handle radio button events
      public void itemStateChanged( ItemEvent event )
      {  if (event.getItem()==maleButton)
             field.setText( "性别值是: 男" );
         else if (event.getItem()==womenButton)
             field.setText( "性别值是: 女" );         
      }
   }
     } 

猜你喜欢

转载自blog.csdn.net/AnalogElectronic/article/details/88805111