JAVA范例 十七)界面-Swing(二)

 


实例326 BorderLayout版面布局

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class BorderLayoutDemo {
public BorderLayoutDemo() {
JFrame jf = new JFrame();
Container contentPane = jf.getContentPane();
contentPane.setLayout( new BorderLayout());
contentPane.add( new JButton( "EAST"), BorderLayout.EAST); // 将按钮放到东侧
contentPane.add( new JButton( "WEST"), BorderLayout.WEST); // 将按钮放到西侧
contentPane.add( new JButton( "SOUTH"), BorderLayout.SOUTH); // 将按钮放到南侧
contentPane.add( new JButton( "NORTH"), BorderLayout.NORTH); // 将按钮放到北侧
contentPane.add( new JLabel( "CENTER", JLabel.CENTER),
BorderLayout.CENTER); // 将按钮放到中部
jf.setTitle( "BorderLayout版面布局"); // 设置标题
jf.pack();
jf.setVisible(true);
/*
* 对一个窗口进行关闭操作,如果没有写这一段,即使你已经关闭窗口了,但程序并不会终止。
*/

jf.addWindowListener( new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit( 0);
}
});
}
public static void main(String[] args) {
BorderLayoutDemo b = new BorderLayoutDemo();
}
}


实例327 FlowLayout布局管理

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;

public class FlowLayoutDemo {
public FlowLayoutDemo() {
JFrame f = new JFrame();
Container contentPane = f.getContentPane();
contentPane.setLayout( new FlowLayout()); //创建一个布局管理器
contentPane.add( new JButton( "第一个")); //设置第一个按钮
contentPane.add( new JButton( "第二个"));
contentPane.add( new JButton( "第三个"));
contentPane.add( new JButton( "第四个"));
contentPane.add( new JButton( "第五个"));
contentPane.add( new JButton( "第六个"));
f.setTitle( "FlowLayout");
// f.pack();//必须将f.pach()去掉,否则setSize功能将没有作用
f.setSize( 400, 220);
f.setVisible(true); //将框架设置为可见状态
f.addWindowListener( new WindowAdapter() { //创建一个监听
public void windowClosing(WindowEvent e) {
System.exit( 0);
}
});
}
public static void main(String[] args) {
FlowLayoutDemo b = new FlowLayoutDemo();
}
}



实例328 应用GridLayout设计版面

import java.awt.Button;
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;

import javax.swing.JPanel;


public class GridLayoutDemo implements ActionListener {
JPanel p1, p2, p3, p4;
int i = 1;
JFrame f;
public GridLayoutDemo() {
f = new JFrame(); // 创建一个JFrame的对象
Container contentPane = f.getContentPane();
contentPane.setLayout( new GridLayout( 2, 1));
p1 = new JPanel();
Button b = new Button( "换一个");
b.addActionListener( this); // 当按下"换一个"按钮时,进行事件监听,将会有系统操作产生。
p1.add(b); // 处理操作在52-64行.
contentPane.add(p1);
p2 = new JPanel();
p2.setLayout( new FlowLayout());
p2.add( new JButton( "first")); // 将第一个按钮添加到对象p2中
p2.add( new JButton( "second")); // 将第二个按钮添加到对象p2中
p2.add( new JButton( "third")); // 将第三个按钮添加到对象p2中
p3 = new JPanel();
p3.setLayout( new GridLayout( 3, 1));
p3.add( new JButton( "fourth"));
p3.add( new JButton( "fifth"));
p3.add( new JButton( "This is the last button"));
p4 = new JPanel();
p4.setLayout( new GridLayout());
p4.add( "one", p2);
p4.add( "two", p3);

contentPane.add(p4);
f.setTitle( "CardLayout");
f.pack();
f.setVisible(true); // 将框架设置为可见状态
f.addWindowListener( new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit( 0);
}
});
}
public void actionPerformed(ActionEvent event) {
switch (i) {
case 1 :
break;
case 2 :
break;
}
i ++;
if (i == 3)
i = 1;
f.validate();
}
public static void main(String[] args) {
new GridLayoutDemo();
}
}



实例329 如何使用BoxLayout布局管理器

import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;

public class BoxLayout {
public BoxLayout() {
JFrame f = new JFrame();
Container contentPane = f.getContentPane();
Box baseBox = Box.createHorizontalBox();
contentPane.add(baseBox);
baseBox.add( new JButton( "A")); //定义一个"A"按钮
baseBox.add( new JButton( "B")); //定义一个"B"按钮
f.setTitle( "BoxLayout");
f.setSize( new Dimension( 200, 50)); //设置窗口的大小
f.setVisible(true);
f.addWindowListener( new WindowAdapter() { //对窗口进行监听
public void windowClosing(WindowEvent e) {
System.exit( 0);
}
});
}
public static void main(String[] args) {
BoxLayout b = new BoxLayout();
}
}



