swing讲解

如何创建更好看的界面
1.导入swing包,里面有更好看的组件
2.创建各个组件的实例,然后添加到面板
import java.awt.*;
import javax.swing.*; //swing包中的组件是从awt包扩展而来的,这些组件更好看
 
知识点
1.Java 基本类库 (JFC)
JFC 通过添加一组 GUI 类库扩展了原始 AWT
JFC提供附加的可视化组件类以及屏幕设计的独特方式
JFC 是一组 API,包括以下的一些模块:
Swing 组件集
可访问性 API
拖放 API
Java 2D API
2.Swing介绍
Java1.2引入称为Swing的新的GUI组件库。
Swing包括javax.swing包及其子包。
Swing有一个与平台无关的实现,而且具有一个艺术状态的属性集。
尽管Swing独立于AWT,但它是依照基本的 AWT类实现的
3.Swing 组件
Swing 组件独立于本地窗口系统。
Swing组件除了 AbstractButton 类之外都以 J 开头。
Swing 组件是基于 AWT 构建。
包含 AWT 可视化组件的替代组件,也包含复杂组件 - 树和表
4.Swing 应用程序的容器层次
设计 GUI 时,都有用于放置可视化组件的主窗口。
Container 对象可用于将组件组合在一起。
容器中的组件根据特定布局排列。
Swing 中的容器有两类:
顶级容器
中间容器
5.顶级容器
JFrame:用于框架窗口的类,此窗口带有边框、标题、用于关闭和最小化窗口的图标等。带 GUI 的应用程序通常至少使用一个框架窗口。
JDialog:用于对话框的类。
JApplet:用于使用 Swing 组件的 Java Applet 的类。
6.中间容器
JPanel:最灵活、最常用的中间容器。
JScrollPane:与 JPanel 类似,但还可在大的组件或可扩展组件周围提供滚动条。
JTabbedPane:包含多个组件,但一次只显示一个组件。用户可在组件之间方便地切换。
JToolBar:按行或列排列一组组件(通常是按钮)。
 
 
7.基本的 Swing 应用程序
import javax.swing.*;       
public class HelloSwing {
 public static void main(String[] args) {
   JFrame frame = new JFrame("HelloSwing");
   JLabel label = new JLabel("你好,Swing");
   frame.getContentPane().add(label);              
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭程序,以后用它方便
   frame.setSize(300,200);
   frame.setVisible(true);
 }
}
8. JFrame
是放置其他 Swing 组件的顶级容器
JFrame 组件用于在 Swing 程序中创建窗体
它的构造函数:
JFrame()
JFrame(String Title)
组件必须添加至内容窗格,而不是直接添加至 JFrame 对象,示例:
frame.getContentPane().add(b);
9. JPanel
JPanel 组件是一个中间容器
用于将小型的轻量级组件组合在一起
JPanel 的缺省布局为 FlowLayout
JPanel 具有下列构造函数:
JPanel()
JPanel(LayoutManager lm)
10.JButton
Swing 的按钮相对于AWT中Button类提供了更多的功能。
JButton类允许用图标、字符串或两者同时构造一个按钮。
构造函数如下:
JButton()
JButton(Icon icon):icon表示使用的图标
JButton(String text):text表示使用的字符串
JButton(String text, Icon icon)
ImageIcon buttonIcon1 = new ImageIcon("on.gif"); //实例化图标对象
JButton b1 = new JButton("First Button", buttonIcon1);// 创建一个带图JButton
b1.setMnemonic(KeyEvent.VK_F);// 设置键盘快捷方式
11.JPanel和JButton使用示例
import java.awt.*;
import javax.swing.*;
class PanelDemo extends JFrame {
 public PanelDemo( String title){
 super(title);
 Container c=getContentPane();
    JPanel cpane = new JPanel();
    JButton ok = new JButton("确定");
    cpane.add(ok);
    JButton cancel = new JButton("取消");
    cpane.add(cancel);
    c.add(cpane,BorderLayout.SOUTH);
   }
 
public static void main(String args[]) {
    PanelDemo pd=new PanelDemo("JPanel测试");
    pd.setSize(300,200);
    pd.setVisible(true);
 }
}
12.JLabel
它既可以显示文本也可以显示图像
构造函数如下:
JLabel(Icon icon):icon表示使用的图标
JLabel(String text,Icon icon,int align):text表示使用的字符串; icon表示使用的图标;align表示水平对齐方式,其值可以为:LEFT、RIGHT、CENTER。
ImageIcon icon = new ImageIcon("Calv.gif");
JLabel calv_label = new JLabel("这是 Calvin", icon,
                              SwingConstants.LEFT);
13.JTextField
JTextField 组件允许输入或编辑单行文本
此类的构造函数包括:
JTextField()
JTextField(Document doc, String text, int columns)
JTextField(int columns)
JTextField(String text)
JTextField(String text, int columns)
 
