Swing开发时必定会碰到的11个问题及解决方案

开发 后端
本文将主要讲了Swing开发时必定会碰到的11个问题以及解决方案,同时也给了大家一个UI的工厂来解决其他的问题,是平时常用的组件的创建(自动注册按钮、快捷键等),应有尽有。使用了单例模式。

1.JTable ,JTable如何在内容里面存放组件(下拉框,图片)和修改数据。

2.系统托盘TrayIcon的使用

3.JPopupMenu的使用

4.JTree的使用及替换样式

5.BorderLayout的灵活使用

6.进度条的同步读取和显示

7.滚动条下拉时候滚动条自动衍生

8.监听器、适配器的使用

9.如何直接把文件、文件夹拖拉到组件

10.剪切板的使用

11.JFM拓展包

*********************

JTable放图片

  1. package roy;   
  2. import java.awt.Color;  
  3. import java.awt.Component;  
  4. import java.awt.Dimension;  
  5.  
  6. import javax.swing.Icon;  
  7. import javax.swing.ImageIcon;  
  8. import javax.swing.JFrame;  
  9. import javax.swing.JScrollPane;  
  10. import javax.swing.JTable;  
  11. import javax.swing.border.LineBorder;  
  12. import javax.swing.event.TableModelEvent;  
  13. import javax.swing.table.AbstractTableModel;  
  14. import javax.swing.table.DefaultTableCellRenderer;  
  15. import javax.swing.table.DefaultTableModel;  
  16. import javax.swing.table.TableColumnModel;  
  17.  
  18. //  
  19. public class MyTable{  
  20.     JTable table=new JTable();  
  21.     public MyTable(){  
  22.          JFrame frame=new JFrame("测试jtable放图片");  
  23.          frame.setLayout(null);  
  24.  
  25.          table=this.getTable();  
  26.  
  27.          JScrollPane src=new JScrollPane(table);  
  28.          src.setBounds(00400200);  
  29.          frame.setSize(new Dimension(400,200));  
  30.          frame.add(src);  
  31.          frame.setVisible(true);  
  32.     }  
  33.     public JTable getTable(){  
  34.         MyTableModel dm = new MyTableModel();  
  35.        
  36.         JTable table = new JTable();  
  37.         table.setModel(dm);//设置每个单元格的格式  
  38.         table.setDefaultRenderer(Icon.classnew ImageRenderer());  
  39.  
  40.      return table;  
  41.     }  
  42.     public static void main(String args[]){  
  43.       new MyTable();  
  44.      }  
  45.     }  
  46.     class MyTableModel extends AbstractTableModel{  
  47.      private String[] columnNames = {"First Name",  
  48.                 "Favorite Color",  
  49.                };  
  50.             private Object[][] data = {  
  51.             {"Marry"new ImageIcon("1.gif")  
  52.                 },  
  53.                {"Joe",new ImageIcon("1.jpg")  
  54.                 },  
  55.                {"Kims"new ImageIcon("1.png")  
  56.                }  
  57.            };  
  58.     public int getColumnCount() {  
  59.         return columnNames.length;  
  60.     }  
  61.  
  62.     public int getRowCount() {  
  63.         return data.length;  
  64.     }  
  65.  
  66.     public String getColumnName(int col) {  
  67.         return columnNames[col];  
  68.     }  
  69.  
  70.     public Object getValueAt(int row, int col) {  
  71.        
  72.         return data[row][col];  
  73.     }  
  74.    
  75.     public Class getColumnClass(int c) {  
  76.      if(c==1){  
  77.       System.out.println("---"+getValueAt(0, c).getClass());  
  78.      }  
  79.      return c == 1 ? Icon.class : Object.class;  
  80.  
  81.     }  
  82.  
  83.     public boolean isCellEditable(int row, int col) {   
  84.         if (col < 1) {  
  85.             return false;  
  86.         } else {  
  87.             return true;  
  88.         }  
  89.     }  
  90.  
  91.     public void setValueAt(Object value, int row, int col) {  
  92.          
  93.  
  94.         data[row][col] = value;  
  95.         fireTableCellUpdated(row, col);  
  96.  
  97.         
  98.     }   
  99. }//定义  
  100. class ImageRenderer extends DefaultTableCellRenderer {  
  101.    
  102.      public Component getTableCellRendererComponent(JTable table, Object value,  
  103.        boolean isSelected, boolean hasFocus, int row, int column) {  
  104.       setIcon(null);    
  105.       setBorder(null);   
  106.       System.out.println("++"+(value instanceof Icon));  
  107.       super.getTableCellRendererComponent(table, value, isSelected,  hasFocus,  row,  column);  
  108.       if (value instanceof Icon) {  
  109.  
  110.        this.setIcon((Icon) value);  
  111.         
  112.       if (isSelected)  
  113.        setBorder(new LineBorder(Color.red));  
  114.       }else if (value instanceof String)  
  115.        setText((String) value);  
  116.       else 
  117.        setText("");  
  118.  
  119.       return this;  
  120.      }  
  121.  
  122. }  
  123. //设置图片和边框  
  124. class TextAndIcon {  
  125.  
  126.      Icon icon;  
  127.      TextAndIcon(String text, ImageIcon icon) {  
  128.       this.text = text;  
  129.       this.icon = icon;  
  130.      }  
  131.  
  132.      String text;  
  133.  
  134.      public String getText() {  
  135.       return text;  
  136.      }  
  137.  
  138.      public void setText(String text) {  
  139.       this.text = text;  
  140.      }  
  141.  
  142.      public Icon getIcon() {  
  143.       return icon;  
  144.      }  
  145.  
  146.      public void setIcon(ImageIcon icon) {  
  147.       this.icon = icon;  
  148.      }  
  149.  

系统托盘和PopupMenu的使用

  1. package roy;  
  2.  
  3. import java.awt.AWTException;  
  4. import java.awt.MenuItem;  
  5. import java.awt.PopupMenu;  
  6. import java.awt.SystemTray;  
  7. import java.awt.TrayIcon;  
  8. import java.awt.event.ActionEvent;  
  9. import java.awt.event.ActionListener;  
  10. import java.awt.event.WindowAdapter;  
  11. import java.awt.event.WindowEvent;  
  12. import javax.swing.ImageIcon;  
  13. import javax.swing.JFrame;  
  14.  
  15. /**  
  16.  *  Author:Roy  
  17.  */ 
  18. public class TestTray extends JFrame implements Runnable {  
  19.  
  20.     private SystemTray sysTray;// 当前操作系统的托盘对象  
  21.     private TrayIcon trayIcon;// 当前对象的托盘  
  22.     ImageIcon icon = null;  
  23.  
  24.     public TestTray()  
  25.     {  
  26.         this.createTrayIcon();// 创建托盘对象  
  27.         init();  
  28.     }  
  29.  
  30.     /**  
  31.      * 初始化窗体的方法  
  32.      */ 
  33.      public void init()  
  34.     {  
  35.         this.setTitle("闪动托盘");  
  36.         this.setSize(400400);  
  37.         this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);  
  38.         this.setLocationRelativeTo(null);  
  39.         // 添加窗口事件,将托盘添加到操作系统的托盘  
  40.         this.addWindowListener(new WindowAdapter() {  
  41.    
  42.             public void windowIconified(WindowEvent e)  
  43.             {  
  44.                 addTrayIcon();  
  45.             }  
  46.         });  
  47.  
  48.         this.setVisible(true);  
  49.     }  
  50.  
  51.     /**  
  52.      * 添加托盘的方法  
  53.      */ 
  54.     public void addTrayIcon()  
  55.     {  
  56.         try 
  57.         {  
  58.             sysTray.add(trayIcon);// 将托盘添加到操作系统的托盘  
  59.             setVisible(false);// 使得当前的窗口隐藏  
  60.             new Thread(this).start();  
  61.         }  
  62.         catch (AWTException e1)  
  63.         {  
  64.             e1.printStackTrace();  
  65.         }  
  66.     }  
  67.  
  68.     /**  
  69.      * 创建系统托盘的对象 步骤: 1,获得当前操作系统的托盘对象 2,创建弹出菜单popupMenu 3,创建托盘图标icon  
  70.      * 4,创建系统的托盘对象trayIcon  
  71.      */ 
  72.     public void createTrayIcon()  
  73.     {  
  74.         sysTray = SystemTray.getSystemTray();// 获得当前操作系统的托盘对象  
  75.         icon = new ImageIcon("1.gif");// 托盘图标  
  76.         PopupMenu popupMenu = new PopupMenu();// 弹出菜单  
  77.         MenuItem mi = new MenuItem("弹出");  
  78.         MenuItem exit = new MenuItem("关闭");  
  79.         popupMenu.add(mi);  
  80.         popupMenu.add(exit);  
  81.         // 为弹出菜单项添加事件  
  82.         mi.addActionListener(new ActionListener() {  
  83.  
  84.             public void actionPerformed(ActionEvent e)  
  85.             {  
  86.                 setVisible(true);  
  87.                 sysTray.remove(trayIcon);  
  88.             }  
  89.         });  
  90.         exit.addActionListener(new ActionListener() {  
  91.  
  92.             public void actionPerformed(ActionEvent e)  
  93.             {  
  94.                 System.exit(0);  
  95.             }  
  96.         });  
  97.         trayIcon = new TrayIcon(icon.getImage(), "闪动托盘", popupMenu);  
  98.     }  
  99.    
  100.     public static void main(String[] args)  
  101.     {  
  102.         TestTray testTray = new TestTray();  
  103.     }  
  104.  
  105.     /*  
  106.      * 线程控制闪动、替换图片  
  107.      *   
  108.      */ 
  109.     public void run()  
  110.     {  
  111.         while (true)  
  112.         {  
  113.             this.trayIcon.setImage(new ImageIcon("2.jpg").getImage());  
  114.             try 
  115.             {  
  116.                 Thread.sleep(300);  
  117.             }  
  118.             catch (InterruptedException e)  
  119.             {  
  120.                 e.printStackTrace();  
  121.             }  
  122.             this.trayIcon.setImage(icon.getImage());  
  123.             try 
  124.             {  
  125.                 Thread.sleep(300);  
  126.             }  
  127.             catch (InterruptedException e)  
  128.             {  
  129.                 e.printStackTrace();  
  130.             }  
  131.             
  132.         }  
  133.     }  
  134. }  

