ITeye博客

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

263229365 · 2013-03-07 09:50 · Swing · 编程语言 · Markdown

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

系统托盘和PopupMenu的使用

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

实现拖拉上传效果的界面

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

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

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

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。