Container con = getContentPane();
con.setLayout(new FlowLayout());
JLabel jl = new JLabel(“文本域”);
con.add(jl);
JTextField tf = new JTextField(20);
con.add(tf);
14. JTextArea
JTextArea 组件用于接受来自用户的多行文本
它可实现可滚动界面
JTextArea 组件可使用下列构造函数创建:
JTextArea()
JTextArea(int rows, int cols)
JTextArea(String text)
JTextArea(String text, int rows, int cols)
JTextArea(Document doc)
JTextArea(Document doc, String text, int rows, int cols)
JLabel jl = new JLabel(“文本区”);
con.add(jl);
JTextArea ta = new JTextArea(5,10);
con.add(ta);
15. 复选框
复选框用于为用户提供一组选项
JCheckBox 类具有下列构造函数:
JCheckBox()
JCheckBox(Icon icon)
JCheckBox(Icon icon, boolean selected)
JCheckBox(String text)
JCheckBox(String text, boolean selected)
JCheckBox(String text, Icon icon)
JCheckBox(String text, Icon icon, boolean selected)
16.单选按钮
单选按钮允许用户从多个选项中选择其中一个
ButtonGroup 用于在 Swing 中创建组
JRadioButton 对象可使用下列构造函数创建:
JRadioButton()
JRadioButton(Icon icon)
JRadioButton(Icon, boolean selected)
JRadioButton(String text)
JRadioButton(String text, boolean selected)
JRadioButton(String text, Icon icon)
JRadioButton(String text, Icon icon, boolean selected)
17. JCheckBox和JRadioButton使用示例
import java.awt.*;
import javax.swing.*;
class Hobby extends JPanel {
 JCheckBox c1 = new JCheckBox("阅读",false);
 JCheckBox c2 = new JCheckBox("音乐",false);
 JCheckBox c3 = new JCheckBox("绘画",false);
 JRadioButton rad1 = new JRadioButton("大专");
 JRadioButton rad2 = new JRadioButton("本科");
 JRadioButton rad3 = new JRadioButton("硕士");
 JLabel jl = new JLabel("您有什么爱好?" );
 JLabel j2 = new JLabel("您的最高学历?" );
 JButton exitbtn = new JButton("退出");
 public Hobby( ) {
     setLayout(new GridLayout(9,1));
     add(jl); add(c1); add(c2); add(c3);
     add(j2); add(rad1); add(rad2); add(rad3); add(exitbtn);
   }
}
public class Hobbytest extends JFrame {
   Hobbytest() {
      super();
      getContentPane().add(new Hobby());
      setSize(300,200); setVisible(true);
   }
 public static void main(String args[]) {
    new Hobbytest(); }
}
18. 列表
在可供选择的选项很多时,可向用户呈现一个列表来供他们选择
JList 组件依次排列项目列表,这些项目可以单选或多选
JList 类既可显示字符串,也可显示图标
JList 不支持双击
MouseListener 可用于解决双击问题
JList 构造函数
public JList() : 使用空模型构造 JList
public JList(ListModel dataModel) :构造一个列表,用它显示指定模型中的元素。
public JList (Object [] listData) :构造一个列表以显示指定数组listData的元素。
JList 不支持滚动。要启用滚动,可使用下列代码:
   JScrollPane myScrollPane=new JScrollPane();
myScrollPane.getViewport().setView(dataList);
19. 组合框
文本域和下拉列表的组合
在 Swing 中,组合框由 JComboBox 表示
构造函数如下:
public JComboBox() : 此构造函数使用缺省数据模型创建 JComboBox
public JComboBox(ComboBoxModel asModel) : 使用现有 ComboBoxModel 中的项目的组合框
public JComboBox(Object [] items) : 包含指定数组元素的组合框
String names[] = {"弗雷德里克.福西斯", "约翰.克里沙姆", "玛丽.希金斯.克拉克","帕特丽夏.康威尔"};
JComboBox authors = new JComboBox(names);
20. JMenuBar
JMenuBar 是可通过 JFrame、JWindow 或 JInternalFrame 的根窗格添加至容器的组件。
由多个 JMenu 组成,每个 JMenu 在 JMenubar 中都表示为字符串。
JMenuBar 需要两个附加类:
SingleSelectionModel类 : 跟踪当前选定的菜单
LookAndFeel类 :负责绘制菜单栏以及对在其中发生的事件作出响应
JMenu
JMenu 在 JMenuBar 下以文本字符串形式显示,而在用户单击它时,则以弹出式菜单显示。
JMenu 具有两个附加类:
JPopupMenu :用于显示 JMenu 的菜单项
LookAndFeel :负责绘制菜单栏中的菜单以及对在其中发生的所有事件作出响应
 
JPopupMenu 的函数
public JMenuItem add(JMenuItem menuItem)
将指定菜单项附加至菜单末尾
public JMenuItem add(String s)
使用指定文本新建菜单项,并将其添加至菜单末尾
public void show(Component c, int x, int y)
在组件c的坐标 (x,y) 位置显示弹出式菜单
public boolean isPopupTrigger()
确定鼠标事件是否为平台的弹出触发器
JMenuItem
JMenu 或 JPopupMenu 中的一个组件,以文本字符串形式显示,可以具有图标
JMenuItem 的外观可以修改,如字体、颜色、背景、边框等
除字符串外,在 JMenuItem 中还可以添加图标
JCheckBoxMenuItem
将复选框作为其项目
复选框是使用 JCheckBox 类创建的
可有文本字符串和(或)图标
在单击并释放 JCheckBoxMenuItem 时,菜单项的状态会变为选定或取消选定
JRadioButtonMenuItem
除了在任何时间点都只能选择一个单选按钮外,其他的与复选框类似
可有文本字符串和(或)图标
单击选定的单选按钮不会改变其状态
单击未选定的单选按钮时将取消选定此前选定的单选按钮
 
import javax.swing.*;
import java.awt.*;
public class Menutest extends JApplet {
 public void init() {
 JMenuBar mb = new JMenuBar();
 JMenu fileMenu = new JMenu("显示");
 JMenu pullRightMenu = new JMenu(“问好");
 fileMenu.add("欢迎");
 fileMenu.addSeparator();
 fileMenu.add(pullRightMenu);
 fileMenu.add("退出");
 pullRightMenu.add(new JCheckBoxMenuItem("早上好!"));
 pullRightMenu.add(new JCheckBoxMenuItem("下午好!"));
 pullRightMenu.add(new JCheckBoxMenuItem("晚安!再见!"));
 mb.add(fileMenu);   setJMenuBar(mb);
 }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值