实例330 使用ActionEvent监听组件

import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;



public class EventTest1 extends WindowAdapter implements ActionListener {
JButton b1 = null;
JButton b2 = null;
public EventTest1() {
JFrame f = new JFrame( "EventTest1");
Container contentPane = f.getContentPane();
contentPane.setLayout( new GridLayout( 1, 2));
b1 = new JButton( "点我有声音喔");
b2 = new JButton( "点我可开新窗口");
b1.addActionListener( this);
b2.addActionListener( this);
contentPane.add(b1);
contentPane.add(b2);
f.pack();
f.show();
f.addWindowListener( this);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1) // 利用getSource判断哪个按钮被按下了。
Toolkit.getDefaultToolkit().beep();
if (e.getSource() == b2) {
JFrame jf = new JFrame( "新窗口");
jf.setSize( 200, 200);
jf.show();
}
}
public void windowClosing(WindowEvent e) {
System.exit( 0);
}
public static void main(String args[]) {
new EventTest1();
}
}



实例331 使用WindowAdapter实现鼠标事件

import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class EventTest2 extends WindowAdapter implements MouseListener {
JFrame f = null;
JButton b1 = null;
JLabel label = null;
public EventTest2() {
f = new JFrame( "EventTest2");
Container contentPane = f.getContentPane();
contentPane.setLayout( new GridLayout( 2, 1));
b1 = new JButton( "按钮");
label = new JLabel( "起始状态,还没有鼠标事件", JLabel.CENTER);
b1.addMouseListener( this);
contentPane.add(label);
contentPane.add(b1);
f.pack();
f.show();
f.addWindowListener( this);
}
public void mousePressed(MouseEvent e) {
label.setText( "你已经压下鼠标按钮");
}
public void mouseReleased(MouseEvent e) {
label.setText( "你已经放开鼠标按钮");
}
public void mouseEntered(MouseEvent e) {
label.setText( "鼠标光标进入按钮");
}
public void mouseExited(MouseEvent e) {
label.setText( "鼠标光标离开按钮");
}
public void mouseClicked(MouseEvent e) {
label.setText( "你已经按下按钮");
}
public void windowClosing(WindowEvent e) {
System.exit( 0);
}
public static void main(String[] args) {
new EventTest2();
}
}


实例332 MouseMotionListener监听鼠标

import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;

public class EventTest3 extends JFrame implements MouseListener,
MouseMotionListener {
int flag; // flag=1代表Mouse Moved,flag=2代表Mouse Dragged
int x = 0;
int y = 0;
int startx, starty, endx, endy; // 起始坐标与终点坐标
public EventTest3() {
Container contentPane = getContentPane();
contentPane.addMouseListener( this);
contentPane.addMouseMotionListener( this);
setSize( 300, 300);
show();
addWindowListener( new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit( 0);
}
});
}
//由mousePressed(),mouseReleased()取得示拖曳的开始与结束坐标
public void mousePressed(MouseEvent e) {
startx = e.getX();
starty = e.getY();
}
public void mouseReleased(MouseEvent e) {
endx = e.getX();
endy = e.getY();
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
// mouseMoved(),mouseDragged()取得鼠标移动的每一个坐标,并调用repaint()方法
public void mouseMoved(MouseEvent e) {
flag = 1;
x = e.getX();
y = e.getY();
repaint();
}
public void mouseDragged(MouseEvent e) {
flag = 2;
x = e.getX();
y = e.getY();
repaint();
}
public void update(Graphics g) {
g.setColor( this.getBackground());
g.fillRect( 0, 0, getWidth(), getHeight());
paint(g);
}
public void paint(Graphics g) {
g.setColor(Color.black);
if (flag == 1) {
g.drawString( "鼠标坐标:(" + x + "," + y + ")", 10, 50);
g.drawLine(startx, starty, endx, endy);
}
if (flag == 2) {
g.drawString( "拖曳鼠标价坐标:(" + x + "," + y + ")", 10, 50);
g.drawLine(startx, starty, x, y);
}
}
public static void main(String[] args) {
new EventTest3();
}
}



实例333 使用KeyListener监听键盘

