主要学习使用jwt的组件系统
java图形化界面主要用到的包在java.jwt和javax.Swing.
java.jwt和java.swing区别:
java.jwt中的图形库依赖于各个系统的实现,比如windows和Linux各有各的图形库实现。
javax.swing 针对于java.jwt的每个系统有不同的图形库实现,导致java跨系统优越性造成影响的问题,做出了处理。
组件的类别:容器组件和非容器组件。
容器组件:可以容纳其他组件的组件
Jframe:java图形化窗体。
Dialog:对话框类
JOptionPane:对话框(图形显示好看一点)
FileDialog:文件对话框,可以save和load文件。
Panel:面板
非容器组件:单独不容纳其他组件的组件
组件的使用案例:
package news; import javax.swing.*; public class Register { public static void main(String[] args) { JFrame jFrame = new JFrame("登录"); JPanel jPanel = new JPanel(); jFrame.add(jPanel); //用户名 JLabel namejLabel = new JLabel("用户名"); JTextField namejTextField = new JTextField(15); jPanel.add(namejLabel); jPanel.add(namejTextField); //密码 JLabel pwdjLabel = new JLabel("密码"); JPasswordField pwdnamejTextField = new JPasswordField(15); jPanel.add(pwdjLabel); jPanel.add(pwdnamejTextField); //性别 JLabel sexjLabel = new JLabel("性别"); JRadioButton man = new JRadioButton("男",true); JRadioButton women = new JRadioButton("女"); //如果是单选框要进行分组 同一种只能选择一个 ButtonGroup group = new ButtonGroup(); group.add(man); group.add(women); jPanel.add(sexjLabel); jPanel.add(man); jPanel.add(women); //下拉框 JLabel citiewLabel = new JLabel("来自"); JComboBox jComboBox = new JComboBox(new String[]{"北京","东京"}); jPanel.add(citiewLabel); jPanel.add(jComboBox); //复选框 JLabel loveLabel = new JLabel("爱好"); JCheckBox checkBox1 = new JCheckBox("1"); JCheckBox checkBox2 = new JCheckBox("2"); JCheckBox checkBox3 = new JCheckBox("3"); JCheckBox checkBox4 = new JCheckBox("4"); JCheckBox checkBox5 = new JCheckBox("5"); jPanel.add(checkBox1);jPanel.add(checkBox2);jPanel.add(checkBox3);jPanel.add(checkBox4);jPanel.add(checkBox5); //个人简介 JLabel datail = new JLabel("个人简介"); JTextArea jTextArea = new JTextArea(20,15); jPanel.add(datail); jPanel.add(jTextArea); jTextArea.setLineWrap(true); Util.initFrame(jFrame,800,400); } }
菜单组件:
菜单条(MenuBar) 菜单(Menu) 菜单项(MenuItem)
使用方式案例:
class MenuTest { public static void main(String[] args) { //建立frame JFrame frame = new JFrame("notepad"); //建立菜单条 JMenuBar menuBar = new JMenuBar(); //将菜单条加进去 frame.add(menuBar,BorderLayout.NORTH); //增加菜单 JMenu jMenufile = new JMenu("文件"); JMenu jMenuedit = new JMenu("编辑"); JMenu change = new JMenu("切换"); JTextArea jTextArea = new JTextArea(); //给菜单增加选项 JMenuItem jMenuItemfile1 = new JMenuItem("打开"); JMenuItem jMenuItemfile2 = new JMenuItem("最近文件"); JMenuItem jMenuItemfile3 = new JMenuItem("转换"); //给选项增加选项 JMenuItem project1 = new JMenuItem("project1"); JMenuItem project2 = new JMenuItem("project2"); JMenuItem project3 = new JMenuItem("project3"); change.add(project1); change.add(project2); change.add(project3); //============ jMenufile.add(jMenuItemfile1); jMenufile.add(jMenuItemfile2); jMenufile.add(jMenuItemfile3); jMenufile.add(change); JMenuItem jMenuItemedit1 = new JMenuItem("打开"); JMenuItem jMenuItemedit2 = new JMenuItem("最近文件"); JMenuItem jMenuItemedit3 = new JMenuItem("转换"); jMenuedit.add(jMenuItemedit1); jMenuedit.add(jMenuItemedit2); jMenuedit.add(jMenuItemedit3); menuBar.add(jMenufile); menuBar.add(jMenuedit); frame.add(jTextArea); Util1.initFrame(frame,400,400); } } //初始话窗体的工具类 class Util1 { public static void initFrame(JFrame jFrame,int width,int height){ Toolkit toolkit = Toolkit.getDefaultToolkit(); Dimension dimension = toolkit.getScreenSize(); System.out.println("width"+dimension.width); System.out.println("height"+dimension.height); jFrame.setBounds((dimension.width-width)/2, (dimension.height-height)/2,width,height); jFrame.setVisible(true); jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
布局管理器:处理组件的摆放规则
class Layer{ public static void main(String[] args) { //边框布局管理器 JFrame jFrame = new JFrame("边框布局管理器范例"); BorderLayout borderLayout = new BorderLayout(); jFrame.setLayout(borderLayout); jFrame.add(new JButton("上"),BorderLayout.NORTH); jFrame.add(new JButton("下"),BorderLayout.SOUTH); jFrame.add(new JButton("左"),BorderLayout.WEST); jFrame.add(new JButton("右"),BorderLayout.EAST); jFrame.add(new JButton("中"),BorderLayout.CENTER); Util1.initFrame(jFrame,300,300); } }
结果图:
流式布局管理器:
FlowLayout============== 按行摆放 流式布局
class FlowLayoutDemo{ // 流式布局管理器 public static void main(String[] args) { //边框布局管理器 JFrame jFrame = new JFrame("流式布局管理器范例"); FlowLayout flowLayout = new FlowLayout(); JPanel jPanel = new JPanel(); jPanel.setLayout(flowLayout); jPanel.add(new JButton("按钮1")); jPanel.add(new JButton("按钮2")); jPanel.add(new JButton("按钮3")); jPanel.add(new JButton("按钮4")); jPanel.add(new JButton("按钮5")); jPanel.add(new JButton("按钮6")); jFrame.add(jPanel); Util1.initFrame(jFrame,300,300); } }
表格布局管理器:适用于计算器一类的东西
class GripDemo { public static void main(String[] args) { //表格布局管理器 JFrame jFrame = new JFrame("表格布局管理器范例"); GridLayout gridLayout = new GridLayout(4,4,30,30); jFrame.setLayout(gridLayout); for (int i = 0; i < 10 ; i++) { jFrame.add(new JButton(i+"")); } jFrame.add(new JButton("+")); jFrame.add(new JButton("-")); jFrame.add(new JButton("*")); jFrame.add(new JButton("/")); jFrame.add(new JButton(".")); jFrame.add(new JButton("=")); Util1.initFrame(jFrame,300,300); } }
卡片布局管理器:
class CardLayerDemo {
public static void main(String[] args) {
//卡片布局管理器
JFrame jFrame = new JFrame("卡片布局管理器");
CardLayout cardLayer = new CardLayout();
JPanel panel = new JPanel();
jFrame.add(panel);
panel.setLayout(cardLayer);
JButton button = new JButton("C");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cardLayer.next(panel);
}
});
panel.add(button);
panel.add(new Button("A"));
panel.add(new Button("B"));
panel.add(new Button("D"));
initFrame(jFrame,300,300);
}
public static void initFrame(JFrame jFrame,int weight,int height){
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension dimension = toolkit.getScreenSize();
int x = (dimension.width-weight)/2;
int y = (dimension.height-height)/2;
jFrame.setBounds(x,y,weight,height);
jFrame.setVisible(true);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
事件:为组件增加功能
鼠标时间Demo
class MouseListenerDemo { public static void main(String[] args) { //鼠标监听器 JFrame jFrame = new JFrame("鼠标监听器Demo"); JButton button =new JButton("我的按钮"); jFrame.add(button); button.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { // System.out.println("我被单击了"); JButton jButton = (JButton)e.getSource(); if (e.getClickCount() == 1) System.out.println("我被单机了"); else if (e.getClickCount() == 2) System.out.println("我被双击了"); } }); Util1.initFrame(jFrame,300,300); } }
键盘事件:
class KeyListenerDemo { public static void main(String[] args) { JFrame jFrame = new JFrame("键盘事件Demo"); jFrame.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { System.out.println(e.getKeyChar()); } }); Util1.initFrame(jFrame,300,300); } }
==做一个简单的文件搜索功能
class NotePadDemo { public static void main(String[] args) { JFrame jFrame = new JFrame("文件搜索"); JPanel jPanel = new JPanel(); FlowLayout flowLayout = new FlowLayout(); jPanel.setLayout(flowLayout); JTextField jTextField = new JTextField(15); JButton jButton = new JButton("搜索"); JTextArea jTextArea =new JTextArea(); jPanel.add(jTextField); jPanel.add(jButton); jFrame.add(jPanel,BorderLayout.NORTH); jFrame.add(jTextArea); jButton.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { String text = jTextField.getText(); try { soutFile(text); } catch (IOException e1) { e1.printStackTrace(); } } }); init(jFrame,300,300); } public static void soutFile(String text) throws IOException { File file = new File(text); String[] files = file.list(); for (String f1:files ) { File f = new File(f1); if (f.isDirectory()){ soutFile(f.getCanonicalPath()); }else { System.out.println(f.getName()); } } } public static void init(JFrame jFrame, int width,int height){ Toolkit toolkit = Toolkit.getDefaultToolkit(); Dimension dimension = toolkit.getScreenSize(); int x = (dimension.width-width)/2; int y = (dimension.height-height)/2; jFrame.setBounds(x,y,width,height); jFrame.setVisible(true); jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
模拟notepad打开保存文件
class MyNotePad { public static void main(String[] args) { //建立frame JFrame frame = new JFrame("notepad"); //建立菜单条 JMenuBar menuBar = new JMenuBar(); //将菜单条加进去 frame.add(menuBar,BorderLayout.NORTH); //增加菜单 JMenu jMenufile = new JMenu("文件"); JMenu jMenuedit = new JMenu("编辑"); JMenu change = new JMenu("切换"); JTextArea jTextArea = new JTextArea(); //给菜单增加选项 JMenuItem jMenuItemfile1 = new JMenuItem("打开"); JMenuItem jMenuItemfile2 = new JMenuItem("保存"); JMenuItem jMenuItemfile3 = new JMenuItem("转换"); //增加保存事件 jMenuItemfile2.addMouseMotionListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { System.out.println("我点击了"); } }); jMenuItemfile2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { FileDialog fileDialog = new FileDialog(frame,"请选择保存位置",FileDialog.SAVE); fileDialog.setVisible(true); String path = fileDialog.getDirectory(); String filename = fileDialog.getFile(); //需要保存的附件 String text = jTextArea.getText(); text.replace("/n","/r/n"); try { OutputStream outputStream = new FileOutputStream(new File(path,filename)); outputStream.write(text.getBytes()); outputStream.close(); }catch (IOException s){} } }); jMenuItemfile1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { FileDialog fileDialog = new FileDialog(frame,"请选择需要打开的文件",FileDialog.LOAD); fileDialog.setVisible(true); String path = fileDialog.getDirectory(); String filename = fileDialog.getFile(); try { File file = new File(path,filename); FileInputStream fileInputStream = new FileInputStream(file); int length = 0; byte[] bytes = new byte[1024]; while ((length = fileInputStream.read(bytes))!=-1){ jTextArea.setText(jTextArea.getText()+new String(bytes,0,length)); } }catch (IOException e1){} } }); //给选项增加选项 JMenuItem project1 = new JMenuItem("project1"); JMenuItem project2 = new JMenuItem("project2"); JMenuItem project3 = new JMenuItem("project3"); change.add(project1); change.add(project2); change.add(project3); //============ jMenufile.add(jMenuItemfile1); jMenufile.add(jMenuItemfile2); jMenufile.add(jMenuItemfile3); jMenufile.add(change); JMenuItem jMenuItemedit1 = new JMenuItem("打开"); JMenuItem jMenuItemedit2 = new JMenuItem("最近文件"); JMenuItem jMenuItemedit3 = new JMenuItem("转换"); jMenuedit.add(jMenuItemedit1); jMenuedit.add(jMenuItemedit2); jMenuedit.add(jMenuItemedit3); menuBar.add(jMenufile); menuBar.add(jMenuedit); frame.add(jTextArea); Util1.initFrame(frame,400,400); } }