Swing设计中的问题与方法

  1. 如何使JTextField和JTextArea具备复制粘贴功能?
    这两个类都不支持复制粘贴功能,需要继承这个类再自己实现:具备复制粘贴功能的JTextField和JTextArea
  2. 如何使JTextArea自动换行
    JTextArea txa = new JTextArea(rows,cols);
    txa.setLineWrap(true);
  3. 如何使JTextArea具有滚动条
    创建JTextArea的时候指定高度
    和宽度(对应行数和列数),放到JScrollPane中,设置滚动策略即可。
    JTextArea txa = new JTextArea(rows,cols); 
    JScrollPane jsp = new JScrollPane(txa); 
    //把JTextArea放到JScrollPane里面
    
    //分别设置水平和垂直滚动条自动出现 
    jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 
    jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); 
    
    //分别设置水平和垂直滚动条总是出现 
    jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 
    jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
    //分别设置水平和垂直滚动条总是隐藏
    jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
  4. JTextArea设置文字后滚动条会自动滚动到最下,如何定位滚动条到最上?
    txa.setCaretPosition(0);
  5. 设置JLabel中文字的对齐方式。
    JLabel label = new JLabel(“标签名”);
    label.setHorizontalAlignment(SwingConstants.RIGHT);
    label.setVerticalAlignment(SwingConstants.CENTER);
  6. 设置JFileChooser为多选或只选目录。
    基本用法:
    JFileChooser dlg = new JFileChooser();
    dlg.setDialogTitle("Open JPEG file");
    int result = dlg.showOpenDialog(this);  // 打开"打开文件"对话框
    // int result = dlg.showSaveDialog(this);  // 打"开保存文件"对话框
    if (result == JFileChooser.APPROVE_OPTION) {
    	File file = dlg.getSelectedFile();
    	...
    }
    多选:
    dlg.setMultiSelectionEnabled(true); //即可
    File[] files = c.getSelectedFiles();//读取选择的文件
    只选目录:
    dlg.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    int result = c.showOpenDialog(this);
    if (result == JFileChooser.APPROVE_OPTION) {
    	String path = c.getSelectedFile().getAbsolutePath());
    	...
    }
    awt库中的FileDialog可以实现本地风格的文件对话框,但是不支持多选。

  7. 设置JSplitPane为比例分割
    JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, recordPane, picPane); 
    splitPane.setDividerLocation(2.0/3); //分割比例为2:1
    //splitPane.setDividerLocation(150);  //在150像素处分割
    splitPane.setOneTouchExpandable(true);//设置为可折叠
  8. 获取系统中的文件图标
    private static Icon getSmallIcon(File f) {  
            if (f != null && f.exists()) {  
                FileSystemView fsv = FileSystemView.getFileSystemView();  
                return fsv.getSystemIcon(f);  
            }  
            return null;  
        }  
    
        private static Icon getBigIcon(File f) {  
            if (f!=null && f.exists()) {  
                try {  
                    sun.awt.shell.ShellFolder sf = sun.awt.shell.ShellFolder.getShellFolder(f);  
                    return new ImageIcon(sf.getIcon(true));  
                } catch (FileNotFoundException e) {  
                    e.printStackTrace();  
                }  
            }  
            return null;  
        }  

  9. 设置JButton为图片样式。
    JButton bt = new JButton();
    ImageIcon icon = new ImageIcon("tmp/Kaola.jpg");
    bt.setIcon(icon);
    bt.setSize(100, 200);
    //或者这样
    JButton button = new JButton("文字", new ImageIcon("tmp/Jellyfish.jpg"));
    bt.setSize(100, 200);
    一定要setSize或者setBounds,否则是看不到图片的,甚至连按钮都看不到。

  10. 设置JInternalFrame最大化。
    //设置需在desktopPane.add(iFrame)之后方可,否则会出现NullPointerException
    picFrame.setMaximum(true);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值