ContactManagementSystem总结

来源:互联网 发布:淘宝店铺运营模式 编辑:程序博客网 时间:2024/05/22 08:23

1.Java具有的观感名称以及类名
这里写图片描述
为JFrame设置观感的方法:

try {    UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");//设置观感    SwingUtilities.updateComponentTreeUI(JFrame x);//刷新x以及其中的组件}catch(Exception e) {    e.printStackTrace();}

2.一个组件(如JLabel)不能同时添加到两个容器中
3.JTabbedPane类:选项卡

public void setTabPlacement(int tabPlacement)//设置选项卡窗格的位置参数为:JTabbedPane.TOP JTabbedPane.BOTTOM JTabbedPane.LEFT JTabbedPane.RIGHT如果未设置默认为TOP 

选项卡中默认文字是横向排列的,如果要让其纵向排列,则使用html表示,< br>表示换行

tab.add("<html><br><br><br><br><br><br><br><br></html>", load);//void add(String title,Component com);Adds a component with the specified tab title.

3 . 修改JFrame中组件的默认字体:使用UIManager

在构造函数中使用:put("相应组件名+.font",Font);UIManager.put("Label.font", font);UIManager.put("Button.font", font);UIManager.put("TextField.font", input);UIManager.put("PasswordField.font", input);UIManager.put("TabbedPane.font", font);改方法调用必须在组件初始化之前

4.JTextField/JPasswordField固定输入长度且只能输入特定字符:重写PlainDocument

/***使用方法:JTextField t = new JTextField();*t.setDocument(new JTextFieldLimit(15));*输入最大长度是15  */class JTextFieldLimit extends PlainDocument{//限制输入长度和字符    private Set<Character> set;    private int limit;    public JTextFieldLimit(int limit,Character[] a) {//a[]为想输入的字符数组        super();        this.limit = limit;        this.set = new HashSet<>(Arrays.asList(a));    }    public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException{        if(str == null)        {            return;         }        for(int i = 0;i < str.length();i++) {            if((getLength()+str.length()) > limit || !set.contains(str.charAt(i)))                return;             super.insertString(offset, str, attr);        }    }   }

5.set/array/list的相互转换

Integer[] a = {1,2,4,3};List<Integer> list = new ArrayList<>();list = Arrays.asList(a);a = list.toArray(new Integer[list.size()]);Set<Integer> set = new HashSet<>(list)//HashSet(Collection<? extends E> c)Set<Integer> set1 = new HashSet<>(Arrays.asList(a));list.addAll(set); //addAll(Colletion<?> c)

6.JPasswordField获取内容

JPasswordField f = new JPasswordField();String passWord = new String(f.getPassword());//char[] getPassword();//String(char[]);

7.System.getProperty(“user.dir”)返回工程的绝对路径
8.创建文件夹:

File file = new File(System.getProperty("user.dir")+"\\文件");if(!file.exists() && !file.isDirectory()) {    file.mkdir();//创建“文件”文件夹}

创建该文件夹下的文件(必须在文件夹创建之后才能创建)

File file = new File(System.getProperty("user.dir")+"\\文件"+"\\user.txt");if(!file.exists())    file.createNewFile();//创建文件

9.写入文件和读取文件
写入文件:

File file = new File(System.getProperty("user.dir")+"\\文件"+"\\user.txt");FileWriter fw = new FileWriter(file);BufferedWriter bw = new BufferedWriter(fw);bw.write(name);bw.write(" ");bw.write(password);bw.write("\r\n");bw.flush();//刷新流bw.close();

读取文件:

File file = new File(System.getProperty("user.dir")+"\\文件"+"\\user.txt");FileReader fr = null;try {    fr = new FileReader(file);}catch(IOException e) {    System.out.println("读取文件错误");}BufferedReader br = new BufferedReader(fr);count = 0;String s = "";try {    while((s = br.readLine()) != null) {    Scanner in = new Scanner(s);    name[count] = in.next();    name[count] = in.next();    count++;    }}catch(IOException e) {    system.out.println("文件写入错误");}try {    br.close();}catch(IOException e) {    System.out.println("文件关闭错误");}

10.修改图片大小

Image Image.getScaledInstance(int width,int height,int hints)//将图像缩放成相对应的比例width - the width to which to scale the image. height - the height to which to scale the image. hints - flags to indicate the type of algorithm to use for image resampling.SCALE_DEFAULT, SCALE_FAST, SCALE_SMOOTH, SCALE_REPLICATE, SCALE_AREA_AVERAGING  
原创粉丝点击