先写一个圆柱的类,然后写一个命令行方式计算的类,最后写一个界面类,每个类单独存成一个java文件,放在同一个目录就可以了。不好意思,第二个也写成计算圆柱的面积和体积了,呵呵,待会我再修改一下。
三个类如下:
//圆柱类class Cylinder {
double r, h// 圆柱的底圆的半径和高
public Cylinder(double r, double h) {
this.r = r
this.h = h
}
// 求面积
public double getArea() {
return 2 * Math.PI * r * h + 2 * Math.PI * r * r
}
// 求体积
public double getVolume() {
return Math.PI * r * r * h
}
} //命令行方式计算
import java.util.*
class ConsoleCylinder {
public static void main(String[] args) {
try {
System.out.print("请输入圆柱底面圆的半径:")
Scanner sc = new Scanner(System.in)
double r = sc.nextDouble()
System.out.print("请输入圆柱的高:")
double h = sc.nextDouble()
Cylinder cylinder = new Cylinder(r, h)
System.out.println("圆柱的面积为:" + cylinder.getArea())
System.out.println("圆柱的体积为:" + cylinder.getVolume())
} catch (Exception e) {
System.out.println("输入内容不正确!")
}
}
} //界面方式计算
import java.awt.*
import java.awt.event.*
import javax.swing.*
class GuiCylinder {
Cylinder cylinder
JPanel panel = new JPanel()
JFrame frame = new JFrame("求圆柱的面积和体积:")
JButton calc = new JButton("计算")
JLabel rLabel = new JLabel("输入半径:")
JLabel hLabel = new JLabel("输入高度:")
JTextField rText = new JTextField()
JTextField hText = new JTextField()
public void init() {
panel.setLayout(new GridLayout(2, 2))
panel.add(rLabel)
panel.add(rText)
panel.add(hLabel)
panel.add(hText)
frame.add(panel)
calc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
double r = Double.parseDouble(rText.getText())
double h = Double.parseDouble(hText.getText())
Cylinder cylinder = new Cylinder(r, h)
StringBuilder sb = new StringBuilder()
sb.append("圆柱的面积为:")
sb.append(cylinder.getArea())
sb.append("\n")
sb.append("圆柱的体积为:")
sb.append(cylinder.getVolume())
JOptionPane.showMessageDialog(null, sb.toString())
} catch (Exception exp) {
JOptionPane.showMessageDialog(null, "输入的内容不正确!")
}
}
})
frame.add(calc, BorderLayout.SOUTH)
frame.setLocation(300, 200)
frame.pack()
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
frame.setVisible(true)
}
public static void main(String[] args) {
new GuiCylinder().init()
}
}
第二个题的代码:
import java.awt.*import java.awt.event.*
import javax.swing.*
class GUICalcNumber {
JFrame frame = new JFrame("求整数的各位数之和:")
JButton calc = new JButton("计算")
JLabel label = new JLabel("输入一个整数:")
JTextField text = new JTextField()
public void init() {
frame.add(label, BorderLayout.NORTH)
frame.add(text)
frame.add(calc, BorderLayout.SOUTH)
calc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
int number = Integer.parseInt(text.getText())
int sum = 0
while (number != 0) {
sum += number % 10
number /= 10
}
JOptionPane.showMessageDialog(null,
"各位数之和为:" + String.valueOf(sum))
} catch (Exception exp) {
JOptionPane.showMessageDialog(null, "输入的内容不正确!")
}
}
})
//frame.add(calc, BorderLayout.SOUTH)
frame.setLocation(300, 200)
frame.pack()
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
frame.setVisible(true)
}
public static void main(String[] args) {
new GUICalcNumber().init()
}
}
public class HelpView 里面的this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)改为
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)
EXIT_ON_CLOSE表示关闭整个虚拟机,所有所有窗口都关闭了
绑定一个事件到被点击的窗体或控件,点击后获取控件信息,然后传参给一个自定义的窗体类的构造函数,如A = new oneDialog(参数1,参数2....)然后A.setVisible(true)。大概就是这个样子,有不懂得可以继续问欢迎分享,转载请注明来源:内存溢出
评论列表(0条)