javase开发问题贴(持续更新)

1.模态框

//设置模态,阻塞其它窗体
setModal(true);
//一点要在setVisible(true);之前,
//而且一帮setvisibale要放在最后,否则会有监听事件没有监听到
  • 正确的顺序
    1.监听事件
    2.模态
    3.可见

JSplitPane设置比例问题

根据 setDividerLocation(int) 来实现此方法。此方法以分隔窗格的当前大小为基础迅速改变窗格的大小。如果分隔窗格没有正确地实现并且不显示在屏幕上,此方法将不产生任何影响(新的分隔条位置将成为 0(当前的 size * proportionalLocation ))。

看完后没什么概念。。。只觉得写的不是那么直白,也许确有什么猫腻在里边。特别是”如果分隔窗格没有正确地实现并且不显示在屏幕上,此方法将不产生任何影响”这句,没大理解。。。

因而去看看JSplitPane的源码。关于setDividerLocation大致如下:

    public void setDividerLocation(double proportionalLocation) { 
        if (proportionalLocation < 0.0 || 
           proportionalLocation > 1.0) { 
            throw new IllegalArgumentException("proportional location must " + 
                                               "be between 0.0 and 1.0."); 
        } 
        if (getOrientation() == VERTICAL_SPLIT) { 
            setDividerLocation((int)((double)(getHeight() - getDividerSize()) * 
                                     proportionalLocation)); 
        } else { 
            setDividerLocation((int)((double)(getWidth() - getDividerSize()) * 
                                     proportionalLocation)); 
        } 
    } 

这下有些明白了,setDividerLocation(double)这个函数会用到getWidth()或者getHeight()这样的函数,而java桌面程序在没有主窗体setVisible之前,如果使用布局,尚未validate()和paint()每个组件的宽和高默认都是0。也就是说一定要在主窗体setVisible(true)之后再使用setDividerLocation(double)才会有效。

3.分隔条的使用

 //创建水平分隔线
 JSeparator separator = new JSeparator();    
separator.setOrientation(JSeparator.HORIZONTAL); 
verticalBox.add(separator);//在容器中add

4.设置边框

label[i].setBorder(BorderFactory.createEtchedBorder());

5.JScorllPane添加容器

//我们一帮都是直接new JScorllOane(容器)
//因为它只能放一个容器,add的方法
jsPane.getViewport().add(verticalBox);

6. JScorllPane添加多个容器

//我们可以先new一个JPanel或者Box,把其它的容器先放到JPanel或Box上面再把它放到JScorllPane中,下面的代码实现在JScollPanel中放置做个Label显示图片的功能。
 private JScrollPane jsPane;

    public void initScan()
    {
        jsPane = new JScrollPane();
        //JPanel panel = new JPanel();
        Box verticalBox   = Box.createVerticalBox(); 
        File file = new File("tmp/");
        String[] files = file.list();
        label = new JLabel[files.length];
        int i = 0;
        for(String e: files)
        {
            if (!e.endsWith("png")) continue; 
            System.out.println(e);
            label[i] = new JLabel();
            label[i].setIcon(new ImageIcon(MyOfficeImage.tmpDir + e));
            label[i].setBorder(BorderFactory.createEtchedBorder());
            //panel.add(label[i]);
            verticalBox.add(label[i]);
//          JSeparator separator = new JSeparator();   //创建水平分隔线  
//          separator.setOrientation(JSeparator.HORIZONTAL); 
//          verticalBox.add(separator);
            //jsPane.getViewport().add(label[i]);
            i++;
        }
        jsPane.getViewport().add(verticalBox);
        contentPanel.add(jsPane, BorderLayout.CENTER);//jsPane
    }

7. 文字的居中显示和swing布局

  • 如果直接放在FlowLayout里,控件会从左往右,从上到下排列,不会占满整块布局。
  • 如果放在BorderLayout中,则会占满整个布局,这样控件就是按比例放大的,就会自动居中了。
  • 如果不想居中,或者绝得BorderLayout的North和sourth占的地方还是太大,可以用toolBar来放置控件
public class MyScanDialog extends JDialog {

    private final JPanel contentPanel = new JPanel();
    private JTextArea textArea = new JTextArea();
    private JLabel[] label;
    private JScrollPane jsPane;

