`
seara
  • 浏览: 624594 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Java SE 6之GUI:让界面更加绚丽(下)

阅读更多
本文为原创,如需转载,请注明作者和出处,谢谢!

Java SE 6之GUI:让界面更加绚丽( 上)

在上一篇中我介绍了Java SE 6GUI上的部分改进。在这篇 文章中我接着介绍另外几种新的GUI功能。这些功能是:

1. 带有排序和过滤功能的JTable

2. 增强的JTabbedPane组件

3. 增强的打印功能

4. 增强的拖放功能

一、带有排序和过滤功能的JTable

Java SE 6中除了 java.awt被更新外,javax.swing同时也有了很大的改进。在 C/S程序中我们会经常使用到 “表”。如我们可以在查询数据库后将查询结果显示在表格中。在Java中显示表格使用的是JTable类。在以前的版本中,JTable只能简单地显示数据,并没有什么附加的处理功能,而在Java SE 6中的JTable增加了排序和过滤功能。用户可以单击列头进行排序,也可以根据某一列来过滤表中的数 据。

为了使JTable可以对数据进行,必须将< /span>RowSorter类和 JTable进行关联。 RowSorter是一个抽象类,它负责将 JTable中的数据映射成可排序的数据。在真正使用时,我们将直接使用 RowSorter的子类TableRowSorter。下面的代码显示了如何将 TableRowSorter类和JTable相关联。


<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->TableModelmodel=newDefaultTableModel(rows,columns);
JTabletable
=newJTable(model);
RowSortersorter
=newTableRowSorter(model);
table.setRowSorter(sorter);

上面代码首先建立一个TableModel,然后将这个 TableModel的实例同时传递给了JTable RowSorter。下面是一个使用 JTable排序的简单的例子。


<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->importjavax.swing.*;
importjavax.swing.table.*;
importjava.awt.*;

publicclassTestSortedTable
{
publicstaticvoidmain(Stringargs[])
{
JFrameframe
= newJFrame("JTable的排序测试");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//表格中显示的数据
Objectrows[][]=
{
{
" 王明 ","中国",44},
{
" 姚明 ","中国",25},
{
" 赵子龙 ","西蜀",1234},
{
" 曹操 ","北魏",2112},
{
" BillGates ","美国",45},
{
" Mike","< /span>英国",33}};
Stringcolumns[]
=
{
" 姓名 ","国籍","年龄"};
TableModelmodel
=newDefaultTableModel(rows,columns);
JTabletable
=newJTable(model);
RowSorter
<TableModel>sorter=newTableRowSorter<TableModel>(model);
table.setRowSorter(sorter);
JScrollPanepane
=newJScrollPane(table);
frame.add(pane,BorderLayout.CENTER);
frame.setSize(
300,150);
frame.setVisible(
true);
}
}

1和图2 分别是按“姓名”进行升序和降序排列的显示结果。



1 按“姓名”升序显示




2 按“姓名”降序显示

3显示的是按“年龄”进行降序排列。但我们发现一个奇怪的问题 ,就是“年龄”字段并不是按数值类型进行排序的,而是按字符类型进行排序的。



3 按年龄降序显示

出现这种情况是因为在默认情况下DefaultTableModal的 列是Object类型。而要想使JTable按数值进行排序,必须要覆盖 DefaultTableModalgetColumnClass方法。


<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->TableModelmodel=newDefaultTableModel(rows,columns)
{
publicClassgetColumnClass(intcolumn)
{
ClassreturnValue;
if((column>=0)&&(column<getColumnCount()))
{
returnValue
=getValueAt(0,column).getClass();
}
else
{
returnValue
=Object.class;
}
returnreturnValue;
}
};

4显示了按“年龄”进行排序的界面,看看,是不是按数值进行排序了。



4 按数值类型进行排序

下面让我们来看看来何使用JTable进行过滤。我们可以 通过convertRowIndexToModel方法进行过滤。下面的代码加在一个按钮中添加事件代码调用JTable的过滤功能。

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->button.addActionListener(newActionListener()
{
publicvoidactionPerformed(ActionEvente)
{
Stringtext
=filterText.getText();
if(text.length()==0)
{
sorter.setRowFilter(
null);
}
else
{
sorter.setRowFilter (RowFilter.regexFilter(text));
}
}
});

上面的代码并没有调用convertRowIndextoModel()方法,如果调用它,你就可以在 表中进行相应的操作。

JTable中通过抽象类 RowFilter类对行进行过滤。和排序不同,你可以不建立它们的子类,而使用这个抽象类的6个静态方法。

1. andFilter

2. dateFilter(RowFilter.ComparisonType type, Date date, int... indices)

3. notFilter(RowFilter<M,I> filter)

4. numberFilter(RowFilter.ComparisonType type, Number number, int... indices)

5. orFilter

6. regexFilter(String regex, int... indices)

其中andFilter()orFilter()以及 notFilter()方法的功能是将当前的过滤条件和其它的过滤条件进行组合。如在同时比较日期和数 值时需要将日期过滤和数值过滤进行组合。这些组合是非常简单的。

RowFilter的类型比较允许你进行4种关系的比较,等于 、不等于、大于或小于。我们可以通过指定某一列进行过滤,也可以对所有的列进行过滤。这其中最为有趣的也许是正则表达式过滤(regular expression filter,或简称为regex filter)。使用这个过滤器可以对表中数据进行更高级的过滤。下面是实现一个简 单过滤器的代码。

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->importjavax.swing.*;
importjavax.swing.table.*;
importjava.awt.*;
importjava.awt.event.*;

publicclassTestFilter
{
publicstaticvoidmain(Stringargs[])
{
JFrameframe
= newJFrame("JTable的过滤测试");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Objectrows[][]
=
{
{
" 王明 ","中国",44},
{
" 姚明 ","中国",25},
{
" 赵子龙 ","西蜀",1234},
{
" 曹操 ","北魏",2112},
{
" BillGates ","美国",45},
{
" Mike","< /span>英国",33}};
Stringcolumns[]
=
{
" 姓名 ","国籍","年龄"};
TableModelmodel
=newDefaultTableModel(rows,columns)
{
publicClassgetColumnClass(intcolumn)
{
ClassreturnValue;
if((column>=0)&&(column<getColumnCount()))
{
returnValue
=getValueAt(0,column).getClass();
}
else
{
returnValue
=Object.class;
}
returnreturnValue;
}
};
finalJTabletable=newJTable(model);
finalTableRowSorter<TableModel>sorter=newTableRowSorter<TableModel>(
model);
table.setRowSorter(sorter);
JScrollPanepane
=newJScrollPane(table);
frame.add(pane,BorderLayout.CENTER);
JPanelpanel
=newJPanel(newBorderLayout());
JLabellabel
= newJLabel("过滤");
panel.add(label,BorderLayout.WEST);
finalJTextFieldfilterText=newJTextField("");
panel.add(filterText,BorderLayout.CENTER);
frame.add(panel,BorderLayout.NORTH);
JButtonbutton
= newJButton("过滤");
button.addActionListener(
newActionListener()
{
publicvoidactionPerformed(ActionEvente)
{
Stringtext
=filterText.getText();
if(text.length()==0)
{
sorter.setRowFilter(
null);
}
else
{
sorter.setRowFilter (RowFilter.regexFilter(text));
}
}
});
frame.add(button,BorderLayout.SOUTH);
frame.setSize(
300,250);
frame.setVisible(
true);
}
}

5是上面程序的运行界面。



二、增强的JTabbedPane组件

JTabbedPane组件为我们提供了一种非常好的方法在窗体上显示很多的控件。我们可以将不同类别的控 件放到不同的Tab页上,然后通过需要点击相应的Tab页。在传统的 Tab页上只能防止文本的图标。而在 Java SE 6中使我们可以直接将控件放到 Tab上。我们可以通过 setTabComponentAt方法将控件放到Tab上。这个方法有两个参数,一个是Tab的索引,另一个是要放置的对象。


<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->JTabbedPanepane=newJTabbedPane();
pane.setTabComponentAt(
1,component);

JTabbedPane控件中有3个常用的方法,setTabComponentAt(int index, Component comp), getTabComponentAt (int index)indexOfTabComponent(Component)。最后一个方法将替换Tab上的控件。下面的代码是一个关于JTabbedPane控件的演示。

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->importjavax.swing.*;
importjavax.swing.table.*;
importjava.awt.*;
importjava.awt.event.*;

publicclassTestTabbedPane
{
staticvoidaddIt(JTabbedPanetabbedPane,Stringtext)
{
JLabellabel
=newJLabel(text);
JButtonbutton
=newJButton(text);
JPanelpanel
=newJPanel();
panel.add(label);
panel.add(button);
tabbedPane.addTab(text,panel);
if(text.equals ("tab4"))
tabbedPane.setTabComponentAt(tabbedPane.getTabCount()
-1,
newJTextField("插入了文本控件" ));
else
tabbedPane.setTabComponentAt(tabbedPane.getTabCount()
- 1,
button);
}
publicstaticvoidmain(Stringargs[])
{
JFramef
=newJFrame(" JTabbedPane演示");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTabbedPanetabbedPane
=newJTabbedPane();
addIt(tabbedPane,
"tab1");
addIt(tabbedPane,
"tab2");
addIt(tabbedPane,
"tab3");
addIt(tabbedPane,
"tab4");
addIt(tabbedPane,
"tab5");
f.add(tabbedPane,BorderLayout.CENTER);
f.setSize(
400,200);
f.setVisible(
true);
}
}


6是显示界面,其中在Tab4上插入了一个文本控件,在Tab1Tab5上各插入了一个 按钮控件。




6 JTabbedPane演示

三、增强的打印功能

自从Java SE 5开始, Sun就对控件的打印功能进行了加强。如 JTextFieldJTextArea等。在 Java SE 6Sun为打印增加了分页功能。我们只需要调用 JtextField JTextAreaprint方法就可以调用打印对话框。下面是一段测试代 码。

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;
importjava.awt.print.*;

publicclassTextPrint
{
publicstaticvoidmain(finalStringargs[])
{
JFrameframe
= newJFrame("打印测试");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
finalJTextAreatextArea=newJTextArea();
JScrollPanepane
=newJScrollPane(textArea);
frame.add(pane,BorderLayout.CENTER);
textArea.setText(
"打印 内容\r\n可以分页! ");
JButtonbutton
= newJButton("打印");
frame.add(button,BorderLayout.SOUTH);
ActionListenerlistener
=newActionListener()
{
publicvoidactionPerformed(ActionEvente)
{
try
{
textArea.print();
}
catch(PrinterExceptionpe)
{
System.err.println(
"打印失败");
}
}
};
button.addActionListener(listener);
frame.setSize(
250,150);
frame.setVisible(
true);
}
}

7和图8分别是打印对话框和设置对话框, 点击“打印“按钮后弹出如图8的对话框。


7 打印界面

8 设置对话框

虽然提供了打印设置对话框,但我们并无法设置如页眉(角)等信息,幸运的是print的一个重载为我们提供了这个功能。下面是这个方法的参数。

public boolean print(MessageFormat headerFormat,

MessageFormat footerFormat,

boolean showPrintDialog,

PrintService service,

PrintRequestAttributeSet attributes,

boolean interactive)


四、增强的拖放功能

Java SE 6中的拖放功能得到了增强,这主要表现在两个 方面。

1. 可以定制拖放模式。

2. 可以在拖放的过程中加入其它的辅助信息。

首先需要通过JListJTable等控件的 setDropMode()方法来设置一个拖动模式。所有的控件都可以使用 USER_SELECTION模式。这个模式在以前的Java SE版本中就有。这也是默认的拖放模式。

JListJTableJlist都支持ON模式,这个模式允许你将对象拖到其它项的上方。而INSERT模式允许将 一个对象插入在其它项之间。而ON_OR_INSERT模式是前3种模式的组合。下面的代码将演 示一个拖动的例子。


<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->importjava.awt.*;
importjava.awt.datatransfer.*;
importjava.awt.event.*;
importjava.io.*;
importjavax.swing.*;
importjavax.swing.tree.*;

publicclassTestDrapDrop
{
publicstaticvoidmain(Stringargs[])
{
JFramef
=newJFrame(" 拖放测试" );
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPaneltop
=newJPanel(newBorderLayout());
JLabeldragLabel
=new JLabel("拖我:");
JTextFieldtext
=newJTextField();
text.setDragEnabled(
true);
top.add(dragLabel,BorderLayout.WEST);
top.add(text,BorderLayout.CENTER);
f.add(top,BorderLayout.NORTH);
finalJTreetree=newJTree();
finalDefaultTreeModelmodel=(DefaultTreeModel) tree.getModel();
tree.setTransferHandler(
newTransferHandler()
{
publicbooleancanImport (TransferHandler.TransferSupportsupport)
{
if(!support.isDataFlavorSupported (DataFlavor.stringFlavor)
||!support.isDrop())
{
returnfalse;
}
JTree.DropLocationdropLocation
=(JTree.DropLocation)suppor
.getDropLocat ion();
returndropLocation.getPath()!=null;
}

publicbooleanimportData (TransferHandler.TransferSupportsupport)
{
if(!canImport(support))
{
returnfalse;
}
JTree.DropLocationdropLocation
=(JTree.DropLocation)support
.getDropLocat ion();
TreePathpath
=dropLocation.getPath();
Transferabletransferable
=support.getTransferable();
StringtransferData;
try
{
transferData
=(String)transferable
& nbsp;.getTransferData(DataFlavor.stringFlavor);
}
catch(IOExceptione)
{
returnfalse;
}
catch(UnsupportedFlavorExceptione)
{
returnfalse;
}
intchildIndex=dropLocation.getChildIndex();
if(childIndex==-1)
{
childIndex
=model.getChildCount(path
& nbsp;.getLastPathComponent());
}
DefaultMutableTreeNodenewNode
=newDefaultMutableTreeNode(
transferData) ;
DefaultMutableTreeNodeparentNode
=(DefaultMutableTreeNode)path
.getLastPathC omponent();
model.insertNodeInto (newNode,parentNode,childIndex);
TreePathnewPath
=path.pathByAddingChild(newNode);
tree.makeVisible(newPath);
tree.scrollRectToVisible(tree.getPathBounds(newPath));
returntrue;
}
});
JScrollPanepane
=newJScrollPane(tree);
f.add(pane,BorderLayout.CENTER);
JPanelbottom
=newJPanel();
JLabelcomboLabel
=newJLabel("DropMode");
Stringoptions[]
=
{
"USE_SELECTION","ON","INSERT","ON_OR_INSERT"};
finalDropModemode[]=
{DropMode.USE_SELECTION,DropMode.ON,DropMode.INSERT,
DropMode.ON_OR_INSERT};
finalJComboBoxcombo=newJComboBox(options);
combo.addActionListener(
newActionListener()
{
publicvoidactionPerformed(ActionEvente)
{
intselectedIndex=combo.getSelectedIndex();
tree.setDropMode(mode[selectedIndex]);
}
});
bottom.add(comboLabel);
bottom.add(combo);
f.add(bottom,BorderLayout.SOUTH);
f.setSize(
300,400);
f.setVisible(
true);
}
}

9为拖动程序的运行界面。在上面的文本框里输入相应的 文本,然后将其选择再拖动到下方的树中。


9 拖动界面





国内最棒的Google Android技术社区(eoeandroid),欢迎访问!

《银河系列原创教程》发布

《Java Web开发速学宝典》出版,欢迎定购

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics