- 浏览: 122778 次
- 性别:
- 来自: 佛山
文章分类
最新评论
-
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 1619图像处理 实例354 在计算机内存中创建一个图像 i ... -
JAVA范例 十九) 多媒体与图像处理
2011-09-30 18:40 1453a多媒体 实例349 测试音频播放器 import j ... -
JAVA范例 - Applet小应用程序
2011-09-30 18:27 3265Applet小应用程序 实例337 不断变大的文字 ... -
JAVA范例 十七)界面-Swing(一)
2011-09-30 17:51 2134实例306 JFrame框架的应用 import j ... -
JAVA范例 十六)数据库技术
2011-07-21 20:16 1620数据库技术 实例293 加载JDBC驱动程序 ... -
JAVA范例 十五)网络编程
2011-07-21 17:38 139915.1 IP地址 实例270 获 ... -
JAVA范例 十四)泛型
2011-07-21 17:30 1165第14章 泛型 14 ... -
JAVA范例 十三)多线程编程(3)
2011-07-21 17:12 1557线程应用实例 实例244 下雪的村庄 ... -
JAVA范例 十三)多线程编程(2)
2011-07-21 17:06 120113.3 线程的优先级 实例238 排座位(线程优 ... -
JAVA范例 十三)多线程编程(1)
2011-07-21 16:07 1733第13章 多线程编程 13.1 多线程的五种基本状态 ... -
JAVA范例 十二)Java高级开发技术
2011-07-21 13:55 141812.1 Set 实 ... -
JAVA范例 十一)JAVA常用类
2011-07-21 13:34 137711.1 数学Math类 实例186 求圆周率∏值 ... -
JAVA范例 十) 内部类与接口
2011-07-21 12:30 1038内部类与接口 10.1 成员内部类 ... -
JAVA范例 九)面向对象---面向对象的四大特征
2011-07-21 11:50 17149.1 抽象 实例 ... -
JAVA范例 八)面向对象---面向对象的设计模式、垃圾回收
2011-07-21 11:43 9728.3 面向对象的 ... -
JAVA范例 八)面向对象---类、成员变量和方法
2011-07-21 11:30 1725类 实例148 简单的通讯录类 p ... -
JAVA范例 七)输入/输出流---字符流
2011-07-21 02:40 2175字符流 实例140 按顺序创建文件 ... -
JAVA范例 七)输入/输出流---字节流
2011-07-21 02:28 19887.2 字节流 实例123 复制指定目录下的文件 ... -
JAVA范例 七)输入/输出流---文件和目录
2011-07-21 02:16 1728文件和目录 实例116 ... -
JAVA范例 六)字符串---StringBuffer
2011-07-21 02:12 1426字符串缓存类StringBuffer ...
相关推荐
Java 3DMenu 界面源码,有人说用到游戏中不错,其实平时我信编写Java应用程序时候也能用到吧,不一定非要局限于游戏吧,RES、SRC资源都有,都在压缩包内。 Java zip压缩包查看程序源码 1个目标文件 摘要:Java源码...
《Java范例开发大全》共22章,内容涉及Java开发环境的搭建、Java基础类型与运算符、条件控制语句、异常处理、数组、字符串、输入输出流、面向对象及其四大特征、内部类与接口、Java常用类、集合、多线程编程、Java...
在本项目"BlackJack-GUI-java-swing-"中,开发者使用了Java Swing库来创建一个图形用户界面(GUI)的BlackJack游戏,并遵循了模型-视图-控制器(MVC)设计模式。这是一个很好的实践,因为MVC模式有助于将业务逻辑、...
10. **JavaFX和Swing**:用于构建图形用户界面(GUI)的应用程序框架。 11. **Java 8及以后的新特性**:Lambda表达式、Stream API、Optional类、日期和时间API的改进等。 这个压缩包中的每个子文件名都可能对应一...
《Java范例开发大全》是一本全面覆盖Java编程的实用指南,旨在帮助初学者和有经验的开发者深入理解和掌握Java语言。这本书包含了从基础到高级的各类编程概念和技巧,通过丰富的实例代码来阐述理论知识,使学习过程...
在这个"Swing使用范例.rar"压缩包中,我们可以预见到包含了一系列的Java代码示例,这些示例将帮助初学者快速理解并掌握Swing的基本用法。Swing 提供了许多组件,如按钮(Button)、文本框(TextField)、标签(Label)、...
9. **GUI编程**:Java Swing和JavaFX库可用于创建图形用户界面。通过实例,你可以学习如何创建窗口、按钮、文本框等组件,并实现事件监听。 10. **数据库操作**:Java JDBC(Java Database Connectivity)允许与...
12. **Swing和JavaFX**:创建图形用户界面(GUI)的应用,包括组件、布局管理器、事件处理等。 13. **注解(Annotation)**:用于元编程,提供代码的附加信息,如Spring框架中的依赖注入。 14. **设计模式**:单例...
Java Swing 是Java平台上的一个库,它提供了丰富的用户界面组件,用于构建桌面应用程序。Swing 提供了一套轻量级的UI组件,可以创建美观且功能丰富的图形用户界面(GUI)。本章将详细介绍如何使用Swing进行桌面程序...
《Java范例开发大全》是一本专为Java初学者设计的教材,旨在通过丰富的实例帮助读者深入理解并掌握Java编程语言。这本书的核心理念是实践出真知,通过大量的代码示例来阐述Java语言的关键概念和技术,使学习者能够...
《Java范例开发大全》是一本深度探讨Java编程实践的书籍,其提供的全书源代码压缩包涵盖了370多个实例,旨在帮助开发者全面理解并掌握Java语言的应用。这些实例覆盖了Java语言的基础到高级特性,是学习和提升Java...
9. **GUI设计**:Java Swing和JavaFX库提供了丰富的组件和布局管理器,用于构建图形用户界面,实现交互式应用。 10. **反射**:Java反射机制允许在运行时动态地获取类的信息并操作其内部属性和方法,提高了程序的...
11. **JavaFX或Swing图形界面编程**:构建桌面应用,事件处理,布局管理等。 12. **Spring框架**:依赖注入,AOP,MVC模式,Spring Boot等。 这个资源包不仅有助于初学者快速上手,也是经验丰富的开发者巩固知识或...
10. **JavaFX或Swing**:如果是GUI编程的例子,可能会涉及到JavaFX或Swing库,用于创建桌面应用程序的用户界面。 11. **单元测试**:可能包含JUnit测试用例,展示了如何编写和执行单元测试,确保代码质量。 12. **...
- GUI编程:使用Java Swing或JavaFX构建图形用户界面,理解事件处理模型。 4. **Java标准库与API**: - 学习并理解Java基础库,如集合框架、IO流、日期时间API(java.time包)、反射机制、注解等。 - 探索Java并...
java 范例大全》和《施霞萍 .java程序设计教程》这两本书。 通过完成这个Java课程设计,学生不仅能够掌握基本的Java语法和面向对象编程概念,还能进一步了解Swing库的应用,特别是事件处理机制,这对于进一步开发...
- 用户界面设计:界面由Swing或AWT组件构建,包括一个主面板,采用网格布局管理器,将按钮以特定的排列方式分布,模拟真实计算器的按键布局。 - 类设计:可能包括一个主类(可能是`Calculator`),负责整体逻辑;...
10. **Java Swing和JavaFX**:图形用户界面(GUI)开发,包括组件使用、布局管理器、事件处理等。 11. **Java Applet**:嵌入网页的小型Java程序,用于交互式内容展示。 12. **JDBC数据库访问**:如何连接数据库,...
在“Java范例经典02”这个资源中,我们看到一系列关于Java编程的实例和练习,旨在帮助初学者和有经验的开发者进一步提升他们的技能。Java是一种广泛应用的面向对象的编程语言,以其跨平台的特性、强大的功能和丰富的...