    public void initScan()
    {
        jsPane = new JScrollPane();
        //JPanel panel = new JPanel();
        Box verticalBox   = Box.createVerticalBox(); 
        File file = new File("tmp/");
        String[] files = file.list();
        label = new JLabel[files.length];
        int i = 0;
        for(String e: files)
        {
            if (!e.endsWith("png")) continue; 
            System.out.println(e);
            label[i] = new JLabel();
            label[i].setIcon(new ImageIcon(MyOfficeImage.tmpDir + e));
            label[i].setBorder(BorderFactory.createEtchedBorder());
            //panel.add(label[i]);
            verticalBox.add(label[i]);
//          JSeparator separator = new JSeparator();   //创建水平分隔线  
//          separator.setOrientation(JSeparator.HORIZONTAL); 
//          verticalBox.add(separator);
            //jsPane.getViewport().add(label[i]);
            i++;
        }
        jsPane.getViewport().add(verticalBox);
        contentPanel.add(jsPane, BorderLayout.CENTER);//jsPane
    }
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        //String content = MyPdf.readPdf("E:\\杂项\\CMake Practice.pdf");
        //String content = MyWord.readDoc("E:\\杂项\\毕业论文开题报告范文.doc");
        //textArea.setText(content);//差点闹了个笑话

        //JFrame.setDefaultLookAndFeelDecorated(true);
        //必须要有这一句,因为英文解释里大概是可以设置了这个,修饰的那些东西就会由lookfeel提供
        JDialog.setDefaultLookAndFeelDecorated(true);
        EventQueue.invokeLater(new Runnable() {
            public void run() 
            {
                try 
                {


                    SubstanceLookAndFeel.setCurrentButtonShaper(new org.jvnet.substance.button.ClassicButtonShaper());

                } 
                catch (Exception e) 
                {
                    e.printStackTrace();
                }

                MyScanDialog dialog = null;
                try {
                    dialog= new MyScanDialog();
                    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                    dialog.initScan();  
                    dialog.setVisible(true);


                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });



        //SwingUtilities.updateComponentTreeUI(dialog);
    }

    /**
     * Create the dialog.
     */
    public MyScanDialog()
    {
        setBounds(100, 100, 657, 484);
        getContentPane().setLayout(new BorderLayout());
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        contentPanel.setLayout(new BorderLayout());

        JPanel panel = new JPanel();
        contentPanel.add(panel, BorderLayout.NORTH);
        panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

        JLabel label = new JLabel("hah");
        label.setFont(new Font("宋体", Font.PLAIN, 22));
        panel.add(label);   
    }
}

8.系统目录树

如果要实现调用系统本地小图标,只需要重写DefaultTreeCellRenderer
clientTree.setCellRenderer(new MyTreeCellRender());

public class MyTreeCellRender extends DefaultTreeCellRenderer{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public static Icon getSmallIcon(File file)
    {
        if (file != null && file.exists())
        {
            FileSystemView fsv = FileSystemView.getFileSystemView();
            return fsv.getSystemIcon(file);
        }
        return null;
    }

    private static String getTreePath(TreeNode[] treenode)
    {
        String tmp = "";
         for (int i = 1; i < treenode.length-1; i++)
         {
             tmp += (treenode[i].toString() + "\\");
         }
         tmp += treenode[ treenode.length-1 ].toString();
         return tmp;
    }


    /** 
     * 重写父类DefaultTreeCellRenderer的方法 
     */  
    @Override  
    public Component getTreeCellRendererComponent(JTree tree, Object value,  
            boolean sel, boolean expanded, boolean leaf, int row,  
            boolean hasFocus)  
    {  

        //执行父类原型操作  
        super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf,  
                row, hasFocus);  

        setText(value.toString());  
        //得到每个节点的TreeNode  
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;  
        String path = getTreePath(node.getPath());
        //判断是哪个文本的节点设置对应的值(这里如果节点传入的是一个实体,则可以根据实体里面的一个类型属性来显示对应的图标)  
        this.setIcon(getSmallIcon(new File(path)));  

        return this;  
    }  
}