实现拖拉上传效果的界面

  1. package roy;  
  2.  
  3. import java.awt.BorderLayout;  
  4. import java.awt.datatransfer.DataFlavor;  
  5. import java.awt.dnd.DnDConstants;  
  6. import java.awt.dnd.DropTarget;  
  7. import java.awt.dnd.DropTargetDragEvent;  
  8. import java.awt.dnd.DropTargetDropEvent;  
  9. import java.awt.dnd.DropTargetEvent;  
  10. import java.awt.dnd.DropTargetListener;  
  11. import java.io.File;  
  12. import java.util.Iterator;  
  13. import java.util.List;  
  14. import java.util.Vector;  
  15. import java.util.logging.Logger;  
  16.  
  17. import javax.swing.JFrame;  
  18. import javax.swing.JOptionPane;  
  19. import javax.swing.JPanel;  
  20. import javax.swing.JProgressBar;  
  21. import javax.swing.JScrollPane;  
  22. import javax.swing.JTable;  
  23. import javax.swing.table.JTableHeader;  
  24.    
  25. /**  
  26.  *   
  27.  * @author Roy  
  28.  * desc: 上传文件面板  
  29.  */ 
  30. public class UploadPane extends JPanel implements DropTargetListener{  
  31.       
  32.     private String[] columns={"文件名","路径","上传人"};//表格标题  
  33.     private Vector data=new Vector();//表格数据  
  34.     private JTable table;//上传表格  
  35.     private MyTableModel model;//表格数据模型号  
  36.     private Vector files=new Vector();//保存上传的文件路径  
  37.     private String uploadName;//上传人的姓名  
  38.     private JProgressBar jpb;//进度条  
  39.     private UIFactory uiFactory=UIFactory.getInstance();//界面工厂   
  40.       
  41.     public UploadPane(String uploadName){  
  42.         this.uploadName=uploadName;  
  43.         //创建一个拖放目标,本身  
  44.         new DropTarget(this,DnDConstants.ACTION_COPY_OR_MOVE,this);  
  45.           
  46.         jpb=uiFactory.createJProgressBar(0,100,0,true,true);  
  47.         this.setLayout(new BorderLayout());  
  48.         model=new MyTableModel(columns,data);  
  49.         table=uiFactory.createJTable(model);  
  50.         JTableHeader header=table.getTableHeader();  
  51.         header.setReorderingAllowed(false);  
  52.         JScrollPane sc=uiFactory.createJScrollPane(table);  
  53.         this.add(sc,BorderLayout.CENTER);  
  54.         this.add(jpb,BorderLayout.SOUTH);  
  55.     }  
  56.       
  57.     public void drop(DropTargetDropEvent e) {  
  58.         try{  
  59.             if(e.isDataFlavorSupported(DataFlavor.javaFileListFlavor)){  
  60.                 e.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);//接受copy or move   
  61.                 List list=(List)(e.getTransferable().getTransferData(DataFlavor.javaFileListFlavor));  
  62.                 Iterator it=list.iterator();  
  63.                 while(it.hasNext()){  
  64.                     File f=(File)it.next();  
  65.                     if(f.isFile()){  
  66.                         add(f);  
  67.                     }else{  
  68.                         JOptionPane.showMessageDialog(this,"上传文件不能为文件夹");  
  69.                     }  
  70.                 }  
  71.                 e.dropComplete(true);  
  72.                 this.updateUI();  
  73.             }else{  
  74.                 e.rejectDrop();  
  75.             }  
  76.         }catch(Exception ex){   
  77.             ex.printStackTrace();  
  78.         }  
  79.     }  
  80.       
  81.     //提供一个添加数据的方法  
  82.     public void add(File f){  
  83.         jpb.setValue(0);  
  84.         Vector row=new Vector();  
  85.         row.add(f.getName());  
  86.         row.add(f.getPath());  
  87.         row.add(uploadName);  
  88.         data.add(row);  
  89.         table.updateUI();//向表格添加数据  
  90.         files.add(f);  
  91.     }  
  92.     //提共一个移除掉的方法  
  93.     public void remove(){  
  94.         data.clear();  
  95.         files.clear();  
  96.         table.updateUI();  
  97.     }  
  98.       
  99.     //getters and setters ....  
  100.  
  101.     public String[] getColumns() {  
  102.         return columns;  
  103.     }  
  104.     public void setColumns(String[] columns) {  
  105.         this.columns = columns;  
  106.     }  
  107.     public Vector getData() {  
  108.         return data;  
  109.     }  
  110.     public void setData(Vector data) {  
  111.         this.data = data;  
  112.     }  
  113.     public JTable getTable() {  
  114.         return table;  
  115.     }  
  116.     public void setTable(JTable table) {  
  117.         this.table = table;  
  118.     }  
  119.     public MyTableModel getModel() {  
  120.         return model;  
  121.     }  
  122.     public void setModel(MyTableModel model) {  
  123.         this.model = model;  
  124.     }  
  125.  
  126.  
  127.     public JProgressBar getJpb() {  
  128.         return jpb;  
  129.     }  
  130.  
  131.  
  132.     public void setJpb(JProgressBar jpb) {  
  133.         this.jpb = jpb;  
  134.     }  
  135.  
  136.       
  137.     public String getUploadName() {  
  138.         return uploadName;  
  139.     }  
  140.  
  141.     public void setUploadName(String uploadName) {  
  142.         this.uploadName = uploadName;  
  143.     }  
  144.  
  145.     public Vector getFiles() {  
  146.         return files;  
  147.     }  
  148.  
  149.     public void setFiles(Vector files) {  
  150.         this.files = files;  
  151.     }  
  152.  
  153.     public void dragEnter(DropTargetDragEvent e) {}  
  154.     public void dragExit(DropTargetEvent e) {}  
  155.     public void dragOver(DropTargetDragEvent e) {}  
  156.     public void dropActionChanged(DropTargetDragEvent e) {}  
  157.       
  158.       
  159.     public static void main(String args[]){  
  160.         JFrame f=new JFrame();  
  161.         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  162.         UploadPane p=new UploadPane("Joe");  
  163.         f.add(p);  
  164.         f.pack();  
  165.         f.setVisible(true);  
  166.     }  
  167. }  

