找到
5
篇与
java
相关的结果
-
[for+switch]用循环打印出“*”Z字形 题目 需求: - ◆用循环打印出下列图形: ********** //10星 * * * * * * * * * ********** //10星 (提示:若用单层循环,则可以考虑for+switch结构实现) 代码 public class home { public static void main(String[] args) { String s = "*"; String n = " "; for (int l = 0; l < 11; l++) { //循环打印11行 switch (l){ case 0: case 10: System.out.println("**********"); //第一行和最后一行输出十个* break; default: //除开第一行和最后一行执行这个方法 s = "*"; n = ""; for (int j = 0; j <= 10-l; j++) { //根据行数来输出多少个“ ”(空格) n = n +" "; if (j == 9-l){ n = n + s; //末尾加上* } } System.out.println(n); } } /* 第一版 System.out.println("**********"); for (int i = 0; i < 9; i++) { s = "*"; n = ""; for (int j = 0; j <= 9-i; j++) { n = n +" "; if (j == 8-i){ n = n + s; } } System.out.println(n); } System.out.println("**********"); */ } } -
[switch + if else]输入一成绩,在控制台上输出其等级A/B/C/D/E..... 题目 需求: - ◆输入一成绩,在控制台上输出其等级A/B/C/D/E 请输入成绩(Q/q退出): 86 B 请输入成绩(Q/q退出): 74 C 请输入成绩(Q/q退出): 120 超出范围(0-100),请重新输入! 请输入成绩(Q/q退出): 98 A 请输入成绩(Q/q退出): q bye! (提示:可用switch语句实现;屏幕输入用Scanner类) 代码 public class Home { public static void main(String[] args) { int i = 1; while (i == 1){ System.out.println("请输入成绩(Q/q退出):"); Scanner sc = new Scanner(System.in); String input = sc.next(); switch (input){ case "q": case "Q": i = 0; System.out.println("bye!"); break; default: Double a = Double.valueOf(input); if (a>100){ System.out.println("超出范围(0-100),请重新输入!"); break; }else if (a>=90){ System.out.println("A"); break; }else if (a>=80){ System.out.println("B"); break; }else if (a>=70){ System.out.println("C"); break; }else if (a>=60){ System.out.println("D"); break; }else if (a>=0){ System.out.println("E"); }else { System.out.println("超出范围(0-100),请重新输入!"); break; } break; } } } } -
java水果商城管理系统(界面框架代码) 前提 - 实现前的准备,需要配置jdbc,导入jdbc驱动jdbc驱动包下载地址:[http://junruocdn.zmide.com/mysql-connector-java-5.1.30.jar](http://junruocdn.zmide.com/mysql-connector-java-5.1.30.jar)注意:如果失效了请之行百度下载! ## 正文开始 描述 - 本项目模拟真实的超市管理模式,以管理员为面向对象,所要实现的功能是基本的增删改查,具体实现步骤如下: 管理员登录 进入欢迎界面 进入仓库管理界面 管理员进行增删改查 ### 1,定义欢迎界面Welcome.java 描述 - 本界面 主要针对按钮进行了部分优化并设置了面板的背景显示 用户通过点击“进入系统”按钮进入到“登录界面 ```java package com.fruit.main; import java.util.prefs.BackingStoreException;import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel;public class Welcome extends JFrame{ JButton b; ImageIcon image; JLabel label; public Welcome() { // TODO Auto-generated constructor stub this.setTitle("水果超市欢迎您!"); this.setBounds(500, 100, 700, 600); this.setLayout(null); image = new ImageIcon("back.jpg"); label = new JLabel(image); label.setBounds(0, 0, 700, 500);b = new JButton("进入系统"); b.setBounds(200, 470, 280, 30); this.add(label); this.add(b); this.setVisible(true); } public static void main(String[] args) { // TODO Auto-generated method stub new Welcome(); } } ```### 2,登录界面Login.java ```java package com.fruit.main;import java.awt.Color; import java.awt.Font; import java.util.List; import java.util.Properties;import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPasswordField; import javax.swing.JRadioButton; import javax.swing.JTextField;import com.fruit.po.User; import com.fruit.utils.BaseDao;public class Login extends JFrame { JLabel l_username, l_password, l_per; JTextField t_username; JPasswordField t_password; JRadioButton r_per; JButton b_login, b_reg, b_reset, b_exit; public Login() { // TODO Auto-generated constructor stub this.setTitle("登录"); this.setBounds(500, 100, 700, 600); this.setLayout(null);// 界面设计 l_username = new JLabel("用户名:"); l_username.setBounds(190, 150, 60, 37); l_username.setForeground(Color.RED); t_username = new JTextField(); t_username.setBounds(250, 155, 180, 30); t_username.setFont(new Font("宋体", Font.PLAIN, 20)); l_password = new JLabel("密 码:"); l_password.setBounds(190, 215, 60, 37); l_password.setForeground(Color.blue); t_password = new JPasswordField(); t_password.setBounds(250, 220, 180, 30); t_password.setFont(new Font("宋体", Font.PLAIN, 20)); l_per = new JLabel("权 限:"); l_per.setBounds(190, 280, 130, 40); l_per.setForeground(Color.green); r_per = new JRadioButton("管理员"); r_per.setBounds(255, 290, 100, 20); r_per.setSelected(true); b_login = new JButton("登录"); b_login.setBounds(180, 350, 60, 30); b_reg = new JButton("注册"); b_reg.setBounds(260, 350, 60, 30); b_reset = new JButton("重置"); b_reset.setBounds(340, 350, 60, 30); b_exit = new JButton("退出"); b_exit.setBounds(425, 350, 60, 30); this.add(l_username); this.add(l_password); this.add(l_per); this.add(t_username); this.add(t_password); this.add(r_per); this.add(b_login); this.add(b_reg); this.add(b_reset); this.add(b_exit); this.setVisible(true); } public static void main(String[] args) { // TODO Auto-generated method stub // new Login(); new Welcome(); }} ```### 3,注册界面reg.java ```java package com.fruit.main; import java.awt.Color; import java.awt.Font; import java.io.FileOutputStream; import java.util.List; import java.util.Properties;import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JRadioButton; import javax.swing.JTextField;import com.fruit.po.User; import com.fruit.utils.BaseDao; import com.fruit.utils.PageUtil;public class Reg extends JFrame{ JLabel l_username, l_password; JTextField t_username,t_password; JButton b_login,b_reg,b_reset; public Reg() { // TODO Auto-generated constructor stub this.setTitle("注册"); this.setBounds(500, 100, 700, 600); this.setLayout(null);// 界面设计 l_username = new JLabel("用户名:"); l_username.setBounds(190, 150, 60, 37); l_username.setForeground(Color.RED); t_username=new JTextField(); t_username.setBounds(250, 155,180, 30); t_username.setFont(new Font("宋体",Font.PLAIN,20)); l_password = new JLabel("密 码:"); l_password.setBounds(190, 215, 60, 37); l_password.setForeground(Color.blue); t_password=new JTextField(); t_password.setBounds(250, 220,180, 30); t_password.setFont(new Font("宋体",Font.PLAIN,20)); b_reg = new JButton("确定注册"); b_reg.setBounds(190, 350, 90, 30); b_login = new JButton("返回登录"); b_login.setBounds(290, 350, 90, 30); b_reset = new JButton("重置"); b_reset.setBounds(390, 350, 90, 30); this.add(l_username); this.add(l_password); this.add(t_username); this.add(t_password); this.add(b_reg); this.add(b_login); this.add(b_reset); this.setVisible(true); } public static void main(String[] args) { // TODO Auto-generated method stub // new Reg(); new Welcome(); }} ```### 4,商店界面Goods.java ```java package com.fruit.main; import java.awt.Color; import java.awt.Font;import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextField;import com.fruit.utils.BaseDao; public class Goods extends JFrame{ JLabel listTitle,l_id,l_name,l_price,l_unit; JTable table; JScrollPane tablePane;//滚动 JTextField j_add_id,j_add_name,j_add_price,j_add_unit ,j_up_id,j_up_name,j_up_price,j_up_unit,j_de_id; JButton b_add,b_up,b_de,b_exit; public Goods() { // TODO Auto-generated constructor stub this.setTitle("超市货物管理"); this.setBounds(500, 100, 700, 600); this.getContentPane().setBackground(Color.cyan); this.setLayout(null);// 标题 listTitle = new JLabel("水果列表"); listTitle.setBounds(280, 15, 100, 20); listTitle.setFont(new Font("黑体",Font.PLAIN,20)); this.add(listTitle);// 退出登录 b_exit = new JButton("退出登录"); b_exit.setBounds(520, 15, 100, 20); b_exit.setFont(new Font("宋体",Font.PLAIN,16)); add(b_exit); //表格 table = new JTable(); tablePane = new JScrollPane(); table.getTableHeader().setReorderingAllowed(false);//列不可移动 table.getTableHeader().setResizingAllowed(false);//不可拉动表格 table.setEnabled(false);//不可更改数据 tablePane.setBounds(30,50,630,270); tablePane.setViewportView(table); this.add(tablePane);// 标签 l_id = new JLabel("水果编号"); l_name = new JLabel("水果名称"); l_price = new JLabel("水果单价"); l_unit = new JLabel("计价单位"); l_id.setBounds(30, 330, 100, 30); l_id.setFont(new Font("宋体",Font.PLAIN,16)); l_name.setBounds(150, 330, 100, 30); l_name.setFont(new Font("宋体",Font.PLAIN,16)); l_price.setBounds(280, 330, 100, 30); l_price.setFont(new Font("宋体",Font.PLAIN,16)); l_unit.setBounds(410, 330, 100, 30); l_unit.setFont(new Font("宋体",Font.PLAIN,16)); this.add(l_id); this.add(l_name); this.add(l_price); this.add(l_unit);// 增加 j_add_id = new JTextField(); j_add_name = new JTextField(); j_add_price = new JTextField(); j_add_unit = new JTextField(); j_add_id.setBounds(30, 360, 80, 30); j_add_name.setBounds(150, 360, 80, 30); j_add_price.setBounds(280, 360, 80, 30); j_add_unit.setBounds(410, 360, 80, 30); this.add(j_add_id); this.add(j_add_name); this.add(j_add_price); this.add(j_add_unit); b_add = new JButton("提交"); b_add.setBounds(540, 360, 80, 30); b_add.setFont(new Font("宋体",Font.PLAIN,16)); this.add(b_add);// 修改 j_up_id = new JTextField(); j_up_name = new JTextField(); j_up_price = new JTextField(); j_up_unit = new JTextField(); j_up_id.setBounds(30, 410, 80, 30); j_up_name.setBounds(150, 410, 80, 30); j_up_price.setBounds(280, 410, 80, 30); j_up_unit.setBounds(410, 410, 80, 30); this.add(j_up_id); this.add(j_up_name); this.add(j_up_price); this.add(j_up_unit); b_up = new JButton("改动"); b_up.setBounds(540, 410, 80, 36); b_up.setFont(new Font("宋体",Font.PLAIN,16)); this.add(b_up);// 删除 j_de_id = new JTextField(); j_de_id.setBounds(30, 460, 80, 36);//510 this.add(j_de_id); b_de = new JButton("删除"); b_de.setBounds(540, 460, 80, 36);//510 b_de.setFont(new Font("宋体",Font.PLAIN,16)); this.add(b_de); this.setVisible(true); } public static void main(String[] args) { // TODO Auto-generated method stub new Goods(); // new Welcome(); }} ``` -
又一个java习题—学生类设计 ## 题目 需求: - 设计一个学生类Student2,包含的属性有name和age。然后由这个学生类派生出本科生类Undergrad和研究生类Postgraduate,本科生类包含的属性为专业specialty,研究生包含的属性为研究方向studydirection。每个类都有相关数据的输出方法。 ## 相关代码 ```java package student;//包名 根据自己的写//主类Student(自行修改) public class Student { public static void main(String[] args) { // 创建一个本科生 Undergraduate ubder = new Undergraduate("小明",19,"移动通信"); ubder.info();//相关的数据输出方法 // 创建一个研究生 Postgraduate post = new Postgraduate("小红",22,"安卓移动应用开发"); post.info();//相关的数据输出方法 }} //创建一个学生类Student2 class Student2{ String name;// 姓名 int age;// 年龄 //学生类的一个相关数据的输出方法 public void show() { System.out.print(name+"今年"+age+"岁"); } } ``````java //创建一个本科生类Undergrad class Undergraduate extends Student2{ // 定义专业属性 String specialty; public Undergraduate(String name,int age,String specialty) { super.name=name; super.age=age; this.specialty=specialty; }//相关的数据输出方法 public void info() { super.show(); System.out.println(",他的专业是:"+specialty); }} //创建一个研究生类Postgraduate class Postgraduate extends Student2{ // 定义研究方向属性 String studydirection; //接收从主类输入的数据 public Postgraduate(String name,int age,String studydirection) { super.name=name; super.age=age; this.studydirection=studydirection; }//相关的数据输出方法 public void info() { super.show(); System.out.println(",他的研究方向是:"+studydirection); }} ``` -
Java几何计算器案例代码 ## Java几何计算器 成果展示 -  ### 1,程序主窗体代码(CalculatorWindow.java)```java import java.awt.BorderLayout; import java.awt.CardLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener;import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel;public class CalculatorWindow extends JFrame implements ActionListener,ItemListener { CardLayout mycard; JPanel controPanel,pCenter; JComboBox chooseList; JMenuBar menuBar; JMenu menu; JMenuItem mainWindow,exit; public CalculatorWindow(){ setTitle("常见几何图形计算器"); menuBar = new JMenuBar(); menu = new JMenu("操作"); mainWindow = new JMenuItem("主界面"); mainWindow.addActionListener(this); exit = new JMenuItem("退出"); exit.addActionListener(this); menu.add(mainWindow); menu.add(exit); menuBar.add(menu); setJMenuBar(menuBar); mycard = new CardLayout(); pCenter = new JPanel(); pCenter.setLayout(mycard); add(pCenter,"Center"); controPanel = new JPanel(); controPanel.setLayout(new BorderLayout()); chooseList = new JComboBox(); chooseList.addItem("请点击下拉列表选择"); chooseList.addItem("矩形的基本计算"); chooseList.addItem("圆的基本计算"); chooseList.addItem("三角形的基本计算"); chooseList.addItem("圆柱的基本计算"); chooseList.addItemListener(this); ImageIcon icon = new ImageIcon(图片地址); JButton imageButton = new JButton(icon); controPanel.add(imageButton,"Center"); controPanel.add(chooseList,"North"); pCenter.add("0",controPanel); pCenter.add("1",new RectanglePanel()); pCenter.add("2",new CirclePanel()); pCenter.add("3",new TrianglePanel()); pCenter.add("4",new CylinderPanel()); setBounds(100, 100, 700, 300); setResizable(false); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public void itemStateChanged(ItemEvent e) { int index = chooseList.getSelectedIndex(); String choice = String.valueOf(index); mycard.show(pCenter, choice); } public void actionPerformed(ActionEvent e) { if (e.getSource() == mainWindow) { mycard.first(pCenter); chooseList.setSelectedIndex(0); } else if (e.getSource() == exit) { System.exit(0); } } public static void main(String[] args) { new CalculatorWindow(); }} ```### 2,BoxPanel.java和AbstractPanel.java的实现 ```java //代码(BoxPanel.java)import java.awt.Font; import javax.swing.Box; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField;public class BoxPanel extends JPanel{ JTextField text; public BoxPanel(String s,int n) { Box box = Box.createHorizontalBox(); box.add(new JLabel(s)); text = new JTextField(" ",n); text.setHorizontalAlignment(JTextField.RIGHT); text.setFont(new Font("Arial",Font.BOLD,15)); box.add(text); add(box); } public JTextField getJTextField() { return text; } }//代码(AbstractPanel.java) import javax.swing.JPanel; import javax.swing.JTextField;public abstract class AbstractPanel extends JPanel { public abstract JTextField getInputTextField(); } ```###### 3,矩形计算器的实现(RectanglePanel.java) ```java import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.FocusEvent; import java.awt.event.FocusListener;import javax.swing.Box; import javax.swing.JButton; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField;public class RectanglePanel extends AbstractPanel implements ActionListener,FocusListener{ JButton resultButton,clearButton; JPanel leftPanel,rightPanel,buttonPanel; JTextField widthTextField,heightTextField; JTextField lengthTextField,areaTextField,inputTextField; BoxPanel bpWidth,bpHeight,bpLength,bpArea; public RectanglePanel() { setLayout(new GridLayout(1,2)); rightPanel = new KeyJPanel(this); leftPanel = new JPanel(); Box box = Box.createVerticalBox(); bpWidth = new BoxPanel("请输入矩形的宽", 10); widthTextField = bpWidth.getJTextField(); widthTextField.addFocusListener(this); bpHeight = new BoxPanel("请输入矩形的高",10); heightTextField = bpHeight.getJTextField(); heightTextField.addFocusListener(this); buttonPanel = new JPanel(); resultButton = new JButton("计算结果"); resultButton.addActionListener(this); clearButton = new JButton("清空"); clearButton.addActionListener(this); buttonPanel.add(resultButton); buttonPanel.add(clearButton); bpLength = new BoxPanel("矩形的周长:", 20); lengthTextField = bpLength.getJTextField(); bpArea = new BoxPanel("矩形的面积:",20); this.areaTextField = this.bpArea.getJTextField(); box.add(bpWidth); box.add(bpHeight); box.add(buttonPanel); box.add(bpLength); box.add(bpArea); leftPanel.add(box); add(leftPanel); add(rightPanel); } public void actionPerformed(ActionEvent e) { if (e.getSource() == resultButton) { try { double width = Double.parseDouble(widthTextField.getText()); double height = Double.parseDouble(heightTextField.getText()); lengthTextField.setText(""+2*(width + height)); areaTextField.setText(""+width*height); } catch (NumberFormatException e1) { JOptionPane.showMessageDialog(this, "请输入数字:","警告对话框",JOptionPane.WARNING_MESSAGE); } } else if (e.getSource() == clearButton) { widthTextField.setText(" "); heightTextField.setText(" "); } } public void focusGained(FocusEvent e) { inputTextField = (JTextField)e.getSource(); } public void focusLost(FocusEvent e) {} public JTextField getInputTextField() { return inputTextField; }} ```### 4,圆计算器的实现(CirclePanel.java) ```java import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.FocusEvent; import java.awt.event.FocusListener;import javax.swing.Box; import javax.swing.JButton; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField;public class CirclePanel extends AbstractPanel implements ActionListener,FocusListener{ JButton resultButton,clearButton; JPanel leftPanel,rightPanel,buttonPanel; JTextField radiusTextField; JTextField lengthTextField,areaTextField,inputTextField; BoxPanel bpRadius,bpLength,bpArea; public CirclePanel() { setLayout(new GridLayout(1,2)); rightPanel = new KeyJPanel(this); leftPanel =new JPanel(); Box box = Box.createVerticalBox(); bpRadius = new BoxPanel("请输入圆的半径:", 10); radiusTextField = bpRadius.getJTextField(); radiusTextField.addFocusListener(this); buttonPanel = new JPanel(); resultButton = new JButton("计算结果:"); resultButton.addActionListener(this); clearButton = new JButton("清空"); clearButton.addActionListener(this); buttonPanel.add(resultButton); buttonPanel.add(clearButton); bpLength = new BoxPanel("圆的周长:", 20); lengthTextField = bpLength.getJTextField(); bpArea = new BoxpPanel("圆的面积:", 20); areaTextField = bpArea.getJTextField(); box.add(bpRadius); box.add(buttonPanel); box.add(bpLength); box.add(bpArea); leftPanel.add(box); add(leftPanel); add(rightPanel); } public void actionPerformed(ActionEvent e) { if (e.getSource() == resultButton) { try { double radius = Double.parseDouble(radiusTextField.getText()); lengthTextField.setText(""+2*Math.PI*radius); areaTextField.setText(""+Math.PI*radius*radius); } catch (NumberFormatException e1) { JOptionPane.showMessageDialog(this, "请输入数字:","警告对话框",JOptionPane.WARNING_MESSAGE); } } else if (e.getSource() == clearButton) { radiusTextField.setText(" "); } } public void focusGained(FocusEvent e) { inputTextField = (JTextField)e.getSource(); } public void focusLost(FocusEvent e) {} public JTextField getInputTextField() { return inputTextField; }} ```### 5,三角形计算器的实现(TrianglePanel.java) ```java import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.FocusEvent; import java.awt.event.FocusListener;import javax.swing.Box; import javax.swing.JButton; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField;public class TrianglePanel extends AbstractPanel implements ActionListener,FocusListener{ JButton resultButton,clearButton; JPanel leftPanel,rightPanel,buttonPanel; JTextField sideATextField,sideBTextField,sideCTextField; JTextField lengthTextField,areaTextField,inputTextField; BoxPanel bpSideA,bpSideB,bpSideC,bpLength,bpArea; public TrianglePanel() { setLayout(new GridLayout(1,2)); rightPanel = new KeyJPanel(this); leftPanel =new JPanel(); Box box = Box.createVerticalBox(); bpSideA = new BoxPanel("请输入三角形的边A:",10); sideATextField = bpSideA.getJTextField(); sideATextField.addFocusListener(this); bpSideB = new BoxPanel("请输入三角形的边B:",10); sideBTextField = bpSideB.getJTextField(); sideBTextField.addFocusListener(this); bpSideC = new BoxPanel("请输入三角形的边C:",10); sideCTextField = bpSideC.getJTextField(); sideCTextField.addFocusListener(this); buttonPanel = new JPanel(); resultButton = new JButton("计算结果:"); resultButton.addActionListener(this); clearButton = new JButton("清空"); clearButton.addActionListener(this); buttonPanel.add(resultButton); buttonPanel.add(clearButton); bpLength = new BoxPanel("三角形的周长:", 20); lengthTextField = bpLength.getJTextField(); bpArea = new BoxPanel("三角形的面积:", 20); areaTextField = bpArea.getJTextField(); box.add(bpSideA); box.add(bpSideB); box.add(bpSideC); box.add(buttonPanel); box.add(bpLength); box.add(bpArea); leftPanel.add(box); add(leftPanel); add(rightPanel); } public void actionPerformed(ActionEvent e) { if (e.getSource() == resultButton) { try { double sideA = Double.parseDouble(sideATextField.getText()); double sideB = Double.parseDouble(sideBTextField.getText()); double sideC = Double.parseDouble(sideCTextField.getText()); if ((sideA + sideB>sideC)&&(sideA + sideC>sideB)&&(sideB + sideC>sideA)) { double p = (sideA + sideB +sideC)/2.0; lengthTextField.setText("" + 2 * p); double area = Math.sqrt(p * (p - sideA) * (p - sideB) *(p - sideC)); areaTextField.setText("" + area); } else { JOptionPane.showMessageDialog(this, "这不构成一个三角形,请重新输入三边","警告对话框",JOptionPane.WARNING_MESSAGE); } } catch (NumberFormatException e1) { JOptionPane.showMessageDialog(this, "请输入数字:","警告对话框",JOptionPane.WARNING_MESSAGE); } } else if (e.getSource() == clearButton) { sideATextField.setText(" "); sideBTextField.setText(" "); sideCTextField.setText(" "); } } public void focusGained(FocusEvent e) { inputTextField = (JTextField)e.getSource(); } public void focusLost(FocusEvent e) {} public JTextField getInputTextField() { return inputTextField; }} ```### 6,圆柱计算器的实现(CylinderPanel.java) ```java import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.FocusEvent; import java.awt.event.FocusListener;import javax.swing.Box; import javax.swing.JButton; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField;public class CylinderPanel extends AbstractPanel implements ActionListener,FocusListener{ JButton resultButton,clearButton; JPanel leftPanel,rightPanel,buttonPanel; JTextField radiusTextField,highTextField; JTextField surfaceAreaTextField,volumeTextField,inputTextField; BoxPanel bpRadius,bpHeight,bpSurfaceArea,bpVolume; public CylinderPanel() { setLayout(new GridLayout(1,2)); rightPanel = new KeyJPanel(this); leftPanel =new JPanel(); Box box = Box.createVerticalBox(); bpRadius = new BoxPanel("请输入圆柱的底面半径:", 10); radiusTextField = bpRadius.getJTextField(); radiusTextField.addFocusListener(this); bpHeight = new BoxPanel("请输入圆柱的高:", 10); highTextField = bpHeight.getJTextField(); highTextField.addFocusListener(this); buttonPanel = new JPanel(); resultButton = new JButton("计算结果:"); resultButton.addActionListener(this); clearButton = new JButton("清空"); clearButton.addActionListener(this); buttonPanel.add(resultButton); buttonPanel.add(clearButton); bpSurfaceArea = new BoxPanel("圆柱的表面积:", 20); this.surfaceAreaTextField = this.bpSurfaceArea.getJTextField(); bpVolume = new BoxPanel("圆柱的体积:", 20); this.volumeTextField = this.bpVolume.getJTextField(); box.add(bpRadius); box.add(bpHeight); box.add(buttonPanel); box.add(bpSurfaceArea); box.add(bpVolume); leftPanel.add(box); add(leftPanel); add(rightPanel); } public void actionPerformed(ActionEvent e) { if (e.getSource() == resultButton) { try { double radius = Double.parseDouble(radiusTextField.getText()); double height = Double.parseDouble(highTextField.getText()); double area = Math.PI * radius *radius; surfaceAreaTextField.setText(""+(Math.PI * 2 * radius * height + 2 * area)); volumeTextField.setText(""+area * height); } catch (NumberFormatException e1) { JOptionPane.showMessageDialog(this, "请输入数字:","警告对话框",JOptionPane.WARNING_MESSAGE); } } else if (e.getSource() == clearButton) { radiusTextField.setText(" "); highTextField.setText(" "); } } public void focusLost(FocusEvent e) { inputTextField = (JTextField)e.getSource(); } public void focusGained(FocusEvent e) {} public JTextField getInputTextField() { return inputTextField; }} ```### 7,数字软键盘的实现(KeyJPanel.java) ```java import java.awt.Color; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.border.Border;public class KeyJPanel extends JPanel implements ActionListener{ JButton[] keyButton = new JButton[12]; String[] num = {"1","2","3","4","5","6","7","8","9","0",".","BackSpace"}; AbstractPanel selectedPanel; JTextField inputTextField; public KeyJPanel(AbstractPanel selectedPanel) { this.selectedPanel = selectedPanel; Border lb = BorderFactory.createLineBorder(Color.gray, 2); setBorder(lb); setLayout(new GridLayout(4,3)); for (int i = 0; i < 12; i++) { keyButton[i] = new JButton(num[i]); keyButton[i].setFont(new Font("Arial",Font.BOLD,15)); keyButton[i].setForeground(Color.BLACK); keyButton[i].addActionListener(this); add(keyButton[i]); } } public void actionPerformed(ActionEvent e) { JButton button = (JButton)e.getSource(); inputTextField = selectedPanel.getInputTextField(); inputNumber(inputTextField,button); } private void inputNumber(JTextField tf, JButton button) { String oldString = tf.getText(); if (oldString == null) { tf.setText(" "); } String subStr = oldString.substring(0,oldString.length() -1); String newString = button.getText(); if (newString.equals("BackSpace")) { tf.setText(subStr); } else if (newString.equals(".")) { tf.setText(oldString+"."); } else { tf.setText(oldString+newString); } }} ```