武汉大学信息管理学院java上机考试_java上机试题
展開(kāi)全部
import?javax.swing.*;
import?java.awt.*;
import?java.awt.event.*;
public?class?Main?extends?JFrame?implements?ActionListener{
LabledText?upperBase?=?new?LabledText("上底:"),
62616964757a686964616fe59b9ee7ad9431333337373561lowerBase?=?new?LabledText("下底:"),
height????=?new?LabledText("高:");
JButton?getArea???=?new?JButton("求梯形面積");
JTextArearesult????=?new?JTextArea();
public?Main()?{
super("梯形");
JPanel?main?=?new?JPanel();
main.setLayout(new?BorderLayout(10,?10));
JPanel?top??=?new?JPanel(),
base?=?new?JPanel();
top.setLayout(new?GridLayout(2,?2));
top.add(upperBase);
top.add(lowerBase);
top.add(height);
getArea.addActionListener(this);
top.add(getArea);
main.add(top,?BorderLayout.NORTH);
result.setBorder(BorderFactory.createLoweredBevelBorder());
JScrollPane?scroll?=?new?JScrollPane(result);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scroll.setPreferredSize(new?Dimension(200,?100));
base.add(scroll);
main.add(base,?BorderLayout.CENTER);
main.setBorder(BorderFactory.createEmptyBorder(10,?10,?10,?10));
this.add(main);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.pack();
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public?void?actionPerformed(ActionEvent?arg0)?{
//?TODO?Auto-generated?method?stub
try?{
Trapezoid?trapezoid?=?new?Trapezoid(upperBase.getValue(),?lowerBase.getValue(),?height.getValue());
result.setText("梯形面積為:?"?+?String.valueOf(trapezoid.getArea()));
}?catch?(Exception?e)?{
result.setText(e.toString());
}
}
class?LabledText?extends?JPanel?{
JTextField?text?=?new?JTextField();
JLabellabel;
public?LabledText(String?nm)?{
this.setLayout(new?BorderLayout());
label?=?new?JLabel(nm);
label.setPreferredSize(new?Dimension(40,?30));
label.setHorizontalAlignment(JLabel.RIGHT);
this.add(label,?BorderLayout.WEST);
text.setPreferredSize(new?Dimension(60,?30));
this.add(text,?BorderLayout.EAST);
this.setBorder(BorderFactory.createEmptyBorder(5,?5,?5,?5));
}
public?double?getValue()?throws?Exception{
double?retval?=?Double.parseDouble(text.getText());
if(retval?
throw?new?Exception("長(zhǎng)度不能為負(fù)數(shù)");
return?retval;
}
}
public?static?void?main(String[]?args)?{
new?Main();
}
}
class?Trapezoid?{
private?double?upperBase,?lowerBase,?height;
public?Trapezoid()?{}
public?Trapezoid(double?upperBase,?double?lowerBase,?double?height)?{
this.setUpperBase(upperBase);
this.setLowerBase(lowerBase);
this.setHeight(height);
}
public?double?getUpperBase()?{
return?upperBase;
}
public?void?setUpperBase(double?upperBase)?{
this.upperBase?=?upperBase;
}
public?double?getLowerBase()?{
return?lowerBase;
}
public?void?setLowerBase(double?lowerBase)?{
this.lowerBase?=?lowerBase;
}
public?double?getHeight()?{
return?height;
}
public?void?setHeight(double?height)?{
this.height?=?height;
}
public?double?getArea()?{
return?(upperBase?+?lowerBase)?*?height?/?2;
}
}
總結(jié)
以上是生活随笔為你收集整理的武汉大学信息管理学院java上机考试_java上机试题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: C++ STL一些注意事项
- 下一篇: jquery li ul 伪分页_求教关