---
title: "Swing开发时必定会碰到的11个问题及解决方案"
author: "263229365"
date: "2013-03-07T09:50:24Z"
tags: []
categories: ["编程语言"]
slug: "swing-11"
draft: false
lastmod: "2013-03-07T09:50:24Z"
id: 1825233
url: "https://263229365.iteye.com/blog/1825233"
---

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

2.系统托盘TrayIcon的使用

3.JPopupMenu的使用

4.JTree的使用及替换样式

5.BorderLayout的灵活使用

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

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

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

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

10.剪切板的使用

11.JFM拓展包

\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**

JTable放图片

001. package roy;
002. import java.awt.Color;
003. import java.awt.Component;
004. import java.awt.Dimension;
005. import javax.swing.Icon;
006. import javax.swing.ImageIcon;
007. import javax.swing.JFrame;
008. import javax.swing.JScrollPane;
009. import javax.swing.JTable;
010. import javax.swing.border.LineBorder;
011. import javax.swing.event.TableModelEvent;
012. import javax.swing.table.AbstractTableModel;
013. import javax.swing.table.DefaultTableCellRenderer;
014. import javax.swing.table.DefaultTableModel;
015. import javax.swing.table.TableColumnModel;
016. //
017. public class MyTable{
018. JTable table=new JTable();
019. public MyTable(){
020. JFrame frame=new JFrame("测试jtable放图片");
021. frame.setLayout(null);
022. table=this.getTable();
023. JScrollPane src=new JScrollPane(table);
024. src.setBounds(0, 0, 400, 200);
025. frame.setSize(new Dimension(400,200));
026. frame.add(src);
027. frame.setVisible(true);
028. }
029. public JTable getTable(){
030. MyTableModel dm = new MyTableModel();
031. JTable table = new JTable();
032. table.setModel(dm);//设置每个单元格的格式
033. table.setDefaultRenderer(Icon.class, new ImageRenderer());
034. return table;
035. }
036. public static void main(String args\[]){
037. new MyTable();
038. }
039. }
040. class MyTableModel extends AbstractTableModel{
041. private String\[] columnNames = {"First Name",
042. "Favorite Color",
043. };
044. private Object\[]\[] data = {
045. {"Marry", new ImageIcon("1.gif")
046. },
047. {"Joe",new ImageIcon("1.jpg")
048. },
049. {"Kims", new ImageIcon("1.png")
050. }
051. };
052. public int getColumnCount() {
053. return columnNames.length;
054. }
055. public int getRowCount() {
056. return data.length;
057. }
058. public String getColumnName(int col) {
059. return columnNames\[col];
060. }
061. public Object getValueAt(int row, int col) {
062. return data\[row]\[col];
063. }
064. public Class getColumnClass(int c) {
065. if(c==1){
066. System.out.println("---"+getValueAt(0, c).getClass());
067. }
068. return c == 1 ? Icon.class : Object.class;
069. }
070. public boolean isCellEditable(int row, int col) {
071. if (col &lt; 1) {
072. return false;
073. } else {
074. return true;
075. }
076. }
077. public void setValueAt(Object value, int row, int col) {
078. data\[row]\[col] = value;
079. fireTableCellUpdated(row, col);
080. }
081. }//定义
082. class ImageRenderer extends DefaultTableCellRenderer {
083. public Component getTableCellRendererComponent(JTable table, Object value,
084. boolean isSelected, boolean hasFocus, int row, int column) {
085. setIcon(null);
086. setBorder(null);
087. System.out.println("++"+(value instanceof Icon));
088. super.getTableCellRendererComponent(table, value, isSelected,  hasFocus,  row,  column);
089. if (value instanceof Icon) {
090. this.setIcon((Icon) value);
091. if (isSelected)
092. setBorder(new LineBorder(Color.red));
093. }else if (value instanceof String)
094. setText((String) value);
095. else
096. setText("");
097. return this;
098. }
099. }
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的使用**