import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class EventTest4 extends KeyAdapter implements ActionListener {
JFrame f = null;
JLabel label = null;
JTextField tField = null;
String keyString = "";
public EventTest4() {
f = new JFrame( "键盘监听"); // 为这个JFrame设置一个标题
Container contentPane = f.getContentPane();
contentPane.setLayout( new GridLayout( 3, 1));
label = new JLabel(); // 创建一个标签对象
tField = new JTextField();
tField.requestFocus();
tField.addKeyListener( this);
JButton b = new JButton( "清除"); // 创建一个按钮
b.addActionListener( this);
contentPane.add(label);
contentPane.add(tField);
contentPane.add(b);
f.pack();
f.show();
f.addWindowListener( new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit( 0);
}
});
}
public void actionPerformed(ActionEvent e) {
keyString = "";
label.setText( "");
tField.setText( "");
tField.requestFocus();
}
//输入字母"O"之后,会产生新窗口
public void keyTyped(KeyEvent e) {
char c = e.getKeyChar(); /* 注意getKeyChar()的用法 */
if (c == 'o') {
JFrame newF = new JFrame( "新窗口");
newF.setSize( 200, 200);
newF.show();
}
keyString = keyString + c;
label.setText(keyString);
}
public static void main(String[] args) {
new EventTest4();
}
}



实例334 计算器

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class Calculator extends WindowAdapter implements ActionListener {
JFrame list;
JTextField show;
JButton bc, c, ce, ab, jia, jian, cheng, chu, equ, point, sqrt, ds, bfh,
zf;
// 按钮 退格,清空,复位,关于,加,减,乘,除,等号,小数点,2次方根,倒数,百分号,正负号
JButton b[] = new JButton[ 10]; // 按钮数组,数字键0~9
double sum = 0, getValue;
int i = 0, j = 0, p = 0, l, action;
JDialog about;
final int slength = 30; // 设置结果显示有效长度
public void disp() {
list = new JFrame( "简易计算器");
list.setSize( 360, 230);
list.setLocation( 380, 260);
list.setBackground(Color.LIGHT_GRAY);
list.setLayout( new FlowLayout(FlowLayout.CENTER));
list.setResizable(false);
show = new JTextField( 31);
show.setText( "0");
show.setHorizontalAlignment(SwingConstants.RIGHT);
show.setEditable(false);
list.add(show);
Panel dispTop = new Panel();
list.add(dispTop);
dispTop.setLayout( new GridLayout( 1, 4, 3, 3));
bc = new JButton( " Back ");
bc.setForeground(Color.BLUE);
dispTop.add(bc);
ce = new JButton( " CE ");
ce.setForeground(Color.BLUE);
dispTop.add(ce);
c = new JButton( " C ");
c.setForeground(Color.BLUE);
dispTop.add(c);
ab = new JButton( " About ");
ab.setForeground(Color.BLUE);
dispTop.add(ab);
about = new JDialog(list, "关于计算器", true);
Label ct = new Label( "http://blog.csdn.net/wibnmo", 1);
ct.setForeground(Color.RED);
about.add(ct, "Center");
about.setSize( 200, 100);
about.setLocation( 500, 300);
about.addWindowListener( this);
Panel dispMain = new Panel();
list.add(dispMain);
dispMain.setLayout( new GridLayout( 1, 2, 10, 10));
Panel dispLeft = new Panel();
dispMain.add(dispLeft);
dispLeft.setLayout( new GridLayout( 4, 3, 3, 3));
Panel dispRight = new Panel();
dispMain.add(dispRight);
dispRight.setLayout( new GridLayout( 4, 2, 3, 3));
for (l = 9; l > = 0; l --) {
b[l] = new JButton(String.valueOf(l));
dispLeft.add(b[l]);
b[l].addActionListener( this);
}
jia = new JButton( "+");
jia.setForeground(Color.RED);
jian = new JButton( "-");
jian.setForeground(Color.RED);
cheng = new JButton( "*");
cheng.setForeground(Color.RED);
chu = new JButton( "/");
chu.setForeground(Color.RED);
equ = new JButton( "=");
equ.setForeground(Color.RED);
point = new JButton( ".");
zf = new JButton( " +/- ");
sqrt = new JButton( "sqrt");
bfh = new JButton( "%");
ds = new JButton( "1/x");
dispRight.add(chu);
dispRight.add(sqrt);
dispRight.add(cheng);
dispRight.add(bfh);
dispRight.add(jian);
dispRight.add(ds);
dispRight.add(jia);
dispRight.add(equ);
dispLeft.add(zf);
dispLeft.add(point);
bc.addActionListener( this);
ce.addActionListener( this);
c.addActionListener( this);
ab.addActionListener( this);
jia.addActionListener( this);
jian.addActionListener( this);
cheng.addActionListener( this);
chu.addActionListener( this);
equ.addActionListener( this);
point.addActionListener( this);
zf.addActionListener( this);
sqrt.addActionListener( this);
bfh.addActionListener( this);
ds.addActionListener( this);
list.addWindowListener( this);
list.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
getValue = Double.valueOf(show.getText()).doubleValue();
if (e.getSource() == jia) { // 加运算,可连加
if (j == 0) {
sum = getValue;
} else if (action == 1) {
sum += getValue;
}
setSum();
j ++;
p = 0;
i = 0;
action = 1;
} else if (e.getSource() == jian) { // 减运算,可连减
if (j == 0) {
sum = getValue;
} else if (action == 2) {
sum -= getValue;
}
setSum();
j ++;
p = 0;
i = 0;
action = 2;
} else if (e.getSource() == cheng) { // 乘运算,可连乘
if (j == 0) {
sum = getValue;
} else if (action == 3) {
sum *= getValue;
}
setSum();
j ++;
p = 0;
i = 0;
action = 3;
} else if (e.getSource() == chu) { // 除运算,可连除
if (j == 0)
sum = getValue;
else if (action == 4) {
sum /= getValue;
}
setSum();
j ++;
p = 0;
i = 0;
action = 4;
} else if (e.getSource() == equ) { // 等号,运算最后一个操作数
switch (action) {
case 1 :
show.setText(String.valueOf(sum += getValue));
break;
case 2 :
show.setText(String.valueOf(sum -= getValue));
break;
case 3 :
show.setText(String.valueOf(sum *= getValue));
break;
case 4 :
show.setText(String.valueOf(sum /= getValue));
break;
}
setSum();
i = 0;
j = 0;
action = 0;
} else if (e.getSource() == point) { // 小数点,只能按一个小数点
if (p == 0)
show.setText(show.getText() + e.getActionCommand());
p = 1;
} else if (e.getSource() == c || e.getSource() == ce) { // 清空与复位
i = 0;
j = 0;
p = 0;
sum = 0;
action = 0;
show.setText( "0");
} else if (e.getSource() == bc) { // 退格
String s = show.getText();
if (s.length() > 1) {
show.setText( "");
for (l = 0; l < s.length() - 1; l ++) { // 按一下,删除尾部一位
char a = s.charAt(l);
show.setText(show.getText() + a);
}
} else
show.setText( "0");
} else if (e.getSource() == ab) { // 关于
about.setVisible(true);
} else if (e.getSource() == sqrt) { // 开2次方根
sum = Math.sqrt(getValue);
setSum();
i = 0;
} else if (e.getSource() == ds) { // 求倒数
sum = 1 / getValue;
setSum();
i = 0;
} else if (e.getSource() == bfh) { // 百分号
sum = getValue / 100;
setSum();
i = 0;
} else if (e.getSource() == zf) { // 正负号切换,正号不显示
String s = show.getText();
char a = s.charAt( 0);
if (a == '-') {
show.setText( "");
for (l = 1; l < s.length(); l ++) { // 去掉负号
show.setText(show.getText() + s.charAt(l));
}
} else if (getValue != 0) { // 加上负号
show.setText( "-" + s);
}
}
for (l = 0; l < 10; l ++) { // 0~9数字键触发
if (e.getSource() == b[l]) {
if (i == 0)
show.setText( "");
String s = show.getText();
if (s.length() < slength)
show.setText(show.getText() + e.getActionCommand());
if (e.getSource() == b[ 0] && getValue == 0 && p == 0)
show.setText( "0");
if (e.getSource() != b[ 0] && getValue == 0 && p == 0)
show.setText(e.getActionCommand());
i ++; // i用来标记数字键触发的状态
}
}
}
public void setSum() { // 把计算结果显示出来
show.setText(String.valueOf(sum));
String s = show.getText();
char a = s.charAt((s.length() - 1));
char b = s.charAt((s.length() - 2));
if (a == '0' && b == '.') { // 如果是整数,则去掉后面的小数点和0
show.setText(String.valueOf(Math.round(sum)));
}
}
public void windowClosing(WindowEvent e) {
if (e.getSource() == about)
about.setVisible(false);
else if (e.getSource() == list)
System.exit( 0);
}
public static void main(String args[]) {
new Calculator().disp();
}
}



实例335 创建树菜单

import java.awt.BorderLayout;

import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;

