javase开发问题贴(持续更新)
来源:互联网 发布:淘宝宝贝分类 编辑:程序博客网 时间:2024/06/06 12:30
- 模态框
- JSplitPane设置比例问题
- 分隔条的使用
- 设置边框
- JScorllPane添加容器
- JScorllPane添加多个容器
- 文字的居中显示和swing布局
- 系统目录树
- 在Jtable中更新下载进度
- java的百分比
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
- javase开发问题贴(持续更新)
- 开发问题(持续更新)
- javaSE面试题集锦(持续更新)
- iOS开发问题总结(持续更新...)
- 【开发问题记录,持续更新..】
- JavaSE 面试题(持续更新)
- Android开发遇到的问题笔记(持续更新)
- 安卓开发遇到的一些问题(持续更新)
- Mozilla扩展开发过程遇到的问题(持续更新)
- FreeCMS开发过程问题总结(持续更新中)
- Android开发中的问题及相应解决(持续更新)
- Android 项目开发问题整理(持续更新)
- Android 开发小问题集合(持续更新)
- MAVEN学习开发问题记录帖(持续更新...)
- php服务端个人开发问题汇总(持续更新中...)
- Google Play 开发应用问题一览(持续更新)
- Android开发中小问题汇总(持续更新)
- android开发问题汇总(持续更新中)
- Mac Book Pro新手上路
- PHP.ini无效
- struct fb_info
- flex-direction
- JAVA基础
- javase开发问题贴(持续更新)
- UVA12563: Jin Ge Jin Qu hao(类01背包)
- 浅谈数据库及表设计的几个原则
- Java多线程
- Codeforces Round #389 (Div. 2) D. Santa Claus and a Palindrome 回文串+贪心+数据结构、字符串
- python 爬虫分析部分词条影响力分布
- [图解] 11招教你如何玩转数据库设计
- 表达式树使用(四)
- mysql备份所有数据库