1. 带标题的边框:
操作:panel—>properties->border->TitledBorder->Title
2. JTree的使用
首先,选择JTree,然后在代码中自己构造一棵树,
参考代码:
public JTree getJtree1() { //构造一颗树
Object[] league = { "nl ", "al " };
Vector nlV = new Vector() {
public String toString() {
return "National League ";
}
};
nlV.addElement("Braves ");
nlV.addElement("Mets ");
nlV.addElement("Cardinals ");
nlV.addElement("Rockies ");
Vector alV = new Vector() {
public String toString() {
return "American League ";
}
};
alV.addElement("Rangers ");
alV.addElement("Twins ");
alV.addElement("A 's ");
alV.addElement("White Sox ");
league[0] = nlV;
league[1] = alV;
JTree tree1 = new JTree(league);
tree1.setRootVisible(true);
tree1.expandRow(0);
return tree1;
}
然后讲代码中的jTree1 = new javax.swing.JTree();
换为 jTree1=this.getJtree1();
3. 如何让JTable的数据奇偶行颜色显示不同 数据居中
参考代码:
(1) 奇偶行颜色显示不同
package cn.edu.book.util;
import java.awt.Color;
import java.awt.Component;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
public class MyUtil {
public static void makeFace(JTable table) {
try {
DefaultTableCellRenderer tcr = new DefaultTableCellRenderer() {
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
if (row % 2 == 0)
setBackground(Color.white); // 设置奇数行底色
else if (row % 2 == 1)
setBackground(new Color(206, 231, 255)); // 设置偶数行底色
return super.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row, column);
}
};
for (int i = 0; i < table.getColumnCount(); i++) {
table.getColumn(table.getColumnName(i)).setCellRenderer(tcr);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
在上面的程序中没有给出JTable的数据.
因此当你使用的时候只需要在JTable中加入这样一句话!
MyUtil.makeFace(你的JTable名);
例如:if (jTable == null) {
jTable = new JTable(myModel);
jTable.setRowSelectionAllowed(true);//设置JTable可被选择
jTable .setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
//设置JTable为单行选择
jTable.getTableHeader().setBackground(new Color(206,231,255));//设置JTable表头颜色
jTable.getTableHeader().setReorderingAllowed(false);//设置JTable每个字段的顺序不可以改变
jTable.getTableHeader().setResizingAllowed(false); //设置JTable每个表头的大小不可以改变
MyUtil.makeFace(jTable);//设置JTable 颜色
}
(2) 数据居中
DefaultTableCellRenderer r = new DefaultTableCellRenderer();
r.setHorizontalAlignment(JLabel.CENTER);
table.setDefaultRenderer(Object.class, r);
table 为你JTABLE 的实例对象