Swing代码(6)

2019-05-17 19:53

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; } }


Swing代码(6).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:甲级单位编制光伏玻璃项目可行性报告(立项可研+贷款+用地+2013

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: