zoukankan      html  css  js  c++  java
  • 2016/04/21

    Swing是一个用于开发Java应用程序用户界面的开发工具包。它以抽象窗口工具包(AWT)为基础使跨平台应用程序可以使用任何可插拔的外观风格。Swing开发人员只用很少的代码就可以利用Swing丰富、灵活的功能和模块化组件来创建优雅的用户界面。
    工具包中所有的包都是以swing作为名称,例如javax.swing,javax.swing.event
    用Swing创建图形界面步骤:
    (1)导入Swing包
    (2)选择界面风格
    (3)设置顶层容器
    (4)设置按钮和标签
    (5)将组件放到容器上
    (6)为组件增加边框
    (7)处理事件
    (8)辅助技术支持
    1。导入Swing包
    下面语句导入Swing包
    import javax.swing.*;
    大部分Swing程序用到了AWT的基础底层结构和事件模型,因此需要导入两个包:
    import java.awt.*;
    import java.awt.event.*;
    如果图形界面中包括了事件处理,那么还需要导入事件处理包:
    import javax.swing.event.*;
    2.选择界面风格
    Swing允许选择程序的图形界面风格常用的有java风格,windows风格等
    下面的代码用于选择图形界面风格,这里选择的是跨平台的Java界面风格。
    try { UIManager.setLookAndFeel(
    UIManager.getCrossPlatformLookAndFeelClassName( )); }
    catch (Exception e) { }
    (3) 设置顶层容器
    图形界面至少要有一个顶级Swing容器
    顶级Swing容器为其它Swing组件在屏幕上的绘制和处理事件提供支持
    常用的顶级容器:
    JFrame(框架):表示主程序窗口
    JDialog(对话框):每个JDialog对象表示一个对话框,对话框属于二级窗口
    JApplet(小程序):在浏览器内显示一个小程序界面
    一个框架包括边界、菜单栏、工具栏、状态栏,以及中间占主要部分的窗格
    窗格也可以看作是一种面板,但它是框架的一个组成部分
    组件不会直接放到框架上,而是放在若干个面板上,这些面板再放到窗格上
    用框架对象的getContentPane()函数来获得窗格,再调用窗格的add()函数放置面板
    public static void main(String[ ]args){JFrame frame=new JFrame("SwingApplication");
    JPanel panel1=new JPanel();
    frame.getContentPane().add(panel1,BorderLayout.CENTER);
    ......//添加其他组件
    frame.pack();frame.setVisible(true);}

    package com.wode.test;
    /**
     * 学生管理系统
     */
    import java.awt.Container;

    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JPasswordField;
    import javax.swing.JRadioButton;
    import javax.swing.JTabbedPane;
    import javax.swing.JTextField;

    public class Student extends JFrame{

     public Student(String title){
      super(title);
      this.setSize(400, 300);
      this.setLocationRelativeTo(null);
      this.setLayout(null);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.setResizable(false);
      JLabel jLabelName = new JLabel("用户名:");
      JLabel jLabelpassword = new JLabel("密码:");
      JPanel jPanel = new JPanel();  
      jPanel.setLayout(null);  
      jPanel.setBounds(1, 1, 400, 300);
      jLabelName.setBounds(100, 65, 60, 20);
      jLabelpassword.setBounds(100, 120, 60, 20);
      jPanel.add(jLabelpassword);
      jPanel.add(jLabelName);
      JTextField txtName = new JTextField();
      txtName.setBounds(180, 65, 85, 20);
      jPanel.add(txtName);
      JPasswordField txtpassword = new JPasswordField();
      txtpassword.setBounds(180, 120, 85, 20);
      jPanel.add(txtpassword);
      JButton okbtn=new JButton("确定");
      okbtn.setBounds(110, 200, 60, 40);
      jPanel.add(okbtn);
      JButton cancelbtn=new JButton("取消");
      cancelbtn.setBounds(220, 200, 60, 40);
      jPanel.add(cancelbtn);
      JRadioButton supervise = new JRadioButton("管理登陆");
      supervise.setBounds(100,25, 120, 20);
      jPanel.add(supervise);
      JRadioButton studnet = new JRadioButton("学生登陆");
      studnet.setBounds(220,25, 120, 20);
      jPanel.add(studnet);
      JLabel newName = new JLabel("注册账号");
      newName.setBounds(280, 65, 60, 20);
      jPanel.add(newName);
      JLabel password = new JLabel("密码找回");
      password.setBounds(280, 120, 60, 20);
      jPanel.add(password);
      JCheckBox remember = new JCheckBox("记住密码");
      remember.setBounds(110, 160, 110, 20);
      jPanel.add(remember);
      JCheckBox automatic = new JCheckBox("自动登陆");
      automatic.setBounds(225, 160, 110, 20);
      jPanel.add(automatic);
      this.add(jPanel);
      this.setVisible(true);
      
     }
     public static void main(String[] args) {
      new Student("学生管理系统");
     }
    }

  • 相关阅读:
    Sharding-JDBC(三)3.1.0版本实践
    Sharding-JDBC(二)2.0.3版本实践
    Sharding-JDBC(一)简介
    Java并发(六)线程池监控
    Java并发(五)线程池使用番外-分析RejectedExecutionException异常
    Java并发(四)线程池使用
    Java并发(三)线程池原理
    Java并发(二)异步转同步
    tarjan+概率
    线段树(种树)
  • 原文地址:https://www.cnblogs.com/chenyangpeng/p/5419060.html
Copyright © 2011-2022 走看看