只能给你第一个:
import javaawt;
import javaawtevent;
import javaxswing;
public class JNotePadUI extends JFrame {
private JMenuItem menuOpen;
private JMenuItem menuSave;
private JMenuItem menuSaveAs;
private JMenuItem menuClose;
private JMenu editMenu;
private JMenuItem menuCut;
private JMenuItem menuCopy;
private JMenuItem menuPaste;
private JMenuItem menuAbout;
private JTextArea textArea;
private JLabel stateBar;
private JFileChooser fileChooser;
private JPopupMenu popUpMenu;
public JNotePadUI() {
super("新建文本文件");
setUpUIComponent();
setUpEventListener();
setVisible(true);
}
private void setUpUIComponent() {
setSize(640, 480);
// 菜单栏
JMenuBar menuBar = new JMenuBar();
// 设置「文件」菜单
JMenu fileMenu = new JMenu("文件");
menuOpen = new JMenuItem("打开");
// 快捷键设置
menuOpensetAccelerator(
KeyStrokegetKeyStroke(
KeyEventVK_O,
InputEventCTRL_MASK));
menuSave = new JMenuItem("保存");
menuSavesetAccelerator(
KeyStrokegetKeyStroke(
KeyEventVK_S,
InputEventCTRL_MASK));
menuSaveAs = new JMenuItem("另存为");
menuClose = new JMenuItem("关闭");
menuClosesetAccelerator(
KeyStrokegetKeyStroke(
KeyEventVK_Q,
InputEventCTRL_MASK));
fileMenuadd(menuOpen);
fileMenuaddSeparator(); // 分隔线
fileMenuadd(menuSave);
fileMenuadd(menuSaveAs);
fileMenuaddSeparator(); // 分隔线
fileMenuadd(menuClose);
// 设置「编辑」菜单
JMenu editMenu = new JMenu("编辑");
menuCut = new JMenuItem("剪切");
menuCutsetAccelerator(
KeyStrokegetKeyStroke(KeyEventVK_X,
InputEventCTRL_MASK));
menuCopy = new JMenuItem("复制");
menuCopysetAccelerator(
KeyStrokegetKeyStroke(KeyEventVK_C,
InputEventCTRL_MASK));
menuPaste = new JMenuItem("粘贴");
menuPastesetAccelerator(
KeyStrokegetKeyStroke(KeyEventVK_V,
InputEventCTRL_MASK));
editMenuadd(menuCut);
editMenuadd(menuCopy);
editMenuadd(menuPaste);
// 设置「关于」菜单
JMenu aboutMenu = new JMenu("关于");
menuAbout = new JMenuItem("关于JNotePad");
aboutMenuadd(menuAbout);
menuBaradd(fileMenu);
menuBaradd(editMenu);
menuBaradd(aboutMenu);
setJMenuBar(menuBar);
// 文字编辑区域
textArea = new JTextArea();
textAreasetFont(new Font("宋体", FontPLAIN, 16));
textAreasetLineWrap(true);
JScrollPane panel = new JScrollPane(textArea,
ScrollPaneConstantsVERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstantsHORIZONTAL_SCROLLBAR_NEVER);
Container contentPane = getContentPane();
contentPaneadd(panel, BorderLayoutCENTER);
// 状态栏
stateBar = new JLabel("未修改");
stateBarsetHorizontalAlignment(SwingConstantsLEFT);
stateBarsetBorder(
BorderFactorycreateEtchedBorder());
contentPaneadd(stateBar, BorderLayoutSOUTH);
popUpMenu = editMenugetPopupMenu();
fileChooser = new JFileChooser();
}
private void setUpEventListener() {
// 按下窗口关闭钮事件处理
addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
closeFile();
}
}
);
// 菜单 - 打开
menuOpenaddActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
openFile();
}
}
);
// 菜单 - 保存
menuSaveaddActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
saveFile();
}
}
);
// 菜单 - 另存为
menuSaveAsaddActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
saveFileAs();
}
}
);
// 菜单 - 关闭文件
menuCloseaddActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
closeFile();
}
}
);
// 菜单 - 剪切
menuCutaddActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
cut();
}
}
);
// 菜单 - 复制
menuCopyaddActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
copy();
}
}
);
// 菜单 - 粘贴
menuPasteaddActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
paste();
}
}
);
// 菜单 - 关于
menuAboutaddActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 显示对话框
JOptionPaneshowOptionDialog(null,
"程序名称:\n JNotePad \n" +
"程序设计:\n \n" +
"简介:\n 一个简单的文字编辑器\n",
"关于JNotePad",
JOptionPaneDEFAULT_OPTION,
JOptionPaneINFORMATION_MESSAGE,
null, null, null);
}
}
);
// 编辑区键盘事件
textAreaaddKeyListener(
new KeyAdapter() {
public void keyTyped(KeyEvent e) {
processTextArea();
}
}
);
// 编辑区鼠标事件
textAreaaddMouseListener(
new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
if(egetButton() == MouseEventBUTTON3)
popUpMenushow(editMenu, egetX(), egetY());
}
public void mouseClicked(MouseEvent e) {
if(egetButton() == MouseEventBUTTON1)
popUpMenusetVisible(false);
}
}
);
}
private void openFile() {
if(isCurrentFileSaved()) { // 文件是否为保存状态
open(); // 打开
}
else {
// 显示对话框
int option = JOptionPaneshowConfirmDialog(
null, "文件已修改,是否保存?",
"保存文件?", JOptionPaneYES_NO_OPTION,
JOptionPaneWARNING_MESSAGE, null);
switch(option) {
// 确认文件保存
case JOptionPaneYES_OPTION:
saveFile(); // 保存文件
break;
// 放弃文件保存
case JOptionPaneNO_OPTION:
open();
break;
}
}
}
private boolean isCurrentFileSaved() {
if(stateBargetText()equals("未修改")) {
return true;
}
else {
return false;
}
}
private void open() {
// fileChooser 是 JFileChooser 的实例
// 显示文件选取的对话框
int option = fileChoosershowDialog(null, null);
// 使用者按下确认键
if(option == JFileChooserAPPROVE_OPTION) {
/
TODO: 添加读取文件的代码
/
}
}
private void saveFile() {
/
TODO: 添加保存文件的代码
/
}
private void saveFileAs() {
/
TODO: 添加另存为的代码
/
}
private void closeFile() {
// 是否已保存文件
if(isCurrentFileSaved()) {
// 释放窗口资源,而后关闭程序
dispose();
}
else {
int option = JOptionPaneshowConfirmDialog(
null, "文件已修改,是否保存?",
"保存文件?", JOptionPaneYES_NO_OPTION,
JOptionPaneWARNING_MESSAGE, null);
switch(option) {
case JOptionPaneYES_OPTION:
saveFile();
break;
case JOptionPaneNO_OPTION:
dispose();
}
}
}
private void cut() {
textAreacut();
stateBarsetText("已修改");
popUpMenusetVisible(false);
}
private void copy() {
textAreacopy();
popUpMenusetVisible(false);
}
private void paste() {
textAreapaste();
stateBarsetText("已修改");
popUpMenusetVisible(false);
}
private void processTextArea() {
stateBarsetText("已修改");
}
public static void main(String[] args) {
new JNotePadUI();
}
}
public class Invoice {
String bianhao = null;
String shuoming = null;
int count = 0;
double price = 00;
public Invoice(String bianhao, String shuoming, int count, double price) {
thisbianhao = bianhao;
thisshuoming = shuoming;
if (count < 0) {
thiscount = 0;
} else {
thiscount = count;
}
if (price < 00) {
thisprice = 00;
} else {
thisprice = price;
}
}
public double getInvoiceAmount() {
return count price;
}
public String getBianhao() {
return bianhao;
}
public void setBianhao(String bianhao) {
thisbianhao = bianhao;
}
public int getCount() {
return count;
}
public void setCount(int count) {
thiscount = count;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
thisprice = price;
}
public String getShuoming() {
return shuoming;
}
public void setShuoming(String shuoming) {
thisshuoming = shuoming;
}
}
public class InvoiceTest {
/
@param args
/
public static void main(String[] args) {
Invoice invoice = new Invoice("010220", "Desk", 50, 539);
Systemoutprintln(invoicegetInvoiceAmount());
}
}
这个你参考一下。
import javaxswing;
//import javaxswingevent;
import javaawt;
import javaawtevent;
//计算器显示结果的窗体
class Result extends JPanel
{
JTextField text = new JTextField("0"); //text先是输入和结果
Result()
{
textsetHorizontalAlignment(SwingConstantsRIGHT); //讲输入的数字或得到的结果在text的右边显示
textsetEnabled(false); //文本框不能编辑
setLayout(new BorderLayout()); //设定布局管理器边框布局
add(text, BorderLayoutCENTER); //text放置在窗体的中间
}
}
//计算器数字按钮定义面板
class Number_Key extends JPanel
{
JButton zero = new JButton("0");//数字键0
JButton one = new JButton("1");//数字键1
JButton two = new JButton("2");//数字键2
JButton three = new JButton("3");//数字键3
JButton four = new JButton("4");//数字键4
JButton five = new JButton("5");//数字键5
JButton six = new JButton("6");//数字键6
JButton seven = new JButton("7");//数字键7
JButton eight = new JButton("8");//数字键8
JButton nine = new JButton("9");//数字键9
JButton plus = new JButton("+");
JButton sub = new JButton("-");
JButton mul = new JButton("");
JButton div = new JButton("/");
JButton equal = new JButton("=");
JButton ce = new JButton("ce");//置零键
JButton point = new JButton("");
JButton tzero = new JButton("00");
Number_Key()
{
setLayout(new GridLayout(6, 3, 10, 10));//定义布局管理器为网格布局
//添加各个按钮键
add(seven);
add(eight);
add(nine);
add(four);
add(five);
add(six);
add(one);
add(two);
add(three);
add(zero);
add(tzero);
add(plus);
add(sub);
add(mul);
add(div);
add(point);
add(equal);
add(ce);
}
}
//计算器主类
class sakura extends JFrame implements ActionListener
{
Result result = new Result();//定义text的面板
Number_Key number_key = new Number_Key();//定义按钮面板
//当点击按钮+、-、、/时,com = true
boolean com = false;
//当i=0时说明是我们第一次输入,字符串sum不会累加
int i = 0;
//存放text的内容
String sum = "";
//存放点击按钮+、-、、/之前的数值
double total = 0;
//+、-、、/的代号分别为1,2,3,4
int symbol = 0;
sakura()
{
super("Calculator");//设定标题
setDefaultCloseOperation(JFrameEXIT_ON_CLOSE);//设定关闭窗体时退出程序
JPanel pane = new JPanel();//定义主面板
panesetLayout(new BorderLayout());
paneadd(result, BorderLayoutNORTH);
paneadd(number_key, BorderLayoutSOUTH);
number_keyoneaddActionListener(this);//对1按钮添加监听事件
number_keytwoaddActionListener(this);//对2按钮添加监听事件
number_keythreeaddActionListener(this);//对3按钮添加监听事件
number_keyfouraddActionListener(this);//对4按钮添加监听事件
number_keyfiveaddActionListener(this);//对5按钮添加监听事件
number_keysixaddActionListener(this);//对6按钮添加监听事件
number_keysevenaddActionListener(this);//对7按钮添加监听事件
number_keyeightaddActionListener(this);//对8按钮添加监听事件
number_keynineaddActionListener(this);//对9按钮添加监听事件
number_keyzeroaddActionListener(this);//对0按钮添加监听事件
number_keyceaddActionListener(this);//对置零按钮添加监听事件
number_keyplusaddActionListener(this);//对+按钮添加监听事件
number_keyequaladdActionListener(this);//对=按钮添加监听事件
number_keysubaddActionListener(this);//对-按钮添加监听事件
number_keymuladdActionListener(this);//对按钮添加监听事件
number_keydivaddActionListener(this);//对/按钮添加监听事件
number_keytzeroaddActionListener(this);//对00按钮添加监听事件
number_keypointaddActionListener(this);//对按钮添加监听事件
setContentPane(pane);
pack();//初始化窗体大小为正好盛放所有按钮
}
//各个按钮触发的事件
public void actionPerformed(ActionEvent e) {
/如果是点击数字按钮那么先要判断是否在此之前点击了+、-、、/、=,如果是那么com=true
如果没有com= false;或者是否点击数字键,如果是i = 1,如果没有 i = 0;
/
if (egetSource() == number_keyone)
{
if (com || i == 0)
{
resulttextsetText("1");
com = false;
i = 1;
}
else
{
sum = resulttextgetText();
resulttextsetText(sum + "1");
}
}
else if (egetSource() == number_keytwo)
{
if (com || i == 0)
{
resulttextsetText("2");
com = false;
i = 1;
}
else
{
sum = resulttextgetText();
resulttextsetText(sum + "2");
}
}
else if (egetSource() == number_keythree)
{
if (com || i == 0)
{
resulttextsetText("3");
com = false;
i = 1;
}
else
{
sum = resulttextgetText();
resulttextsetText(sum + "3");
}
}
else if (egetSource() == number_keyfour)
{
if (com || i == 0)
{
resulttextsetText("4");
com = false;
i = 1;
}
else
{
sum = resulttextgetText();
resulttextsetText(sum + "4");
}
}
else if (egetSource() == number_keyfive)
{
if (com || i == 0)
{
resulttextsetText("5");
com = false;
i = 1;
}
else
{
sum = resulttextgetText();
resulttextsetText(sum + "5");
}
}
else if (egetSource() == number_keysix)
{
if (com || i == 0)
{
resulttextsetText("6");
com = false;
i = 1;
}
else
{
sum = resulttextgetText();
resulttextsetText(sum + "6");
}
}
else if (egetSource() == number_keyseven)
{
if (com || i == 0)
{
resulttextsetText("7");
com = false;
i = 1;
}
else
{
sum = resulttextgetText();
resulttextsetText(sum + "7");
}
}
else if (egetSource() == number_keyeight)
{
if (com || i == 0)
{
resulttextsetText("8");
com = false;
i = 1;
}
else
{
sum = resulttextgetText();
resulttextsetText(sum + "8");
}
}
else if (egetSource() == number_keynine)
{
if (com || i == 0)
{
resulttextsetText("9");
com = false;
i = 1;
}
else
{
sum = resulttextgetText();
resulttextsetText(sum + "9");
}
}
/对于0这个按钮有一定的说法,在我的程序里不会出现如00000这样的情况,我加了判断条件就是
如果text中的数值=0就要判断在这个数值中是否有存在如果有那么就在原来数值基础之上添
加0;否则保持原来的数值不变
/
else if (egetSource() == number_keyzero)
{
if (com || i == 0)
{
resulttextsetText("0");
com = false;
i = 1;
}
else
{
sum = resulttextgetText();
if (FloatparseFloat(sum) > 0 || FloatparseFloat(sum) < 0)
{
resulttextsetText(sum + "0");
}
else
{
if (sumtrim()indexOf("") == -1)
{
resulttextsetText(sum);
}
else
{
resulttextsetText(sum + "0");
}
}
}
}
else if (egetSource() == number_keyce)
{
resulttextsetText("0");
i = 0;
com = true;
}
else if (egetSource() == number_keytzero)
{
if (com || i == 0)
{
resulttextsetText("0");
com = false;
i = 1;
}
else
{
sum = resulttextgetText();
if (FloatparseFloat(sum) > 0 || FloatparseFloat(sum) < 0)
{
resulttextsetText(sum + "00");
}
else
{
if (sumtrim()indexOf("") == -1)
{
resulttextsetText(sum);
}
else
{
resulttextsetText(sum + "00");
}
}
}
}
/本程序不会让一个数值中出现2个以上的小数点具体做法是:判断是否已经存在存在就不添加,
不存在就添加
/
else if (egetSource() == number_keypoint)
{
if (com || i == 0)
{
resulttextsetText("0");
com = false;
i = 1;
}
else
{
sum = resulttextgetText();
if (sumtrim()indexOf("") == -1)
{
resulttextsetText(sum + "");
}
else
{
resulttextsetText(sum);
}
}
}
//获得点击+之前的数值
else if (egetSource() == number_keyplus)
{
com = true;
i = 0;
total = DoubleparseDouble(resulttextgetText());
symbol = 1;
}//获得点击-之前的数值
else if (egetSource() == number_keysub)
{
com = true;
i = 0;
total = DoubleparseDouble(resulttextgetText());
symbol = 2;
}//获得点击之前的数值
else if (egetSource() == number_keymul)
{
com = true;
i = 0;
total = DoubleparseDouble(resulttextgetText());
Systemoutprintln(total);
symbol = 3;
}//获得点击/之前的数值
else if (egetSource() == number_keydiv)
{
com = true;
i = 0;
total = DoubleparseDouble(resulttextgetText());
symbol = 4;
}
else if (egetSource() == number_keyequal)
{
switch (symbol)
{
case 1 ://计算加法
{
double ad =
total + DoubleparseDouble(resulttextgetText());
resulttextsetText(ad + "");
i = 0;
sum = "";
break;
}
case 2 ://计算减法
{
double ad =
total - DoubleparseDouble(resulttextgetText());
resulttextsetText(StringvalueOf(ad));
i = 0;
sum = "";
break;
}
case 3 ://计算乘法
{
double ad =
total DoubleparseDouble(resulttextgetText());
resulttextsetText(ad + "");
i = 0;
sum = "";
break;
}
case 4 ://计算除法
{
double ad =
total / DoubleparseDouble(resulttextgetText());
resulttextsetText(ad + "");
i = 0;
sum = "";
break;
}
}
Systemoutprintln(com);
}
}
public static void main(String[] args)
{
sakura ww = new sakura();
wwsetVisible(true) ;
}
}
下面是一个可能的Java源代码,它包含了一个接口(Shape)和五个类(Circle, Rectangle, Triangle, Square 和 Main)。它的功能是计算不同形状的面积和周长。
//定义一个接口Shape,有两个抽象方法:getArea()和getPerimeter()interface Shape { double getArea(); double getPerimeter();
}//定义一个类Circle,实现Shape接口class Circle implements Shape { //定义一个私有属性radius,表示圆的半径
private double radius; //定义一个公有构造方法,用于初始化radius
public Circle(double radius) { thisradius = radius;
} //实现getArea()方法,返回圆的面积
public double getArea() { return MathPI radius radius;
} //实现getPerimeter()方法,返回圆的周长
public double getPerimeter() { return MathPI radius 2;
}
}//定义一个类Rectangle,实现Shape接口class Rectangle implements Shape { //定义两个私有属性width和height,表示矩形的宽度和高度
private double width; private double height; //定义一个公有构造方法,用于初始化width和height
public Rectangle(double width, double height) { thiswidth = width; thisheight = height;
} //实现getArea()方法,返回矩形的面积
public double getArea() { return width height;
} //实现getPerimeter()方法,返回矩形的周长
public double getPerimeter() { return (width + height) 2;
}
}//定义一个类Triangle,实现Shape接口class Triangle implements Shape { //定义三个私有属性a,b,c表示三角形的三条边长
private double a; private double b; private double c; //定义一个公有构造方法,用于初始化a,b,c,并检查是否满足三角形条件(任意两边之和大于第三边)
public Triangle(double a, double b, double c) throws Exception{ if (a + b > c && a + c > b && b + c > a) {
thisa = a; thisb = b;
thisc = c;
} else {
throw new Exception("Invalid triangle");
}
} //实现getArea()方法,返回三角形的面积(使用海伦公式)
public double getArea() { //计算半周长p
double p = (a + b + c) /2; //计算并返回面积s(使用Mathsqrt()函数求平方根)
return Mathsqrt(p (p - a) (p - b) (p - c));
} //实现getPerimeter()方法,返回三角形的周长
public double getPerimeter(){ return a + b + c;
}
}//定义一个类Square,继承Rectangle类,并重写构造方法和toString()方法class Square extends Rectangle { //重写构造方法,在调用父类构造方法时传入相同的参数side作为width和height
public Square(double side){ super(side, side);
} //重写toString()方法,在原来基础上加上"Square:"前缀,并只显示side属性而不显示width和height属性(使用Stringformat()函数格式化字符串)
@Override
public String toString(){ return Stringformat("Square: side=%2f", superwidth); / 或者直接使用supergetPerimeter()/4作为side /
/ return Stringformat("Square: side=%2f", supergetPerimeter()/4); /
/ 注意:不能直接访问superside属性,
欢迎分享,转载请注明来源:浪漫分享网
评论列表(0条)