JTree tree = (JTree) e.getSource();
DefaultMutableTreeNode selectionNode = (DefaultMutableTreeNode) tree .getLastSelectedPathComponent(); String nodeName = selectionNode.toString();
if (selectionNode.isLeaf()) { String filepath = \ + System.getProperty(\ try { tp.setPage(filepath); } catch (IOException ex) {
System.out.println(\找不到此文件\
}
} } }
相关知识链接 PaintDemo.java
import java.awt.*; import javax.swing.*;
class MyPanel extends JPanel{ protected void paintComponent(Graphics g){ g.drawString(\组件使用paintComponent 绘制\ } }
public class PaintDemo extends JFrame{ public PaintDemo(String title){ super(title); this.setBounds(200,200,220,100); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); MyPanel p = new MyPanel(); this.add(p); this.validate(); } public static void main(String[] args){ new PaintDemo(\ } }
例题 6-1 简单计算器
import java.awt.*;
import java.awt.event.*; import javax.swing.*;
public class SimpleCalculator extends JFrame implements ActionListener { // 声明GUI组件及相关变量
JPanel p, p1, p2; JTextField tResult; JButton btnBK, btnC;
JButton[] btnNumber = new JButton[10];
JButton btnAdd, btnSub, btnMul, btnDiv, btnEqual, btnDot, btnSign; JButton btnSqrt, btnMod, btnReciprocal; boolean canClick; double memd; int memi;
double tempResult, result; short op = 0;
// 构造方法,建立GUI,为组件注册监听器
public SimpleCalculator() { canClick = true; result = 0; tResult = new JTextField(15); tResult.setEditable(false); tResult.setBackground(Color.WHITE); btnBK = new JButton(\ btnC = new JButton(\ for (int i = 0; i < 10; i++) btnNumber[i] = new JButton(Integer.toString(i)); btnAdd = new JButton(\ btnSub = new JButton(\ btnMul = new JButton(\ btnDiv = new JButton(\ btnMod = new JButton(\ btnSqrt = new JButton(\ btnReciprocal = new JButton(\ btnEqual = new JButton(\ btnDot = new JButton(\ btnSign = new JButton(\ Container c = this.getContentPane(); p1 = new JPanel(new FlowLayout(FlowLayout.RIGHT)); p2 = new JPanel(new GridLayout(4, 5, 3, 3)); c.add(tResult, BorderLayout.NORTH);
c.add(p1, BorderLayout.CENTER); c.add(p2, BorderLayout.SOUTH); p1.add(btnBK); p1.add(btnC);
p2.add(btnNumber[7]); p2.add(btnNumber[8]); p2.add(btnNumber[9]); p2.add(btnDiv); p2.add(btnSqrt);
p2.add(btnNumber[4]); p2.add(btnNumber[5]); p2.add(btnNumber[6]); p2.add(btnMul); p2.add(btnMod);
p2.add(btnNumber[1]); p2.add(btnNumber[2]); p2.add(btnNumber[3]); p2.add(btnSub);
p2.add(btnReciprocal); p2.add(btnNumber[0]); p2.add(btnSign); p2.add(btnDot); p2.add(btnAdd); p2.add(btnEqual);
for (int i = 0; i < 10; i++) btnNumber[i].addActionListener(this); btnBK.addActionListener(this); btnC.addActionListener(this); btnAdd.addActionListener(this); btnSub.addActionListener(this); btnMul.addActionListener(this); btnDiv.addActionListener(this); btnMod.addActionListener(this); btnSqrt.addActionListener(this);
btnReciprocal.addActionListener(this); btnEqual.addActionListener(this); btnDot.addActionListener(this); btnSign.addActionListener(this); this.setTitle(\简单计算器\
this.setLocation(200, 200); this.pack();
this.setVisible(true); this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
// 实现ActionListener接口,进行事件处理 public void actionPerformed(ActionEvent ae) { Object eSrc = ae.getSource(); // 事件源
for (int i = 0; i < 10; i++) { if (eSrc == btnNumber[i] && canClick == true) tResult.setText(tResult.getText() + Integer.toString(i)); }
if (eSrc == btnDot && canClick == true) { boolean hasDot = false; String s = tResult.getText(); if (s.length() == 0) hasDot = true; for (int i = 0; i < s.length(); i++) if (s.charAt(i) == '.') { hasDot = true; break; } if (hasDot == false) tResult.setText(s + \}
// 四种基本运算
if ((eSrc == btnAdd || eSrc == btnSub || eSrc == btnMul || eSrc == btnDiv) && canClick == true) { // “+” if (eSrc == btnAdd) { tempResult = Double.parseDouble(tResult.getText()); tResult.setText(\ op = 1; } // “-” if (eSrc == btnSub) { tempResult = Double.parseDouble(tResult.getText()); tResult.setText(\ op = 2; } // “*” if (eSrc == btnMul) { tempResult = Double.parseDouble(tResult.getText()); tResult.setText(\ op = 3; } // “/” if (eSrc == btnDiv) {
if (Double.parseDouble(tResult.getText()) == 0) { tResult.setText(\注意:除数不可为0\
canClick = false;
} else tempResult = Double.parseDouble(tResult.getText()); tResult.setText(\ op = 4; }
}// end 基本运算
// “=”
if (eSrc == btnEqual && canClick == true) { if (op == 1) { result = tempResult + Double.parseDouble(tResult.getText()); tResult.setText(Double.toString(result)); } if (op == 2) { result = tempResult - Double.parseDouble(tResult.getText()); tResult.setText(Double.toString(result)); } if (op == 3) { result = tempResult * Double.parseDouble(tResult.getText()); tResult.setText(Double.toString(result)); } if (op == 4) { if (Double.parseDouble(tResult.getText()) == 0) { tResult.setText(\除数不能为零\ canClick = false; } else { result = tempResult / Double.parseDouble(tResult.getText()); tResult.setText(Double.toString(result)); } } tempResult = 0; }
// “%”
if (eSrc == btnMod && canClick == true) { String s = tResult.getText(); boolean hasDot = false; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == '.') { hasDot = true; break; } }