- 浏览: 120127 次
- 性别:
- 来自: 佛山
文章分类
最新评论
-
zgw06629:
多谢提供的安装包
MyEclipse安装资源文件支持中文插件编辑属性 -
lmzpp:
学习了,谢谢
MyEclipse安装资源文件支持中文插件编辑属性 -
nba:
不错, 谢谢了
MyEclipse安装资源文件支持中文插件编辑属性 -
CMShmily:
MyEclipse安装资源文件支持中文插件编辑属性
实例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();
}
}
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();
}
}
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();
}
}
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();
}
}
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();
}
}
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();
}
}
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();
}
}
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();
}
}
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();
}
}
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);
}
}
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;
}
}
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;
}
}
发表评论
-
JAVA范例 十九) 多媒体与图像处理 (二)
2011-09-30 19:27 1584图像处理 实例354 在计算机内存中创建一个图像 i ... -
JAVA范例 十九) 多媒体与图像处理
2011-09-30 18:40 1422a多媒体 实例349 测试音频播放器 import j ... -
JAVA范例 - Applet小应用程序
2011-09-30 18:27 3224Applet小应用程序 实例337 不断变大的文字 ... -
JAVA范例 十七)界面-Swing(一)
2011-09-30 17:51 2091实例306 JFrame框架的应用 import j ... -
JAVA范例 十六)数据库技术
2011-07-21 20:16 1575数据库技术 实例293 加载JDBC驱动程序 ... -
JAVA范例 十五)网络编程
2011-07-21 17:38 136315.1 IP地址 实例270 获 ... -
JAVA范例 十四)泛型
2011-07-21 17:30 1135第14章 泛型 14 ... -
JAVA范例 十三)多线程编程(3)
2011-07-21 17:12 1534线程应用实例 实例244 下雪的村庄 ... -
JAVA范例 十三)多线程编程(2)
2011-07-21 17:06 115713.3 线程的优先级 实例238 排座位(线程优 ... -
JAVA范例 十三)多线程编程(1)
2011-07-21 16:07 1701第13章 多线程编程 13.1 多线程的五种基本状态 ... -
JAVA范例 十二)Java高级开发技术
2011-07-21 13:55 137812.1 Set 实 ... -
JAVA范例 十一)JAVA常用类
2011-07-21 13:34 133411.1 数学Math类 实例186 求圆周率∏值 ... -
JAVA范例 十) 内部类与接口
2011-07-21 12:30 988内部类与接口 10.1 成员内部类 ... -
JAVA范例 九)面向对象---面向对象的四大特征
2011-07-21 11:50 16949.1 抽象 实例 ... -
JAVA范例 八)面向对象---面向对象的设计模式、垃圾回收
2011-07-21 11:43 9488.3 面向对象的 ... -
JAVA范例 八)面向对象---类、成员变量和方法
2011-07-21 11:30 1689类 实例148 简单的通讯录类 p ... -
JAVA范例 七)输入/输出流---字符流
2011-07-21 02:40 2151字符流 实例140 按顺序创建文件 ... -
JAVA范例 七)输入/输出流---字节流
2011-07-21 02:28 19617.2 字节流 实例123 复制指定目录下的文件 ... -
JAVA范例 七)输入/输出流---文件和目录
2011-07-21 02:16 1698文件和目录 实例116 ... -
JAVA范例 六)字符串---StringBuffer
2011-07-21 02:12 1401字符串缓存类StringBuffer ...
相关推荐
Java 3DMenu 界面源码,有人说用到游戏中不错,其实平时我信编写Java应用程序时候也能用到吧,不一定非要局限于游戏吧,RES、SRC资源都有,都在压缩包内。 Java zip压缩包查看程序源码 1个目标文件 摘要:Java源码...
《Java范例开发大全》共22章,内容涉及Java开发环境的搭建、Java基础类型与运算符、条件控制语句、异常处理、数组、字符串、输入输出流、面向对象及其四大特征、内部类与接口、Java常用类、集合、多线程编程、Java...
学习java Swing 的范例,是学习java图形使用界面的入门级知识,代码比较简单
《Java Swing(第二版)》涵盖了Java 2 SDK 1.3和1.4的所有功能,本书不但是一本参考书,而且是名副其实的指导书,是一本开发人员为开发人员编写的书,它收罗了众多不同难易程度的实用范例程序,几乎覆盖了Swing的...
第1篇 Java编程基础 第1章 Java开发环境的搭建(教学视频:9分钟) 2 1.1 理解Java 2 1.2 搭建Java所需环境 3 1.2.1 下载JDK 3 1.2.2 安装JDK 4 1.2.3 配置环境 5 1.2.4 测试JDK配置是否成功 7 实例1 开发第一个Java...
星际争霸的JAVA版所有源代码。基于Swing的界面控制编程,以及一些用户交互控制,鼠标拖放,屏幕滚动,游戏角色创建与控制等方面的应用,对学习JAVA编程是绝佳的参考范例。
第1篇 Java编程基础 第1章 Java开发环境的搭建(教学视频:9分钟) 2 1.1 理解Java 2 1.2 搭建Java所需环境 3 1.2.1 下载JDK 3 1.2.2 安装JDK 4 1.2.3 配置环境 5 1.2.4 测试JDK配置...
第1篇 Java编程基础 第1章 Java开发环境的搭建(教学视频:9分钟) 2 1.1 理解Java 2 1.2 搭建Java所需环境 3 1.2.1 下载JDK 3 1.2.2 安装JDK 4 1.2.3 配置环境 5 1.2.4 测试JDK配置是否成功 7 实例1 开发第一个Java...
Java2范例入门与提高 《Java2范例入门与提高》 一、使用说明 本书所附光盘的内容包含了开发实例的所有程序源码,所有程序源码都是在Java2上编译通过的。要运行本光盘上的实例,用户需要安装Java编译环境,请将光盘...
第1篇 Java编程基础 第1章 Java开发环境的搭建(教学视频:9分钟) 2 1.1 理解Java 2 1.2 搭建Java所需环境 3 1.2.1 下载JDK 3 1.2.2 安装JDK 4 1.2.3 配置环境 5 1.2.4 测试JDK配置是否成功 7...
很经典的一个java实例,包括了swing界面设计,java访问和操作数据库的各种方法,是学习java的经典范例!
Java范例开发大全(全书源程序),目录如下: 第1篇 Java编程基础 第1章 Java开发环境的搭建(教学视频:9分钟) 2 1.1 理解Java 2 1.2 搭建Java所需环境 3 1.2.1 下载JDK 3 1.2.2 安装JDK 4 1.2.3 配置环境...
《Java2范例入门与提高》的内容包含了开发实例的所有程序源码,所有程序源码都是在Java2上编译通过的。要运行本光盘上的实例,用户需要安装Java编译环境,并去掉其只读属性,除非特殊说明,一般经编译即可直接运行。...
第6章 java图形用户界面 6.1 HelloWorldSwing 6.2 按钮计数器 6.3 Swing按钮 6.4 温度转换器 6.5 HtmlDemo 6.6 投票器 6.7 TextSamplerDemo 6.8 Swing菜单 6.9 文件对话框 6.10 月相 6.11 单位转换器 6.12 Swing动画...
Java 3DMenu 界面源码 5个目标文件 内容索引:Java源码,窗体界面,3DMenu Java 3DMenu 界面源码,有人说用到游戏中不错,其实平时我信编写Java应用程序时候也能用到吧,不一定非要局限于游戏吧,RES、SRC资源都有,都...
Java 3DMenu 界面源码,有人说用到游戏中不错,其实平时我信编写Java应用程序时候也能用到吧,不一定非要局限于游戏吧,RES、SRC资源都有,都在压缩包内。 Java zip压缩包查看程序源码 1个目标文件 摘要:Java源码...
Java 3DMenu 界面源码,有人说用到游戏中不错,其实平时我信编写Java应用程序时候也能用到吧,不一定非要局限于游戏吧,RES、SRC资源都有,都在压缩包内。 Java zip压缩包查看程序源码 1个目标文件 摘要:Java源码...
Java二进制IO类与文件复制操作实例,好像是一本书的例子,源代码有的是独立运行的,与同目录下的其它代码文件互不联系,这些代码面向初级、中级Java程序员。 Java访问权限控制源代码 1个目标文件 摘要:Java源码...
Java二进制IO类与文件复制操作实例,好像是一本书的例子,源代码有的是独立运行的,与同目录下的其它代码文件互不联系,这些代码面向初级、中级Java程序员。 Java访问权限控制源代码 1个目标文件 摘要:Java源码...