private void listClientDirectory()
    {
         DefaultMutableTreeNode clientRoots = new DefaultMutableTreeNode("电脑");
         File[] root = File.listRoots(); 
         clientTree = new JTree(clientRoots);
         clientTree.setCellRenderer(new MyTreeCellRender());
         JScrollPane clientScrollTree = new JScrollPane(clientTree); 

         for (int i = 0; i < root.length; i++) 
         {  
             if(root[i].canRead())
             {
                 clientRoots.add(new DefaultMutableTreeNode(root[i].toString()));                
             }
         }


         clientTree.addMouseListener(new MouseListener() {

            @Override
            public void mouseReleased(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mousePressed(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseExited(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseEntered(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseClicked(MouseEvent e) 
            {
                // TODO Auto-generated method stub
                log.debug(e.getClickCount()+" "+e.getSource().toString());
                if (e.getClickCount() >= 1)
                {
                    DefaultMutableTreeNode node = (DefaultMutableTreeNode) clientTree.getLastSelectedPathComponent(); //返回最后选中的结点  
                    if (node == null)
                    {
                        return;
                    }
                    String nodeName = node.toString();//获得这个结点的名称

                    if (nodeName.equals("电脑"))
                    {
                        return;
                    }

                    HashMap<String, Integer>map =new HashMap<String, Integer>();
                    int i = 0;
                    for (i = 0; i < node.getChildCount(); i++)
                    {
                        map.put(node.getChildAt(i).toString(), 1);
                    }

                    String filePath = getTreePath(node.getPath());

                    File parentFile = new File(filePath);

                    if (parentFile.isDirectory())
                    {
                        log.info(nodeName + " is " + "directory");
                        String[] sonFiles = parentFile.list();
                        //list获取的将不会是文件路径而只是当前文件名
                        for (String file : sonFiles)
                        {
                            if ( map.containsKey(file) ) continue;
                            map.put(file, 2);
                            EventQueue.invokeLater(new Runnable() 
                            {
                                public void run() 
                                {
                                    try 
                                    {                               
                                        node.add(new DefaultMutableTreeNode(file));                                                                         
                                    } 
                                    catch (Exception e) 
                                    {
                                        e.printStackTrace();
                                    }
                                }
                             });                              
                        }
                     }
                    else
                    {


                    }


                }   
            }
        });

    }

9.在Jtable中更新下载进度

对TabelModel的操作会显示到Table上,所以只要改变TableModel的值就可以,所以只要传TableModel和第几行,第几列就可以。这时候开启一条线程们就可以实时更新

DefaultTableModel tbModel;
private int nowSize;
private int maxSize;
tbModel.setValueAt(object, row, column);
//注意对object的操作是没有的,别以为java是传引用,看到里面的源码你会发现,你获取object的时候他是new了一个新的返回给你。

10. java的百分比

private static NumberFormat nt = NumberFormat.getPercentInstance();
object = nt.format(nowSize*1.0/maxSize);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 内容概要 《计算机试卷1》是一份综合性的计算机基础和应用测试卷,涵盖了计算机硬件、软件、操作系统、网络、多媒体技术等多个领域的知识点。试卷包括单选题和操作应用两大类,单选题部分测试学生对计算机基础知识的掌握,操作应用部分则评估学生对计算机应用软件的实际操作能力。 ### 适用人群 本试卷适用于: - 计算机专业或信息技术相关专业的学生,用于课程学习或考试复习。 - 准备计算机等级考试或职业资格认证的人士,作为实战演练材料。 - 对计算机操作有兴趣的自学者,用于提升个人计算机应用技能。 - 计算机基础教育工作者,作为教学资源或出题参考。 ### 使用场景及目标 1. **学习评估**:作为学校或教育机构对学生计算机基础知识和应用技能的评估工具。 2. **自学测试**:供个人自学者检验自己对计算机知识的掌握程度和操作熟练度。 3. **职业发展**:帮助职场人士通过实际操作练习,提升计算机应用能力,增强工作竞争力。 4. **教学资源**:教师可以用于课堂教学,作为教学内容的补充或学生的课后练习。 5. **竞赛准备**:适合准备计算机相关竞赛的学生,作为强化训练和技能检测的材料。 试卷的目标是通过系统性的题目设计,帮助学生全面复习和巩固计算机基础知识,同时通过实际操作题目,提高学生解决实际问题的能力。通过本试卷的学习与练习,学生将能够更加深入地理解计算机的工作原理,掌握常用软件的使用方法,为未来的学术或职业生涯打下坚实的基础。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值