public class JTreeDemo {
public static void main(String args[]) {
JFrame f = new JFrame( "JTreeDemo"); //创建窗体。
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane();

Box box = Box.createHorizontalBox();
JTree tree1 = new JTree(); //创建树。
tree1.putClientProperty( "JTree.lineStyle", "Angled");
JScrollPane scrollPane1 = new JScrollPane(tree1);
tree1.setAutoscrolls(true);
JTree tree2 = new JTree(); //创建树。
JScrollPane scrollPane2 = new JScrollPane(tree2);
box.add(scrollPane1, BorderLayout.WEST);
box.add(scrollPane2, BorderLayout.EAST);

f.getContentPane().add(box, BorderLayout.CENTER);
f.setSize( 300, 240);
f.setVisible(true);
}
}



实例336 在节点中显示详细信息

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellRenderer;

public class JTreeDemo1 {
public static void main(String args[]) {
JFrame f = new JFrame( "JTreeDemo1");
Book popBooks[] = { new Book( "淘宝", "聂庆亮 著", 87.00f),
new Book( "java实例", "尹继平 著", 99.00f),
new Book( "我和你", "张力 著", 61.00f) };
Book javaBooks[] = {
new Book( "Hardcore Java", "Simmons,R. 著", 626.00f),
new Book( "Core Java2, (7th Edition)", "Cay Horstmann 著",
123. 99f) };
Vector javaVector = new NamedVector( "畅销书", popBooks);
Vector htmlVector = new NamedVector( "Java书", javaBooks);
Object rootNodes[] = { javaVector, htmlVector };
Vector rootVector = new NamedVector( "Root", rootNodes);
JTree tree = new JTree(rootVector);
TreeCellRenderer renderer = new BookCellRenderer();
tree.setCellRenderer(renderer);
JScrollPane scrollPane = new JScrollPane(tree);
f.getContentPane().add(scrollPane, BorderLayout.CENTER);
f.setSize( 300, 300);
f.setVisible(true);
}
}
class Book // 存放书信息。
{
String title;
String authors;
float price;
public Book(String title, String authors, float price) {
this.title = title;
this.authors = authors;
this.price = price;
}
public String getTitle() {
return title;
}
public String getAuthors() {
return authors;
}
public float getPrice() {
return price;
}
}
class BookCellRenderer implements TreeCellRenderer // 实现TreeCellRenderer接口。
{
JLabel titleLabel;
JLabel authorsLabel;
JLabel priceLabel;
JPanel renderer;
DefaultTreeCellRenderer defaultRenderer = new DefaultTreeCellRenderer(); //创建一个DefaultTreeCellRenderer对象
Color backgroundSelectionColor;
Color backgroundNonSelectionColor;
public BookCellRenderer() // 构造方法。
{
renderer = new JPanel( new GridLayout( 0, 1));
titleLabel = new JLabel( " ");
titleLabel.setForeground(Color.black);
renderer.add(titleLabel);
authorsLabel = new JLabel( " ");
authorsLabel.setForeground(Color.black);
renderer.add(authorsLabel);
priceLabel = new JLabel( " ");
priceLabel.setHorizontalAlignment(JLabel.RIGHT);
priceLabel.setForeground(Color.red);
renderer.add(priceLabel);
renderer.setBorder(BorderFactory.createLineBorder(Color.black));
backgroundSelectionColor = defaultRenderer
.getBackgroundSelectionColor();
backgroundNonSelectionColor = defaultRenderer
.getBackgroundNonSelectionColor();
}
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean selected, boolean expanded, boolean leaf, int row,
boolean hasFocus) {
Component returnValue = null;
if ((value != null) && (value instanceof DefaultMutableTreeNode)) {
Object userObject = ((DefaultMutableTreeNode) value)
.getUserObject();
if (userObject instanceof Book) {
Book book = (Book) userObject;
titleLabel.setText(book.getTitle());
authorsLabel.setText(book.getAuthors());
priceLabel.setText( "" + book.getPrice());
if (selected) {
renderer.setBackground(backgroundSelectionColor);
}
else {
renderer.setBackground(backgroundNonSelectionColor);
}
renderer.setEnabled(tree.isEnabled());
returnValue = renderer;
}
}
if (returnValue == null) {
returnValue = defaultRenderer.getTreeCellRendererComponent(tree,
value, selected, expanded, leaf, row, hasFocus);
}
return returnValue;
}
}
class NamedVector extends Vector // 继承向量类,方便管理数据。
{
String name;
public NamedVector(String name) {
this.name = name;
}
public NamedVector(String name, Object elements[]) {
this.name = name;
for ( int i = 0, n = elements.length; i < n; i ++) {
add(elements[i]);
}
}
public String toString() {
return name;
}
}

你可能感兴趣的:(swing)