您的位置:首页 > 编程语言 > Java开发

swing/组件 java(三)

2016-01-06 19:28 363 查看

1.简介

Swing继承AWT,缺乏跨平台

SWT 改进 Swing,但消耗内存

JFace 改进SWT—高效

2.步骤

import java.awt,javax.swing

继承JFrame

构造函数

new JButton

this.add(jb, BorderLayout(NORTH) )

this.setTitle

this.setDefualtCloseOperation(JFrame.EXIT_ON_CLOSE)

this.setLayout(new FlowLayout(FlowLayout.LEFT))

this.setLayout(NULL)

new Demo

3.Button:属于容器

JButton/JRadioButton 单选/ToggleButton 触发/BasicArrowButton

4.函数

setVisible

setLocation

setSize

5.布局

BorderLayout:中间自动调节

FlowLayout:缩放,button大小不变,位置改变

GridLayout(row,col):大小改变,相对位置不改变

* **锁定窗口**setResizable(false);

6.组件

组件种类

JTextField 文本框

JPassword 密码框

JLable 标签

JCombox 下拉框

JList 列表

JScrollPane 滚动栏

JSplitePane 伸缩框

JTextArea 多文本框

JTabbedPane 选项卡

JMenuBar 菜单栏

JToolBar 工具栏

包含以上所有组件的例程代码

class Windows extends JFrame{
JPanel jpNorth, jpSouth, jpCenter, jpEast = null;

JPanel jpAccountInfo;
JPanel jpCenter3 = null;
JPanel jpList, jpselection2, jpselection3;
JPanel jpTabbedJPanel;

JButton jbn1, jbn2, jbs1, jbs2;
JLabel jlAccount, jlPassword;
JLabel jLabel, jLabel2, jLabel3;
JTextField jtfAccount;
JPasswordField jpfPassword;

JCheckBox jcb1, jcb2, jcb3;
JRadioButton jrbMale, jrbFemale;
ButtonGroup bGroup;

JComboBox jcb;
JList jList,jList2, jList3;
JScrollPane jspFriendScrollPane, jspTextAreaJScrollPane;
JSplitPane jsp;
JTextArea jtxChatBoxArea;
JTabbedPane jtpJTabbedPane = null;

JMenuBar jMenuBar =    null;
JMenu jMenuFather =    null, jMenuFather1 = null;
JMenu jMenuSon    =    null;
JMenuItem jItemF1, jItemF2, jItemF3, jItemF4;
JMenuItem jItemSub1, jItemSub2;

JToolBar jtoolBar;
public Windows() {
// TODO Auto-generated constructor stub

/*==========  初始化布局文件    ==========*/
jpNorth = new JPanel(new BorderLayout());
jpSouth = new JPanel();
jpCenter = new JPanel(new GridLayout(3,1));
jpAccountInfo = new JPanel(null);             //无布局
jpList = new JPanel(null);
jpselection2  = new JPanel(null);
jpselection3  = new JPanel(null);
jpCenter3 = new JPanel(new BorderLayout());

/*==========  初始化Button   ===============*/
jbn1 = new JButton("North");
jbn2 = new JButton("北部");
jbs1 = new JButton("Sorth");
jbs2 = new JButton("南部");

/*==========  初始化Label    =============*/
jlAccount = new JLabel("账户");
jlAccount.setBounds(100, 0, 50, 20);                         //设置位置和Size
jlPassword = new JLabel("密码");
jlPassword.setBounds(100, 20, 100, 20);
jLabel = new JLabel(new ImageIcon("F:\\boy.jpeg"));            //图片
jLabel.setSize(50, 50);
jLabel2 = new JLabel("放于父居中", jLabel.CENTER);
jLabel2.setFont(new Font("宋体", Font.PLAIN, 16));              //字体
jLabel2.setForeground(Color.ORANGE);                         //背景色
jLabel2.setBounds(0, 0, 100, 50);
jLabel3 = new JLabel("<html><a href = 'www.baidu.com'>这里要放文字</a>");//链接
jLabel3.setBounds(100, 0, 100, 50);
jLabel3.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));         //鼠标样式
jpAccountInfo.add(jlAccount);
jpAccountInfo.add(jlPassword);
jpselection2.add(jLabel);
jpselection3.add(jLabel2);
jpselection3.add(jLabel3);

/*=====  JTextFiedl/JPasswordFeild =======*/
jtfAccount = new JTextField();                       //文本框
jtfAccount.setBounds(150, 0, 100, 20);
jpfPassword = new JPasswordField();
jpfPassword.setBounds(150, 20, 100, 20);
jpAccountInfo.add(jtfAccount);
jpAccountInfo.add(jpfPassword);

/*==========  初始化JCheckBox  =============*/       //复选框
jcb1 = new JCheckBox("电竞");
jcb1.setBounds(250, 0, 100, 20);
jcb2 = new JCheckBox("体育");
jcb2.setBounds(350, 0, 100, 20);
jcb3 = new JCheckBox("艺术");
jcb3.setBounds(450, 0, 100, 20);
jpAccountInfo.add(jcb1);
jpAccountInfo.add(jcb2);
jpAccountInfo.add(jcb3);

/*====== 初始化 JRadioGroup ========*/               //单选框
jrbFemale = new JRadioButton("Female");
jrbFemale.setBounds(250, 20, 100, 20);
jrbMale = new JRadioButton("Male");
jrbMale.setBounds(350, 20, 100, 20);

/*======  初始化 ButtonGroup =========*/
bGroup = new ButtonGroup();
bGroup.add(jrbFemale);
bGroup.add(jrbMale);
jpAccountInfo.add(jrbFemale);
jpAccountInfo.add(jrbMale);

/*======  Init JCombox  ======*/
String []jcbStrings = {"北京","南京","东京"};       //下拉框
jcb = new JComboBox(jcbStrings);
jcb.setBounds(0, 0, 80, 20);
jpList.add(jcb);

/*======  Init   JList  ====== 列表*/
String []jlFriendsStrings = {"秋风颂剑", "小耳朵","wang","hao","wu","ai"};
jList = new JList(jlFriendsStrings);

/*======  Init  JScrollPane  ====== 滚动条*/
jspFriendScrollPane = new JScrollPane(jList);
jspFriendScrollPane.setBounds(100, 0, 100, 50);
jpList.add(jspFriendScrollPane);

/*======  Init JSplitPane ======收缩框*/
String []jl2Strings = {"我是", "伸缩","框"};          //列表
jList2 = new JList(jl2Strings);
String []jl3Strings = {"我是", "右边", "JList"};
jList3 = new JList(jl3Strings);
jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, jList2, jList3); //收缩框
jsp.setOneTouchExpandable(true);                     //是否可以拖拽

/*====== Init JTextArea   ======*/
jtxChatBoxArea = new JTextArea();                      //多行输入框
jspTextAreaJScrollPane = new JScrollPane(jtxChatBoxArea);
jspTextAreaJScrollPane.setBounds(200, 0, 300, 300);
jspTextAreaJScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);//总是显示ScroolPane
jpCenter3.add(jspTextAreaJScrollPane, BorderLayout.CENTER);

/*======    JTabblePane    ======选项卡*/
jtpJTabbedPane = new JTabbedPane();
jtpJTabbedPane.add(jpList, "Cbox/ScrollP");
jtpJTabbedPane.add(jpselection2, "label images");
jtpJTabbedPane.add(jpselection3, "label Font");
jpTabbedJPanel = new JPanel(new BorderLayout());
jpTabbedJPanel.add(jtpJTabbedPane, BorderLayout.CENTER);
jpCenter.add(jpTabbedJPanel, 0);

/*======   JMenuBar  =====  工具栏*/
jMenuBar = new JMenuBar();
jMenuFather = new JMenu("File(F)");
jMenuFather.setMnemonic('F');              //助记符
jMenuFather1 = new JMenu("Help(H)");
jMenuFather1.setMnemonic('H');          //助记符
jMenuSon = new JMenu("SubMenu(S)");
jMenuSon.setMnemonic('S');
jItemF1 = new JMenuItem("Create");
jItemF1.setToolTipText("Create-setToolTipText"); //额外信息
jItemF2 = new JMenuItem("Delete(D)");
jItemF2.setMnemonic('D');
jItemF2.setToolTipText("Delete-setToolTipText"); //额外信息
jItemF3 = new JMenuItem("Copy");
jItemF3.setToolTipText("这个是setToolTipText"); //额外信息
jItemF4 = new JMenuItem("Cut");
jItemSub1 = new JMenuItem("Sub");
jItemSub2 = new JMenuItem("Menu");
jMenuBar.add(jMenuFather);
jMenuBar.add(jMenuFather1);
jMenuFather.add(jItemF1);
jMenuFather.add(jItemF2);
jMenuFather.addSeparator();              // menu增加分割线
jMenuFather.add(jMenuSon);               //嵌套SubMenu
jMenuFather1.add(jItemF3);
jMenuFather1.add(jItemF4);
jMenuSon.add(jItemSub1);
jMenuSon.add(jItemSub2);
jpNorth.add(jMenuBar, BorderLayout.NORTH);

/*======  JToolBar ======  工具栏*/
jtoolBar = new JToolBar();
jbn1.setMnemonic('B');
jbn1.setToolTipText("Press (B)");
jbn2.setToolTipText("tool2");
jbs1.setToolTipText("3TOOL");
jbs2.setToolTipText("hello word");
jtoolBar.add(jbs1);
jtoolBar.add(jbs2);
jtoolBar.add(jbn1);
jtoolBar.add(jbn2);
jpSouth.add(jtoolBar);

//add JPanel in JPanel
jpCenter.add(jpAccountInfo, 1);
jpCenter.add(jpCenter3, 2);

//add JPanel in JFrame
this.add(jpNorth, BorderLayout.NORTH);
this.add(jpCenter, BorderLayout.CENTER);
this.add(jpSouth, BorderLayout.SOUTH);
this.add(jsp, BorderLayout.WEST);                     //伸缩框

//set JFrame
this.setTitle("My Swing");
this.setSize(800, 400);
this.setLocation(400, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //默认关闭
this.setVisible(true);
this.setResizable(false);                              //不可以放大缩小
//设置标题图标 2种方法
//Image img = Toolkit.getDefaultToolkit().getImage("F:\\boy.jpeg");
//this.setIconImage(img);
this.setIconImage(new ImageIcon("F:\\boy.jpeg").getImage());
}


7.组件补充

JFileChooser

代码

JFileChoose jfc = new JFileChoose();
jfc.setDialogTitle("please choose file");
jfc.showOpenDialog(null); //null:表示default
jfc.showSaveDialog(null);
String name = jfc.getSelectedFile().getAbsolutePath();

bw.write(this.jta.getText()); //JTextArea 得到内容
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: