Swing继承AWT,缺乏跨平台
SWT 改进 Swing,但消耗内存
JFace 改进SWT—高效
- 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
JButton/JRadioButton 单选/ToggleButton 触发/BasicArrowButton
setVisible
setLocation
setSize
BorderLayout:中间自动调节
FlowLayout:缩放,button大小不变,位置改变
GridLayout(row,col):大小改变,相对位置不改变
* **锁定窗口**setResizable(false);
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() {
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());
jbn1 = new JButton("North");
jbn2 = new JButton("北部");
jbs1 = new JButton("Sorth");
jbs2 = new JButton("南部");
jlAccount = new JLabel("账户");
jlAccount.setBounds(100, 0, 50, 20);
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);
jtfAccount = new JTextField();
jtfAccount.setBounds(150, 0, 100, 20);
jpfPassword = new JPasswordField();
jpfPassword.setBounds(150, 20, 100, 20);
jpAccountInfo.add(jtfAccount);
jpAccountInfo.add(jpfPassword);
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);
jrbFemale = new JRadioButton("Female");
jrbFemale.setBounds(250, 20, 100, 20);
jrbMale = new JRadioButton("Male");
jrbMale.setBounds(350, 20, 100, 20);
bGroup = new ButtonGroup();
bGroup.add(jrbFemale);
bGroup.add(jrbMale);
jpAccountInfo.add(jrbFemale);
jpAccountInfo.add(jrbMale);
String []jcbStrings = {"北京","南京","东京"};
jcb = new JComboBox(jcbStrings);
jcb.setBounds(0, 0, 80, 20);
jpList.add(jcb);
String []jlFriendsStrings = {"秋风颂剑", "小耳朵","wang","hao","wu","ai"};
jList = new JList(jlFriendsStrings);
jspFriendScrollPane = new JScrollPane(jList);
jspFriendScrollPane.setBounds(100, 0, 100, 50);
jpList.add(jspFriendScrollPane);
String []jl2Strings = {"我是", "伸缩","框"};
jList2 = new JList(jl2Strings);
String []jl3Strings = {"我是", "右边", "JList"};
jList3 = new JList(jl3Strings);
jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, jList2, jList3);
jsp.setOneTouchExpandable(true);
jtxChatBoxArea = new JTextArea();
jspTextAreaJScrollPane = new JScrollPane(jtxChatBoxArea);
jspTextAreaJScrollPane.setBounds(200, 0, 300, 300);
jspTextAreaJScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jpCenter3.add(jspTextAreaJScrollPane, BorderLayout.CENTER);
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 = 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();
jMenuFather.add(jMenuSon);
jMenuFather1.add(jItemF3);
jMenuFather1.add(jItemF4);
jMenuSon.add(jItemSub1);
jMenuSon.add(jItemSub2);
jpNorth.add(jMenuBar, BorderLayout.NORTH);
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);
jpCenter.add(jpAccountInfo, 1);
jpCenter.add(jpCenter3, 2);
this.add(jpNorth, BorderLayout.NORTH);
this.add(jpCenter, BorderLayout.CENTER);
this.add(jpSouth, BorderLayout.SOUTH);
this.add(jsp, BorderLayout.WEST);
this.setTitle("My Swing");
this.setSize(800, 400);
this.setLocation(400, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setResizable(false);
this.setIconImage(new ImageIcon("F:\\boy.jpeg").getImage());
}
JFileChooser
JFileChoose jfc = new JFileChoose();
jfc.setDialogTitle("please choose file");
jfc.showOpenDialog(null);
jfc.showSaveDialog(null);
String name = jfc.getSelectedFile().getAbsolutePath();
bw.write(this.jta.getText());