001. package roy;
002. import java.awt.AWTException;
003. import java.awt.MenuItem;
004. import java.awt.PopupMenu;
005. import java.awt.SystemTray;
006. import java.awt.TrayIcon;
007. import java.awt.event.ActionEvent;
008. import java.awt.event.ActionListener;
009. import java.awt.event.WindowAdapter;
010. import java.awt.event.WindowEvent;
011. import javax.swing.ImageIcon;
012. import javax.swing.JFrame;
013. /\**
014. *  Author:Roy
015. \*/
016. public class TestTray extends JFrame implements Runnable {
017. private SystemTray sysTray;// 当前操作系统的托盘对象
018. private TrayIcon trayIcon;// 当前对象的托盘
019. ImageIcon icon = null;
020. public TestTray()
021. {
022. this.createTrayIcon();// 创建托盘对象
023. init();
024. }
025. /\**
026. * 初始化窗体的方法
027. \*/
028. public void init()
029. {
030. this.setTitle("闪动托盘");
031. this.setSize(400, 400);
032. this.setDefaultCloseOperation(this.EXIT\_ON\_CLOSE);
033. this.setLocationRelativeTo(null);
034. // 添加窗口事件,将托盘添加到操作系统的托盘
035. this.addWindowListener(new WindowAdapter() {
036. public void windowIconified(WindowEvent e)
037. {
038. addTrayIcon();
039. }
040. });
041. this.setVisible(true);
042. }
043. /\**
044. * 添加托盘的方法
045. \*/
046. public void addTrayIcon()
047. {
048. try
049. {
050. sysTray.add(trayIcon);// 将托盘添加到操作系统的托盘
051. setVisible(false);// 使得当前的窗口隐藏
052. new Thread(this).start();
053. }
054. catch (AWTException e1)
055. {
056. e1.printStackTrace();
057. }
058. }
059. /\**
060. * 创建系统托盘的对象 步骤: 1,获得当前操作系统的托盘对象 2,创建弹出菜单popupMenu 3,创建托盘图标icon
061. * 4,创建系统的托盘对象trayIcon
062. \*/
063. public void createTrayIcon()
064. {
065. sysTray = SystemTray.getSystemTray();// 获得当前操作系统的托盘对象
066. icon = new ImageIcon("1.gif");// 托盘图标
067. PopupMenu popupMenu = new PopupMenu();// 弹出菜单
068. MenuItem mi = new MenuItem("弹出");
069. MenuItem exit = new MenuItem("关闭");
070. popupMenu.add(mi);
071. popupMenu.add(exit);
072. // 为弹出菜单项添加事件
073. mi.addActionListener(new ActionListener() {
074. public void actionPerformed(ActionEvent e)
075. {
076. setVisible(true);
077. sysTray.remove(trayIcon);
078. }
079. });
080. exit.addActionListener(new ActionListener() {
081. public void actionPerformed(ActionEvent e)
082. {
083. System.exit(0);
084. }
085. });
086. trayIcon = new TrayIcon(icon.getImage(), "闪动托盘", popupMenu);
087. }
088. public static void main(String\[] args)
089. {
090. TestTray testTray = new TestTray();
091. }
092. /*
093. * 线程控制闪动、替换图片
094. \*
095. \*/
096. public void run()
097. {
098. while (true)
099. {
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. }

**实现拖拉上传效果的界面**

001. package roy;
002. import java.awt.BorderLayout;
003. import java.awt.datatransfer.DataFlavor;
004. import java.awt.dnd.DnDConstants;
005. import java.awt.dnd.DropTarget;
006. import java.awt.dnd.DropTargetDragEvent;
007. import java.awt.dnd.DropTargetDropEvent;
008. import java.awt.dnd.DropTargetEvent;
009. import java.awt.dnd.DropTargetListener;
010. import java.io.File;
011. import java.util.Iterator;
012. import java.util.List;
013. import java.util.Vector;
014. import java.util.logging.Logger;
015. import javax.swing.JFrame;
016. import javax.swing.JOptionPane;
017. import javax.swing.JPanel;
018. import javax.swing.JProgressBar;
019. import javax.swing.JScrollPane;
020. import javax.swing.JTable;
021. import javax.swing.table.JTableHeader;
022. /\**
023. \*
024. * @author Roy
025. * desc: 上传文件面板
026. \*/
027. public class UploadPane extends JPanel implements DropTargetListener{
028. private String\[] columns={"文件名","路径","上传人"};//表格标题
029. private Vector data=new Vector();//表格数据
030. private JTable table;//上传表格
031. private MyTableModel model;//表格数据模型号
032. private Vector files=new Vector();//保存上传的文件路径
033. private String uploadName;//上传人的姓名
034. private JProgressBar jpb;//进度条
035. private UIFactory uiFactory=UIFactory.getInstance();//界面工厂
036. public UploadPane(String uploadName){
037. this.uploadName=uploadName;
038. //创建一个拖放目标，本身
039. new DropTarget(this,DnDConstants.ACTION\_COPY\_OR\_MOVE,this);
040. jpb=uiFactory.createJProgressBar(0,100,0,true,true);
041. this.setLayout(new BorderLayout());
042. model=new MyTableModel(columns,data);
043. table=uiFactory.createJTable(model);
044. JTableHeader header=table.getTableHeader();
045. header.setReorderingAllowed(false);
046. JScrollPane sc=uiFactory.createJScrollPane(table);
047. this.add(sc,BorderLayout.CENTER);
048. this.add(jpb,BorderLayout.SOUTH);
049. }
050. public void drop(DropTargetDropEvent e) {
051. try{
052. if(e.isDataFlavorSupported(DataFlavor.javaFileListFlavor)){
053. e.acceptDrop(DnDConstants.ACTION\_COPY\_OR\_MOVE);//接受copy or move
054. List list=(List)(e.getTransferable().getTransferData(DataFlavor.javaFileListFlavor));
055. Iterator it=list.iterator();
056. while(it.hasNext()){
057. File f=(File)it.next();
058. if(f.isFile()){
059. add(f);
060. }else{
061. JOptionPane.showMessageDialog(this,"上传文件不能为文件夹");
062. }
063. }
064. e.dropComplete(true);
065. this.updateUI();
066. }else{
067. e.rejectDrop();
068. }
069. }catch(Exception ex){
070. ex.printStackTrace();
071. }
072. }
073. //提供一个添加数据的方法
074. public void add(File f){
075. jpb.setValue(0);
076. Vector row=new Vector();
077. row.add(f.getName());
078. row.add(f.getPath());
079. row.add(uploadName);
080. data.add(row);
081. table.updateUI();//向表格添加数据
082. files.add(f);
083. }
084. //提共一个移除掉的方法
085. public void remove(){
086. data.clear();
087. files.clear();
088. table.updateUI();
089. }
090. //getters and setters ....
091. public String\[] getColumns() {
092. return columns;
093. }
094. public void setColumns(String\[] columns) {
095. this.columns = columns;
096. }
097. public Vector getData() {
098. return data;
099. }
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的工厂来解决其他的问题，是平时常用的组件的创建（自动注册按钮、快捷键等），应有尽有。使用了单例模式。**

001. package roy;
002. import java.awt.BorderLayout;
003. import java.awt.CardLayout;
004. import java.awt.Color;
005. import java.awt.Container;
006. import java.awt.Dimension;
007. import java.awt.FlowLayout;
008. import java.awt.Font;
009. import java.awt.GridLayout;
010. import java.awt.MenuItem;
011. import java.awt.event.ActionListener;
012. import java.awt.event.ItemListener;
013. import java.awt.event.WindowListener;
014. import java.io.FileNotFoundException;
015. import java.util.Vector;
016. import javax.swing.Icon;
017. import javax.swing.ImageIcon;
018. import javax.swing.JButton;
019. import javax.swing.JCheckBox;
020. import javax.swing.JComboBox;
021. import javax.swing.JFrame;
022. import javax.swing.JLabel;
023. import javax.swing.JList;
024. import javax.swing.JMenuItem;
025. import javax.swing.JPanel;
026. import javax.swing.JPasswordField;
027. import javax.swing.JProgressBar;
028. import javax.swing.JRadioButton;
029. import javax.swing.JScrollPane;
030. import javax.swing.JSplitPane;
031. import javax.swing.JTabbedPane;
032. import javax.swing.JTable;
033. import javax.swing.JTextArea;
034. import javax.swing.JTextField;
035. import javax.swing.JTextPane;
036. import javax.swing.JToggleButton;
037. import javax.swing.JToolBar;
038. import javax.swing.KeyStroke;
039. import javax.swing.event.ChangeListener;
040. import javax.swing.event.ListSelectionListener;
041. import javax.swing.table.AbstractTableModel;
042. public class UIFactory {
043. private static UIFactory instance;//当前工厂的一个实例
044. //私有的构造器
045. private UIFactory(){
046. }
047. //获得当前工厂的实例
048. public static UIFactory getInstance(){
049. if(instance==null){
050. synchronized("key1"){
051. instance=new UIFactory();
052. }
053. }
054. return instance;
055. }
056. //产生一个标签
057. public JLabel createJLabel(String text){
058. JLabel l=new JLabel(text);
059. return l;
060. }
061. //产生一个带图标的标签
062. public JLabel createJLabel(String text,Icon icon){
063. JLabel l=new JLabel(text);
064. l.setIcon(icon);
065. return l;
066. }
067. //专门产生按钮，而且添加快捷键      文本显示  命令  按键   辅助键  触发范围  监听器  是否显焦
068. 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){
069. JButton button=new JButton(text,icon);
070. //这里默认会是alt+'mn'
071. button.setMnemonic(mn);//设置辅助键
072. button.setActionCommand(command);//设置动作
073. button.addActionListener(al);
074. KeyStroke stroke=KeyStroke.getKeyStroke(key\_keyEvent,input\_inputEvent,isFocus);
075. button.registerKeyboardAction(al,command,stroke,when\_invoke\_jComponent);
076. button.setFocusable(isFocus);
077. return button;
078. }
079. //专门产生按钮                     文本显示  命令  监听器  是否显焦
080. public JButton createJButton(String text,String command,ImageIcon icon,ActionListener al,boolean isFocus){
081. JButton button=new JButton(text,icon);
082. button.setActionCommand(command);//设置动作
083. button.addActionListener(al);//添加事件监听
084. button.setFocusable(isFocus);
085. return button;
086. }
087. //专门产生按钮，而且添加快捷键 文本显示  命令  按键   辅助键  触发范围  监听器  是否显焦
088. 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){
089. JButton button=new JButton(text,icon);//指定按钮的  文本和图片
090. //这里默认会是alt+'mn'  a
091. button.setMnemonic(mn);//设置辅助键
092. button.setActionCommand(command);//设置动作  【添加】
093. button.setPreferredSize(new Dimension(width,height));//指定按钮的大小
094. button.addActionListener(al);
095. KeyStroke stroke=KeyStroke.getKeyStroke(key\_keyEvent,input\_inputEvent,isFocus);
096. button.registerKeyboardAction(al,command,stroke,when\_invoke\_jComponent);
097. button.setFocusable(isFocus);
098. return button;
099. }
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&lt;Object&gt; 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. }