最后再给大家一个UI的工厂来解决其他的问题,是平时常用的组件的创建(自动注册按钮、快捷键等),应有尽有。使用了单例模式。

  1. package roy;  
  2.  
  3. import java.awt.BorderLayout;  
  4. import java.awt.CardLayout;  
  5. import java.awt.Color;  
  6. import java.awt.Container;  
  7. import java.awt.Dimension;  
  8. import java.awt.FlowLayout;  
  9. import java.awt.Font;  
  10. import java.awt.GridLayout;  
  11. import java.awt.MenuItem;  
  12. import java.awt.event.ActionListener;  
  13. import java.awt.event.ItemListener;  
  14. import java.awt.event.WindowListener;  
  15. import java.io.FileNotFoundException;  
  16. import java.util.Vector;  
  17.  
  18. import javax.swing.Icon;  
  19. import javax.swing.ImageIcon;  
  20. import javax.swing.JButton;  
  21. import javax.swing.JCheckBox;  
  22. import javax.swing.JComboBox;  
  23. import javax.swing.JFrame;  
  24. import javax.swing.JLabel;  
  25. import javax.swing.JList;  
  26. import javax.swing.JMenuItem;  
  27. import javax.swing.JPanel;  
  28. import javax.swing.JPasswordField;  
  29. import javax.swing.JProgressBar;  
  30. import javax.swing.JRadioButton;  
  31. import javax.swing.JScrollPane;  
  32. import javax.swing.JSplitPane;  
  33. import javax.swing.JTabbedPane;  
  34. import javax.swing.JTable;  
  35. import javax.swing.JTextArea;  
  36. import javax.swing.JTextField;  
  37. import javax.swing.JTextPane;  
  38. import javax.swing.JToggleButton;  
  39. import javax.swing.JToolBar;  
  40. import javax.swing.KeyStroke;  
  41. import javax.swing.event.ChangeListener;  
  42. import javax.swing.event.ListSelectionListener;  
  43. import javax.swing.table.AbstractTableModel;  
  44.    
  45.  
  46. public class UIFactory {  
  47.  
  48.     private static UIFactory instance;//当前工厂的一个实例  
  49.     //私有的构造器  
  50.     private UIFactory(){  
  51.           
  52.     }  
  53.     //获得当前工厂的实例  
  54.     public static UIFactory getInstance(){  
  55.         if(instance==null){  
  56.             synchronized("key1"){  
  57.                 instance=new UIFactory();  
  58.             }  
  59.         }  
  60.         return instance;  
  61.     }  
  62.        
  63.     //产生一个标签  
  64.     public JLabel createJLabel(String text){  
  65.         JLabel l=new JLabel(text);  
  66.         return l;  
  67.     }  
  68.     //产生一个带图标的标签  
  69.     public JLabel createJLabel(String text,Icon icon){  
  70.         JLabel l=new JLabel(text);  
  71.         l.setIcon(icon);  
  72.         return l;  
  73.     }  
  74.       
  75.       
  76.     //专门产生按钮,而且添加快捷键      文本显示  命令  按键   辅助键  触发范围  监听器  是否显焦  
  77.     public JButton createJButton(String text,int mn,String command,ImageIcon icon,int key_keyEvent,int input_inputEvent,int when_invoke_jComponent,ActionListener al,boolean isFocus){  
  78.         JButton button=new JButton(text,icon);  
  79.         //这里默认会是alt+'mn'  
  80.         button.setMnemonic(mn);//设置辅助键  
  81.         button.setActionCommand(command);//设置动作  
  82.         button.addActionListener(al);  
  83.         KeyStroke stroke=KeyStroke.getKeyStroke(key_keyEvent,input_inputEvent,isFocus);  
  84.         button.registerKeyboardAction(al,command,stroke,when_invoke_jComponent);  
  85.         button.setFocusable(isFocus);  
  86.         return button;  
  87.     }  
  88.     //专门产生按钮                     文本显示  命令  监听器  是否显焦  
  89.     public JButton createJButton(String text,String command,ImageIcon icon,ActionListener al,boolean isFocus){  
  90.         JButton button=new JButton(text,icon);  
  91.         button.setActionCommand(command);//设置动作  
  92.         button.addActionListener(al);//添加事件监听  
  93.         button.setFocusable(isFocus);  
  94.         return button;  
  95.     }  
  96.     //专门产生按钮,而且添加快捷键 文本显示  命令  按键   辅助键  触发范围  监听器  是否显焦  
  97.     public JButton createJButton(String text,int mn,String command,ImageIcon icon,int width,int height,int key_keyEvent,int input_inputEvent,int when_invoke_jComponent,ActionListener al,boolean isFocus){  
  98.  
  99.         JButton button=new JButton(text,icon);//指定按钮的  文本和图片  
  100.         //这里默认会是alt+'mn'  a    
  101.         button.setMnemonic(mn);//设置辅助键  
  102.         button.setActionCommand(command);//设置动作  【添加】  
  103.  
  104.  
  105.         button.setPreferredSize(new Dimension(width,height));//指定按钮的大小  
  106.         button.addActionListener(al);  
  107.         KeyStroke stroke=KeyStroke.getKeyStroke(key_keyEvent,input_inputEvent,isFocus);  
  108.         button.registerKeyboardAction(al,command,stroke,when_invoke_jComponent);  
  109.         button.setFocusable(isFocus);  
  110.         return button;  
  111.     }  
  112.     //专门产生按钮                     文本显示  命令  监听器  是否显焦  
  113.     public JButton createJButton(String text,String command,ImageIcon icon,int width,int height,ActionListener al,boolean isFocus){  
  114.         JButton button=new JButton(text,icon);  
  115.         button.setActionCommand(command);//设置动作  
  116.         button.setPreferredSize(new Dimension(width,height));  
  117.         button.addActionListener(al);//添加事件监听  
  118.         button.setFocusable(isFocus);  
  119.         return button;  
  120.     }  
  121.     //生成文本框   默认文本  文本框大小长 是否可编辑  
  122.     public JTextField createJTextField(String text,int cols,boolean isEditable){  
  123.         JTextField field=new JTextField(text,cols);  
  124.         field.setEditable(isEditable);  
  125.         return field;  
  126.     }  
  127.     //生成文本框   默认文本  文本框大小长 是否可编辑  设置字体  
  128.     public JTextField createJTextField(String text,int cols,boolean isEditable,Font f){  
  129.         JTextField field=new JTextField(text,cols);  
  130.         field.setEditable(isEditable);  
  131.         field.setFont(f);  
  132.         return field;  
  133.     }  
  134.     //生成文本域    默认文本  列数  行数  是否可编辑  
  135.     public JTextArea createJTextArea(String text,int cols,int rows,boolean isEditable,boolean lineWrap,boolean wrapStyleWord){  
  136.         JTextArea area=new JTextArea(text,cols,rows);  
  137.         area.setEditable(isEditable);  
  138.         area.setLineWrap(lineWrap);  
  139.         area.setWrapStyleWord(wrapStyleWord);  
  140.         return area;  
  141.     }  
  142.     //生成文本域    默认文本  列数  行数  是否可编辑  设置字体   
  143.     public JTextArea createJTextArea(String text,int cols,int rows,boolean isEditable,boolean lineWrap,boolean wrapStyleWord,Font f){  
  144.         JTextArea area=new JTextArea(text,cols,rows);  
  145.         area.setEditable(isEditable);  
  146.         area.setFont(f);  
  147.         area.setLineWrap(lineWrap);  
  148.         area.setWrapStyleWord(wrapStyleWord);  
  149.         return area;  
  150.     }  
  151.     //生成文本域    默认文本  列数  行数  是否可编辑  设置字体  背景颜色  前景颜色  
  152.     public JTextArea createJTextArea(String text,int cols,int rows,boolean isEditable,Font f,boolean lineWrap,boolean wrapStyleWord,Color background, Color foreground){  
  153.         JTextArea area=new JTextArea(text,cols,rows);  
  154.         area.setEditable(isEditable);  
  155.         area.setFont(f);  
  156.         area.setLineWrap(lineWrap);  
  157.         area.setWrapStyleWord(wrapStyleWord);  
  158.         area.setBackground(background);  
  159.         area.setForeground(foreground);  
  160.         return area;  
  161.     }  
  162.     //产生一个滚动面板  
  163.     public JScrollPane createJScrollPane(Container c){  
  164.         JScrollPane sc=new JScrollPane(c);  
  165.         return sc;  
  166.     }  
  167.     public static void main(String args[]){  
  168.           
  169.         UIFactory factory = UIFactory.getInstance();  
  170.  
  171.         //JScrollPane pan = factory.createJScrollPane(new JFrame(),JScrollPane.horizontalScrollBar,JScrollPane.verticalScrollBarPolicy);  
  172.           
  173.     }  
  174.     //产生一个指定垂直和水平滚动策略的滚动面板  水平滚动策略  垂直滚动策略  
  175.     public JScrollPane createJScrollPane(Container c,int horizontal_policy,int vertical_policy){  
  176.         JScrollPane sc=new JScrollPane();  
  177.         sc.setHorizontalScrollBarPolicy(horizontal_policy);  
  178.         sc.setVerticalScrollBarPolicy(vertical_policy);  
  179.         return sc;  
  180.     }  
  181.     //设置框架属性                    传入的框架  关闭策略  x,y,width,height   
  182.     public void setJFrameAttributes(JFrame f,String title,int close_policy,int x,int y,int width,int height,boolean isResizable){  
  183.         f.setTitle(title);  
  184.         f.setDefaultCloseOperation(close_policy);  
  185.         f.setLocation(x,y);  
  186.         f.setSize(width,height);  
  187.         f.setResizable(isResizable);  
  188.     }  
  189.       
  190.     //设置框架属性                    传入的框架  关闭策略  x,y,width,height   
  191.     public void setJFrameAttributes(JFrame f,String title,int close_policy,int x,int y,int width,int height,boolean isResizable,WindowListener wl){  
  192.         f.setTitle(title);  
  193.         f.setDefaultCloseOperation(close_policy);  
  194.         f.setLocation(x,y);  
  195.         f.setSize(width,height);  
  196.         f.setResizable(isResizable);  
  197.         f.addWindowListener(wl);  
  198.     }  
  199.     //产生一条列表组件   条目宽度  
  200.     public JList createJList(int cell_width){  
  201.         JList list=new JList();  
  202.         list.setFixedCellWidth(cell_width);  
  203.         return list;  
  204.     }  
  205.     //产生一条列表组件   条目宽度  列表选择监听器  
  206.     public JList createJList(int cell_width,ListSelectionListener lsl){  
  207.         JList list=new JList();  
  208.         list.setFixedCellWidth(cell_width);  
  209.         list.addListSelectionListener(lsl);  
  210.         return list;  
  211.     }  
  212.     //产生一条列表组件   条目宽度  条目间距  源数据  
  213.     public JList createJList(int cell_width,int cell_height,Vector<Object> v,ListSelectionListener lsl){  
  214.         JList list=new JList(v);  
  215.         list.setFixedCellWidth(cell_width);  
  216.         list.setFixedCellHeight(cell_height);  
  217.         list.addListSelectionListener(lsl);  
  218.         return list;  
  219.     }  
  220.     //产生一条列表组件   条目宽度  条目间距  源数据  
  221.     public JList createJList(int cell_width,int cell_height,Object[] data,ListSelectionListener lsl){  
  222.         JList list=new JList(data);  
  223.         list.setFixedCellWidth(cell_width);  
  224.         list.setFixedCellHeight(cell_height);  
  225.         list.addListSelectionListener(lsl);  
  226.         return list;  
  227.     }  
  228.     //获得一个具有BorderLayout布局的面板  
  229.     public JPanel createBorderJPanel(){  
  230.         JPanel p=new JPanel();  
  231.         p.setLayout(new BorderLayout());  
  232.         return p;  
  233.     }  
  234.     //获得一个具有BorderLayout布局的面板  
  235.     public JPanel createBorderJPanel(int row_distance,int col_distance){  
  236.         JPanel p=new JPanel();  
  237.         p.setLayout(new BorderLayout(row_distance,col_distance));  
  238.         return p;  
  239.     }  
  240.     //获得一个具有GridLayout布局的面板  指定行数,列数,行距,列距  
  241.     public JPanel createGridJPanel(int rows,int cols,int col_distance,int row_distance){  
  242.         JPanel p=new JPanel();  
  243.         p.setLayout(new GridLayout(rows,cols,col_distance,row_distance));  
  244.         return p;  
  245.     }  
  246.     //获得一个具有GridLayout布局的面板  
  247.     public JPanel createGridJPanel(int rows,int cols){  
  248.         JPanel p=new JPanel();  
  249.         p.setLayout(new GridLayout(rows,cols));  
  250.         return p;  
  251.     }  
  252.     //获得一个具有卡片布局的面板  
  253.     public JPanel createCardJPanel(CardLayout card){  
  254.         JPanel p=new JPanel();  
  255.         p.setLayout(card);  
  256.         return p;  
  257.     }  
  258.     //获得一个具有绝对布局的的面板  
  259.     public JPanel createAbsoluteJPanel(){  
  260.         JPanel p=new JPanel();  
  261.         p.setLayout(null);  
  262.         return p;  
  263.     }  
  264.     //获得一个具有FlowLayout流式布局的面板  
  265.     public JPanel createFlowJPanel(){  
  266.         JPanel p=new JPanel();  
  267.         p.setLayout(new FlowLayout());  
  268.         return p;  
  269.     }  
  270.     //获得一个具有FlowLayout流式布局的面板  
  271.     public JPanel createFlowJPanel(int policy,int col_distance,int row_distance){  
  272.         JPanel p=new JPanel();  
  273.         p.setLayout(new FlowLayout( policy, col_distance, row_distance));  
  274.         return p;  
  275.     }  
  276.     //获得一个具有FlowLayout流式布局的面板  指定左右对齐方式  
  277.     public JPanel createFlowJPanel(int leftOrRight_policy){  
  278.         JPanel p=new JPanel();  
  279.         p.setLayout(new FlowLayout(leftOrRight_policy));  
  280.         return p;  
  281.     }  
  282.     //获得一个带有监听器的菜单选项  
  283.     public JMenuItem createJMenuItem(String text,String command,ActionListener al){  
  284.         JMenuItem item=new JMenuItem(text);  
  285.         item.addActionListener(al);  
  286.         item.setActionCommand(command);  
  287.         return item;  
  288.     }  
  289.     //获得一个JRadioButton按钮  
  290.     public JRadioButton createJRadioButton(){  
  291.         JRadioButton jrb=new JRadioButton();  
  292.         return jrb;  
  293.     }  
  294.     //获得一个带ItemListener监听器的JRadioButton按钮  
  295.     public JRadioButton createJRadioButton(ItemListener il){  
  296.         JRadioButton jrb=new JRadioButton();  
  297.         jrb.addItemListener(il);  
  298.         return jrb;  
  299.     }  
  300.     //获得一个复选框  
  301.     public JCheckBox createJCheckBox(){  
  302.         JCheckBox box=new JCheckBox();  
  303.         return box;  
  304.     }  
  305.     //获得一个下拉列表框  
  306.     public JComboBox createJComboBox(String[] data){  
  307.         JComboBox box=new JComboBox(data);  
  308.         return box;  
  309.     }  
  310.     //获得一个下拉列表框  
  311.     public JComboBox createJComboBox(Vector data){  
  312.         JComboBox box=new JComboBox(data);  
  313.         return box;  
  314.     }  
  315.     //获得一个下拉列表框  
  316.     public JComboBox createJComboBox(String[] data,int width,int height){  
  317.         JComboBox box=new JComboBox(data);  
  318.         box.setPreferredSize(new Dimension(width,height));  
  319.         return box;  
  320.     }  
  321.     //获得一个下拉列表框  
  322.     public JComboBox createJComboBox(Vector data,int width,int height){  
  323.         JComboBox box=new JComboBox(data);  
  324.         box.setPreferredSize(new Dimension(width,height));  
  325.         return box;  
  326.     }  
  327.     //获得一个带ItemListener的下拉列表框  
  328.     public JComboBox createJComboBox(Vector data,ItemListener il){  
  329.         JComboBox box=new JComboBox(data);  
  330.         box.addItemListener(il);  
  331.         return box;  
  332.     }  
  333.     //获得一个带ItemListener的下拉列表框  
  334.     public JComboBox createJComboBox(String[] data,ItemListener il){  
  335.         JComboBox box=new JComboBox(data);  
  336.         box.addItemListener(il);  
  337.         return box;  
  338.     }  
  339.     //获得一个带ItemListener的下拉列表框  
  340.     public JComboBox createJComboBox(Vector data,int width,int height,ItemListener il){  
  341.         JComboBox box=new JComboBox(data);  
  342.         box.setMaximumSize(new Dimension(width,height));  
  343.         box.setMinimumSize(new Dimension(width,height));  
  344.         box.addItemListener(il);  
  345.         return box;  
  346.     }  
  347.     //获得一个带ItemListener的下拉列表框  
  348.     public JComboBox createJComboBox(String[] data,int width,int height,ItemListener il){  
  349.         JComboBox box=new JComboBox(data);  
  350.         box.setMaximumSize(new Dimension(width,height));  
  351.         box.setMinimumSize(new Dimension(width,height));  
  352.         box.addItemListener(il);  
  353.         return box;  
  354.     }  
  355.     //产生一个选项卡面板  
  356.     public JTabbedPane createJTabbedPane(){  
  357.         JTabbedPane tab=new JTabbedPane();  
  358.         return tab;  
  359.     }  
  360.     //产生一个指定位置的选项卡面板  
  361.     public JTabbedPane createJTabbedPane(int pos){  
  362.         JTabbedPane tab=new JTabbedPane(pos);  
  363.         return tab;  
  364.     }  
  365.     //产生一个带ChangeListener的选项卡面板  
  366.     public JTabbedPane createJTabbedPane(ChangeListener cl){  
  367.         JTabbedPane tab=new JTabbedPane();  
  368.         tab.addChangeListener(cl);  
  369.         return tab;  
  370.     }  
  371.     //产生一个是否可以浮动的工具栏  
  372.     public JToolBar createJToolBar(boolean isFloatable){  
  373.         JToolBar bar =new JToolBar();  
  374.         bar.setFloatable(isFloatable);  
  375.         return bar;  
  376.     }  
  377.     //产生一个JTextPane面板  
  378.     public JTextPane createJTextPane(boolean isEditable){  
  379.         JTextPane p=new JTextPane();  
  380.         p.setEditable(isEditable);  
  381.         return p;  
  382.     }  
  383.     //产生一个JTextPane面板  
  384.     public JTextPane createJTextPane(boolean isEditable,Color background,Color foreground){  
  385.         JTextPane p=new JTextPane();  
  386.         p.setEditable(isEditable);  
  387.         p.setBackground(background);  
  388.         p.setForeground(foreground);  
  389.         return p;  
  390.     }  
  391.     //产生一个进度条  
  392.     public JProgressBar createJProgressBar(int min,int max,int value,boolean stringPainted,boolean borderPainted){  
  393.         JProgressBar bar=new JProgressBar();  
  394.         bar.setMinimum(min);  
  395.         bar.setMaximum(max);  
  396.         bar.setBorderPainted(borderPainted);  
  397.         bar.setStringPainted(stringPainted);  
  398.         bar.setValue(value);  
  399.         return bar;  
  400.     }  
  401.     //产生一个指定大小的进度条  
  402.     public JProgressBar createJProgressBar(int min,int max,int value,boolean stringPainted,boolean borderPainted,Dimension d){  
  403.         JProgressBar bar=new JProgressBar();  
  404.         bar.setMinimum(min);  
  405.         bar.setMaximum(max);  
  406.         bar.setBorderPainted(borderPainted);  
  407.         bar.setStringPainted(stringPainted);  
  408.         bar.setValue(value);  
  409.         bar.setPreferredSize(d);  
  410.         return bar;  
  411.     }  
  412.     //产生一个分隔面板                       分隔面板1  分隔面板2   水平或垂直分割  分割条位置   分割条宽度  是否可收起  
  413.     public JSplitPane createJSplitPane(Container c1,Container c2,int horizontalOrVertical,int dividerLoaction,int dividerSize,boolean expandable){  
  414.         JSplitPane splitPane=new JSplitPane(horizontalOrVertical,  
  415.                 true,c1,c2);  
  416.         splitPane.setDividerLocation(dividerLoaction);  
  417.         splitPane.setOneTouchExpandable(expandable);  
  418.         splitPane.setDividerSize(dividerSize);  
  419.         return splitPane;  
  420.     }  
  421.     //产生一个JToggleButton  
  422.     public JToggleButton createJToggleButton(String text,String command,Icon icon,int width,int height,ActionListener al, boolean selected){  
  423.         JToggleButton button=new JToggleButton(text,icon,selected);  
  424.         button.setActionCommand(command);  
  425.         button.addActionListener(al);  
  426.         button.setPreferredSize(new Dimension(width,height));  
  427.         return button;  
  428.     }  
  429.     //产生一个JToggleButton  
  430.     public JToggleButton createJToggleButton(String text,String command,Icon icon,int width,int height,ActionListener al){  
  431.         JToggleButton button=new JToggleButton(text,icon);  
  432.         button.setActionCommand(command);  
  433.         button.addActionListener(al);  
  434.         button.setPreferredSize(new Dimension(width,height));  
  435.         return button;  
  436.     }  
  437.     //产生将组件结合起来的面板  
  438.     public JPanel createJPanelWithComponents(JLabel label,Container field,int policy){  
  439.         JPanel p=this.createFlowJPanel(policy);  
  440.         p.add(label);  
  441.         p.add(field);  
  442.         return p;  
  443.     }  
  444.     //产生将两个面板整合起来的面板  
  445.     public JPanel createJPanelWithComponents(Container c1,Container c2,int policy){  
  446.         JPanel p=this.createFlowJPanel(policy,0,0);  
  447.         p.add(c1);  
  448.         p.add(c2);  
  449.         return p;  
  450.     }  
  451.     //产生将三个面板整合起来的面板  
  452.     public JPanel createJPanelWithComponents(Container c1,Container c2,Container c3,int policy){  
  453.         JPanel p=this.createFlowJPanel(policy,0,0);  
  454.         p.add(c1);  
  455.         p.add(c2);  
  456.         p.add(c3);  
  457.         return p;  
  458.     }  
  459.     //产生将四个面板整合起来的面板  
  460.     public JPanel createJPanelWithComponents(Container c1,Container c2,Container c3,Container c4,int policy){  
  461.         JPanel p=this.createFlowJPanel(policy,0,0);  
  462.         p.add(c1);  
  463.         p.add(c2);  
  464.         p.add(c3);  
  465.         p.add(c4);  
  466.         return p;  
  467.     }  
  468.     //产生一个普通表格  
  469.     public JTable createJTable(){  
  470.         JTable table=new JTable();  
  471.         return table;  
  472.     }  
  473.     //产生一个带AbstractTableModel的表格  
  474.     public JTable createJTable(AbstractTableModel model){  
  475.         JTable table=new JTable(model);  
  476.         return table;  
  477.     }  
  478.     //产生一个密码框  
  479.     //生成文本框   默认文本  文本框大小长 是否可编辑  
  480.     public JPasswordField createJPasswordField(String text,int cols,boolean isEditable){  
  481.         JPasswordField field=new JPasswordField(text,cols);  
  482.         field.setEditable(isEditable);  
  483.         return field;  
  484.     }  
  485.        
  486.     //产生一个带监听器的菜单项  
  487.     public MenuItem createMenuItem(String text,ActionListener al){  
  488.         MenuItem mi=new MenuItem(text);  
  489.         mi.addActionListener(al);  
  490.         return mi;  
  491.     }  
  492.     //产生一个带监听器的菜单项  
  493.     public MenuItem createMenuItem(String text,String command,ActionListener al){  
  494.         MenuItem mi=new MenuItem(text);  
  495.         mi.setActionCommand(command);  
  496.         mi.addActionListener(al);  
  497.         return mi;  
  498.     }  
  499. }  

原文链接:http://blog.csdn.net/royliang_peng/article/details/6292841

【编辑推荐】

  1. 浅析Swing线程模型和EDT
  2. Swing使用Substance外观包异常问题
  3. Swing多线程编码过程中的误区
  4. 控件位置可以配置的Swing桌面
  5. Swing特效:渐显效果
责任编辑:林师授 来源: royliang_peng的博客
相关推荐

2011-03-18 09:36:18

Oracle数据库

2009-07-15 17:09:32

Swing线程

2012-08-24 09:15:32

移动应用开发解决方案

2010-06-12 12:46:04

Grub Rescue

2019-10-08 16:05:19

Redis数据库系统

2015-05-12 16:31:22

Elasticsear开源分布式搜索引擎

2018-12-29 14:26:58

物联网IOT物联网设备

2013-01-16 09:34:01

WLAN解决方案

2013-04-19 09:46:05

移动应用开发解决方案mobile apps

2017-08-03 09:37:35

SparkStreamKafkaDirect

2015-10-08 08:51:40

PHP内存耗尽解决方案

2020-03-11 09:57:10

数据安全网络安全网络攻击

2010-08-31 16:09:04

DIV+CSS

2010-08-26 14:00:28

CSSmargin

2010-11-25 15:35:02

2010-05-17 09:49:46

MySQL中文问题

2011-03-02 14:56:56

FileZilla425问题

2016-09-27 21:14:53

JavaURL

2011-08-03 09:44:18

IOS开发 UITextFiel UITableVie

2010-03-12 18:22:51

Python文本乱码
点赞
收藏

51CTO技术栈公众号