1.用C++编写的小游戏源代码
2.在jsp页面中怎样用javaScript检查多选框是否被选中?
3.学生考试管理系统,JAva源代码
4.java编译文本编辑器
用C++编写的小游戏源代码
五子棋的代码:#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <time.h>
using namespace std;
const int N=; //*的棋盘
const char ChessBoardflag = ' '; //棋盘标志
const char flag1='o'; //玩家1或电脑的棋子标志
const char flag2='X'; //玩家2的棋子标志
typedef struct Coordinate //坐标类
{
int x; //代表行
int y; //代表列
}Coordinate;
class GoBang //五子棋类
{
public:
GoBang() //初始化
{
InitChessBoard();
}
void Play() //下棋
{
Coordinate Pos1; // 玩家1或电脑
Coordinate Pos2; //玩家2
int n = 0;
while (1)
{
int mode = ChoiceMode();
while (1)
{
if (mode == 1) //电脑vs玩家
{
ComputerChess(Pos1,flag1); // 电脑下棋
if (GetVictory(Pos1, 0, flag1) == 1) //0表示电脑,真表示获胜
break;
PlayChess(Pos2, 2, flag2); //玩家2下棋
if (GetVictory(Pos2, 2, flag2)) //2表示玩家2
break;
}
else //玩家1vs玩家2
{
PlayChess(Pos1, 1, flag1); // 玩家1下棋
if (GetVictory(Pos1, 1, flag1)) //1表示玩家1
break;
PlayChess(Pos2, 2, flag2); //玩家2下棋
if (GetVictory(Pos2, 2, flag2)) //2表示玩家2
break;
}
}
cout << "***再来一局***" << endl;
cout << "y or n :";
char c = 'y';
cin >> c;
if (c == 'n')
break;
}
}
protected:
int ChoiceMode() //选择模式
{
int i = 0;
system("cls"); //系统调用,清屏
InitChessBoard(); //重新初始化棋盘
cout << "***0、退出 1、开源app源码github电脑vs玩家 2、玩家vs玩家***" << endl;
while (1)
{
cout << "请选择:";
cin >> i;
if (i == 0) //选择0退出
exit(1);
if (i == 1 || i == 2)
return i;
cout << "输入不合法" << endl;
}
}
void InitChessBoard() //初始化棋盘
{
for (int i = 0; i < N + 1; ++i)
{
for (int j = 0; j < N + 1; ++j)
{
_ChessBoard[i][j] = ChessBoardflag;
}
}
}
void PrintChessBoard() //打印棋盘,这个函数可以自己调整
{
system("cls"); //系统调用,清空屏幕
for (int i = 0; i < N+1; ++i)
{
for (int j = 0; j < N+1; ++j)
{
if (i == 0) //打印列数字
{
if (j!=0)
printf("%d ", j);
else
printf(" ");
}
else if (j == 0) //打印行数字
printf("%2d ", i);
else
{
if (i < N+1)
{
printf("%c |",_ChessBoard[i][j]);
}
}
}
cout << endl;
cout << " ";
for (int m = 0; m < N; m++)
{
printf("--|");
}
cout << endl;
}
}
void PlayChess(Coordinate& pos, int player, int flag) //玩家下棋
{
PrintChessBoard(); //打印棋盘
while (1)
{
printf("玩家%d输入坐标:", player);
cin >> pos.x >> pos.y;
if (JudgeValue(pos) == 1) //坐标合法
break;
cout << "坐标不合法,重新输入" << endl;
}
_ChessBoard[pos.x][pos.y] = flag;
}
void ComputerChess(Coordinate& pos, char flag) //电脑下棋
{
PrintChessBoard(); //打印棋盘
int x = 0;
int y = 0;
while (1)
{
x = (rand() % N) + 1; //产生1~N的随机数
srand((unsigned int) time(NULL));
y = (rand() % N) + 1; //产生1~N的随机数
srand((unsigned int) time(NULL));
if (_ChessBoard[x][y] == ChessBoardflag) //如果这个位置是空的,也就是没有棋子
break;
}
pos.x = x;
pos.y = y;
_ChessBoard[pos.x][pos.y] = flag;
}
int JudgeValue(const Coordinate& pos) //判断输入坐标是不是合法
{
if (pos.x > 0 && pos.x <= N&&pos.y > 0 && pos.y <= N)
{
if (_ChessBoard[pos.x][pos.y] == ChessBoardflag)
{
return 1; //合法
}
}
return 0; //非法
}
int JudgeVictory(Coordinate pos, char flag) //判断有没有人胜负(底层判断)
{
int begin = 0;
int end = 0;
int begin1 = 0;
int end1 = 0;
//判断行是否满足条件
(pos.y - 4) > 0 ? begin = (pos.y - 4) : begin = 1;
(pos.y + 4) >N ? end = N : end = (pos.y + 4);
for (int i = pos.x, j = begin; j + 4 <= end; j++)
{
if (_ChessBoard[i][j] == flag&&_ChessBoard[i][j + 1] == flag&&
_ChessBoard[i][j + 2] == flag&&_ChessBoard[i][j + 3] == flag&&
_ChessBoard[i][j + 4] == flag)
return 1;
}
//判断列是否满足条件
(pos.x - 4) > 0 ? begin = (pos.x - 4) : begin = 1;
(pos.x + 4) > N ? end = N : end = (pos.x + 4);
for (int j = pos.y, i = begin; i + 4 <= end; i++)
{
if (_ChessBoard[i][j] == flag&&_ChessBoard[i + 1][j] == flag&&
_ChessBoard[i + 2][j] == flag&&_ChessBoard[i + 3][j] == flag&&
_ChessBoard[i + 4][j] == flag)
return 1;
}
int len = 0;
//判断主对角线是否满足条件
pos.x > pos.y ? len = pos.y - 1 : len = pos.x - 1;
if (len > 4)
len = 4;
begin = pos.x - len; //横坐标的起始位置
begin1 = pos.y - len; //纵坐标的起始位置
pos.x > pos.y ? len = (N - pos.x) : len = (N - pos.y);
if (len>4)
len = 4;
end = pos.x + len; //横坐标的结束位置
end1 = pos.y + len; //纵坐标的结束位置
for (int i = begin, j = begin1; (i + 4 <= end) && (j + 4 <= end1); ++i, ++j)
{
if (_ChessBoard[i][j] == flag&&_ChessBoard[i + 1][j + 1] == flag&&
_ChessBoard[i + 2][j + 2] == flag&&_ChessBoard[i + 3][j + 3] == flag&&
_ChessBoard[i + 4][j + 4] == flag)
return 1;
}
//判断副对角线是否满足条件
(pos.x - 1) >(N - pos.y) ? len = (N - pos.y) : len = pos.x - 1;
if (len > 4)
len = 4;
begin = pos.x - len; //横坐标的起始位置
begin1 = pos.y + len; //纵坐标的起始位置
(N - pos.x) > (pos.y - 1) ? len = (pos.y - 1) : len = (N - pos.x);
if (len>4)
len = 4;
end = pos.x + len; //横坐标的结束位置
end1 = pos.y - len; //纵坐标的结束位置
for (int i = begin, j = begin1; (i + 4 <= end) && (j - 4 >= end1); ++i, --j)
{
if (_ChessBoard[i][j] == flag&&_ChessBoard[i + 1][j - 1] == flag&&
_ChessBoard[i + 2][j - 2] == flag&&_ChessBoard[i + 3][j - 3] == flag&&
_ChessBoard[i + 4][j - 4] == flag)
return 1;
}
for (int i = 1; i < N + 1; ++i) //棋盘有没有下满
{
for (int j =1; j < N + 1; ++j)
{
if (_ChessBoard[i][j] == ChessBoardflag)
return 0; //0表示棋盘没满
}
}
return -1; //和棋
}
bool GetVictory(Coordinate& pos, int player, int flag) //对JudgeVictory的一层封装,得到具体那个玩家获胜
{
int n = JudgeVictory(pos,docker 源码数量 flag); //判断有没有人获胜
if (n != 0) //有人获胜,0表示没有人获胜
{
PrintChessBoard();
if (n == 1) //有玩家赢棋
{
if (player == 0) //0表示电脑获胜,1表示玩家1,2表示玩家2
printf("***电脑获胜***\n");
else
printf("***恭喜玩家%d获胜***\n", player);
}
else
printf("***双方和棋***\n");
return true; //已经有人获胜
}
return false; //没有人获胜
}
private:
char _ChessBoard[N+1][N+1];
};
扩展资料:
设计思路
1、进行问题分析与设计,计划实现的功能为,开局选择人机或双人对战,vue源码解析 网站确定之后比赛开始。
2、比赛结束后初始化棋盘,询问是否继续比赛或退出,后续可加入复盘、未来漫画系统源码悔棋等功能。
3、整个过程中,涉及到了棋子和棋盘两种对象,同时要加上人机对弈时的tractor for android 源码AI对象,即涉及到三个对象。
在jsp页面中怎样用javaScript检查多选框是否被选中?
var chk=document.getElementsByTagName("input");
for(var i=0,len=chk.length;i<len;i++){
if(chk[i].type=="checkbox"){
if(chk[i].checked){
alert(chk[i].id+"被选中了");
}
}
}
这样就能得到所有的chkckbox了.
如果只是在某个范围内,可以这样
如
<div id="cc">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
</div>
则可以这样得到div内的所有checkbox
var arr=document.getElementById("cc").getElementsByTagName("input");
for(var i=0,len=chk.length;i<len;i++){
if(chk[i].type=="checkbox"){
if(chk[i].checked){
alert(chk[i].id+"被选中了");
}
}
}
给你最外层的一个Table指定一个ID就可以了。如:给一个叫TblContainer的ID,可以这样用。
var arr=document.getElementById("TblContainer").getElementsByTagName("input");
for(var i=0,len=chk.length;i<len;i++){
if(chk[i].type=="checkbox"){
if(chk[i].checked){
alert(chk[i].id+"被选中了");
}
}
}
学生考试管理系统,JAva源代码
//主类EnglishTest——
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class EnglishTest extends JFrame
{
TestArea testPanel=null;
Container con=null;
public EnglishTest()
{
super("模拟考试");
testPanel=new TestArea();
con=getContentPane();
con.add(testPanel,BorderLayout.CENTER);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
});
setVisible(true);
setBounds(,,,);
con.validate();
validate();
}
public static void main(String args[])
{
new EnglishTest();
}
}
//读取试题 ReadTestquestion
import java.io.*;
import java.util.*;
public class ReadTestquestion
{ String filename="",
correctAnswer="",
testContent="" ,
selection="" ;
int score=0;
long time=0;
boolean 完成考试=false;
File f=null;
FileReader in=null;
BufferedReader 读取=null;
public void setFilename(String name)
{ filename=name;
score=0;
selection="";
try {
if(in!=null&&读取!=null)
{
in.close();
读取.close();
}
f=new File(filename);
in=new FileReader(f);
读取=new BufferedReader(in);
correctAnswer=(读取.readLine()).trim();
String temp=(读取.readLine()).trim() ;
StringTokenizer token=new StringTokenizer(temp,":");
int hour=Integer.parseInt(token.nextToken()) ;
int minute=Integer.parseInt(token.nextToken());
int second=Integer.parseInt(token.nextToken());
time=*(second+minute*+hour**);
}
catch(Exception e)
{
testContent="没有选择试题";
}
}
public String getFilename()
{
return filename;
}
public long getTime()
{
return time;
}
public void set完成考试(boolean b)
{
完成考试=b;
}
public boolean get完成考试()
{
return 完成考试;
}
public String getTestContent()
{ try {
String s=null;
StringBuffer temp=new StringBuffer();
if(读取!=null)
{
while((s=读取.readLine())!=null)
{
if(s.startsWith("**"))
break;
temp.append("\n"+s);
if(s.startsWith("endend"))
{
in.close();
读取.close();
完成考试=true;
}
}
testContent=new String(temp);
}
else
{
testContent=new String("没有选择试题");
}
}
catch(Exception e)
{
testContent="试题内容为空,考试结束!!";
}
return testContent;
}
public void setSelection(String s)
{
selection=selection+s;
}
public int getScore()
{ score=0;
int length1=selection.length();
int length2=correctAnswer.length();
int min=Math.min(length1,length2);
for(int i=0;i<min;i++)
{ try{
if(selection.charAt(i)==correctAnswer.charAt(i))
score++;
}
catch(StringIndexOutOfBoundsException e)
{
i=0;
}
}
return score;
}: -8-
public String getMessages()
{
int length1=selection.length();
int length2=correctAnswer.length();
int length=Math.min(length1,length2);
String message="正确答案:"+correctAnswer.substring(0,length)+"\n"+
"你的回答:"+selection+"\n";
return message;
}
}
//考试区域TestArea
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
class FileName implements FilenameFilter
{
String str=null;
FileName (String s)
{
str="."+s;
}
public boolean accept(File dir,String name)
{
return name.endsWith(str);
}
}
public class TestArea extends JPanel implements ActionListener,ItemListener,Runnable
{
Choice list=null;
JTextArea 试题显示区=null,消息区=null;
JCheckBox box[];
JButton 提交该题答案,读取下一题,查看分数;
ReadTestquestion 读取试题=null;
JLabel welcomeLabel=null;
Thread countTime=null;
long time=0;
JTextField timeShow=null;
boolean 是否关闭计时器=false,
是否暂停计时=false;
JButton 暂停或继续计时=null;
public TestArea()
{
list= new Choice();
String 当前目录=System.getProperty("user.dir");
File dir=new File(当前目录);
FileName fileTxt=new FileName("txt");
String fileName[]=dir.list(fileTxt);
for(int i=0;i<fileName.length;i++)
{
list.add(fileName[i]);
}
试题显示区=new JTextArea(,);
试题显示区.setLineWrap(true);
试题显示区.setWrapStyleWord(true);
试题显示区.setFont(new Font("TimesRoman",Font.PLAIN,));
试题显示区.setForeground(Color.blue);
消息区=new JTextArea(8,8);
消息区.setForeground(Color.blue);
消息区.setLineWrap(true);
消息区.setWrapStyleWord(true);
countTime=new Thread(this);
String s[]={ "A","B","C","D"};
box=new JCheckBox[4];
for(int i=0;i<4;i++)
{
box[i]=new JCheckBox(s[i]);
}
暂停或继续计时=new JButton("暂停计时");
暂停或继续计时.addActionListener(this);
提交该题答案=new JButton("提交该题答案");
读取下一题=new JButton("读取第一题");
读取下一题.setForeground(Color.blue);
提交该题答案.setForeground(Color.blue);
查看分数=new JButton("查看分数");
查看分数.setForeground(Color.blue);
提交该题答案.setEnabled(false);
提交该题答案.addActionListener(this);
读取下一题.addActionListener(this);
查看分数.addActionListener(this);
list.addItemListener(this);
读取试题=new ReadTestquestion();
JPanel pAddbox=new JPanel();
for(int i=0;i<4;i++)
{
pAddbox.add(box[i]);
}
Box boxH1=Box.createVerticalBox(),
boxH2=Box.createVerticalBox(),
baseBox=Box.createHorizontalBox();
boxH1.add(new JLabel("选择试题文件"));
boxH1.add(list);
boxH1.add(new JScrollPane(消息区));
boxH1.add(查看分数);
timeShow=new JTextField();
timeShow.setHorizontalAlignment(SwingConstants.RIGHT);
timeShow.setEditable(false);
JPanel p1=new JPanel();
p1.add(new JLabel("剩余时间:"));
p1.add(timeShow);
p1.add(暂停或继续计时);
boxH1.add(p1);
boxH2.add(new JLabel("试题内容:"));
boxH2.add(new JScrollPane(试题显示区));
JPanel p2=new JPanel();
p2.add(pAddbox);
p2.add(提交该题答案);
p2.add(读取下一题);
boxH2.add(p2);
baseBox.add(boxH1);
baseBox.add(boxH2);
setLayout(new BorderLayout());
add(baseBox,BorderLayout.CENTER);
welcomeLabel=new JLabel("欢迎考试,提高英语水平",JLabel.CENTER);
welcomeLabel.setFont(new Font("隶书",Font.PLAIN,));
welcomeLabel.setForeground(Color.blue);
add(welcomeLabel,BorderLayout.NORTH);
}
public void itemStateChanged(ItemEvent e)
{
timeShow.setText(null);
是否关闭计时器=false;
是否暂停计时=false;
暂停或继续计时.setText("暂停计时");
String name=(String)list.getSelectedItem();
读取试题.setFilename(name);
读取试题.set完成考试(false);
time=读取试题.getTime();
if(countTime.isAlive())
{
是否关闭计时器=true;
countTime.interrupt();
}
countTime=new Thread(this);
消息区.setText(null);
试题显示区.setText(null);
读取下一题.setText("读取第一题");
提交该题答案.setEnabled(false);
读取下一题.setEnabled(true);
welcomeLabel.setText("欢迎考试,你选择的试题:"+读取试题.getFilename());
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==读取下一题)
{
读取下一题.setText("读取下一题");
提交该题答案.setEnabled(true);
String contentTest=读取试题.getTestContent();
试题显示区.setText(contentTest);
消息区.setText(null);
读取下一题.setEnabled(false);
try {
countTime.start();
}
catch(Exception event)
{
}
}
if(e.getSource()==提交该题答案)
{
读取下一题.setEnabled(true);
提交该题答案.setEnabled(false);
String answer="?";
for(int i=0;i<4;i++)
{
if(box[i].isSelected())
{
answer=box[i].getText();
box[i].setSelected(false);
break;
}
}
读取试题.setSelection(answer);
}
if(e.getSource()==查看分数)
{
int score=读取试题.getScore();
String messages=读取试题.getMessages();
消息区.setText("分数:"+score+"\n"+messages);
}
if(e.getSource()==暂停或继续计时)
{
if(是否暂停计时==false)
{
暂停或继续计时.setText("继续计时");
是否暂停计时=true;
}
else if(是否暂停计时==true)
{
暂停或继续计时.setText("暂停计时");
是否暂停计时=false;
countTime.interrupt();
}
}
}
public synchronized void run()
{
while(true)
{
if(time<=0)
{
是否关闭计时器=true;
countTime.interrupt();
提交该题答案.setEnabled(false);
读取下一题.setEnabled(false);
timeShow.setText("用时尽,考试结束");
}
else if(读取试题.get完成考试())
{
是否关闭计时器=true;
timeShow.setText("考试效果:分数*剩余时间(秒)="+1.0*读取试题.getScore()*(time/));
countTime.interrupt();
提交该题答案.setEnabled(false);
读取下一题.setEnabled(false);
}
else if(time>=1)
{
time=time-;
long leftTime=time/;
long leftHour=leftTime/;
long leftMinute=(leftTime-leftHour*)/;
long leftSecond=leftTime%;
timeShow.setText(""+leftHour+"小时"+leftMinute+"分"+leftSecond+"秒");
}
try
{
Thread.sleep();
}
catch(InterruptedException ee)
{
if(是否关闭计时器==true)
return ;
}
while(是否暂停计时==true)
{
try
{
wait();
}
catch(InterruptedException ee)
{
if(是否暂停计时==false)
{
notifyAll();
}
}
}
}
}
}
java编译文本编辑器
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class EditorJFrame extends JFrame implements ActionListener, ItemListener, MouseListener
{
private JTextField text_size; //字号文本行
private JCheckBox checkbox_bold, checkbox_italic; //粗体、斜体复选框
private JButton button_cut, button_copy, button_paste; //剪切、复制、粘贴按钮
private JTextArea textarea; //文本区
private JPopupMenu popupmenu; //快捷菜单
private JDialog dialog; //出错提示对话框
private JLabel label_dialog; //对话框中的标签
public EditorJFrame()
{
super("文本编辑器"); //默认BorderLayout布局
this.setSize(,);
this.setLocation(,);
this.setDefaultCloseOperation(EXIT_ON_CLOSE); //单击窗口关闭按钮时,结束程序运行
textarea = new JTextArea("TextArea");
textarea.addMouseListener(this); //为文本区注册鼠标事件监听器
this.add(textarea); //文本区添加到框架的中部
JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT)); //面板为流布局,左对齐
this.add(panel,"North"); //面板添加到框架的北部
text_size = new JTextField("",);
panel.add(text_size);
text_size.addActionListener(this); //注册文本行的单击事件监听器
checkbox_bold = new JCheckBox("粗体"); //复选框
panel.add(checkbox_bold);
checkbox_bold.addItemListener(this); //注册复选框的选择事件监听器
checkbox_italic = new JCheckBox("斜体");
panel.add(checkbox_italic);
checkbox_italic.addItemListener(this);
this.addmyMenu(); //调用自定义方法,添加菜单
this.setVisible(true);
}
private void addmyMenu() //添加主菜单、快捷菜单、对话框
{
JMenuBar menubar = new JMenuBar(); //菜单栏
this.setJMenuBar(menubar); //框架上添加菜单栏
JMenu menu_file = new JMenu("文件"); //菜单
menubar.add(menu_file); //菜单栏中加入菜单
menu_file.add(new JMenuItem("打开")); //生成菜单项并加入到菜单
menu_file.add(new JMenuItem("保存"));
menu_file.addSeparator(); //加分隔线
JMenuItem menuitem_exit = new JMenuItem("退出");
menu_file.add(menuitem_exit);
menuitem_exit.addActionListener(this); //为菜单项注册单击事件监听器
JMenu menu_edit = new JMenu("编辑");
menubar.add(menu_edit);
JMenu menu_style = new JMenu("字形");
menu_style.add(new JCheckBoxMenuItem("粗体")); //复选菜单项
menu_style.add(new JCheckBoxMenuItem("斜体"));
menu_edit.add(menu_style); //菜单加入到菜单中成为二级菜单
JMenu menu_color = new JMenu("颜色");
menu_edit.add(menu_color);
ButtonGroup buttongroup = new ButtonGroup(); //按钮组
JRadioButtonMenuItem rbmi_red = new JRadioButtonMenuItem("红",true); //单选菜单项
buttongroup.add(rbmi_red); //单选菜单项添加到按钮组
menu_color.add(rbmi_red); //单选菜单项添加到菜单
JRadioButtonMenuItem rbmi_green = new JRadioButtonMenuItem("绿",true);
buttongroup.add(rbmi_green);
menu_color.add(rbmi_green);
JRadioButtonMenuItem rbmi_blue = new JRadioButtonMenuItem("蓝",true);
buttongroup.add(rbmi_blue);
menu_color.add(rbmi_blue);
menubar.add(new JMenu("帮助"));
popupmenu = new JPopupMenu(); //弹出式菜单对象
JMenuItem menuitem_cut = new JMenuItem("剪切");
menuitem_cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK));//设置快捷键Ctrl+X
popupmenu.add(menuitem_cut); //加入剪切菜单项
menuitem_cut.addActionListener(this);
JMenuItem menuitem_copy = new JMenuItem("复制");
menuitem_copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));//设置快捷键Ctrl+C
popupmenu.add(menuitem_copy);
menuitem_copy.addActionListener(this);
JMenuItem menuitem_paste = new JMenuItem("粘贴");
menuitem_paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK));//设置快捷键Ctrl+V
popupmenu.add(menuitem_paste);
menuitem_paste.addActionListener(this);
textarea.add(popupmenu); //文本区添加快捷菜单
dialog = new JDialog(this,"提示");
dialog.setSize(,);
label_dialog = new JLabel("",JLabel.CENTER);
dialog.add(label_dialog);
dialog.setDefaultCloseOperation(HIDE_ON_CLOSE); //单击对话框的关闭按钮时,隐藏对话框而不结束程序运行
}
public void actionPerformed(ActionEvent e) //单击事件处理程序
{
if(e.getActionCommand()=="退出") //不能用switch(int)语句
System.exit(0); //单击菜单项时结束程序
if(e.getActionCommand()=="剪切")
textarea.cut(); //将选中文本剪切送系统剪贴板
if(e.getActionCommand()=="复制")
textarea.copy();
if(e.getActionCommand()=="粘贴")
textarea.paste();
if(e.getSource()==text_size) //单击文本行时,改变字号
{
int size=0;
try
{
size = Integer.parseInt(text_size.getText());
if (size<=0 || size>)
throw new Exception("SizeException"); //抛出异常对象
java.awt.Font font = textarea.getFont();
textarea.setFont(new Font(font.getName(),font.getStyle(),size));
}
catch(NumberFormatException nfe)
{
label_dialog.setText("\""+text_size.getText()+"\" 不能转换成整数,请重新输入!");
dialog.setLocation(this.getX()+,this.getY()+);
dialog.setVisible(true);
}
catch(Exception ex)
{
if (ex.getMessage()=="SizeException") //捕获自己抛出的异常对象
{
label_dialog.setText(size+" 字号不合适,请重新输入!");
dialog.setLocation(this.getX()+,this.getY()+);
dialog.setVisible(true);
}
}
finally{ }
}
}
public void itemStateChanged(ItemEvent e) //复选框选择事件处理程序
{ //实现ItemListener接口中的方法
Font font = textarea.getFont();
int style = font.getStyle();
if (e.getSource()==checkbox_bold)
style = style ^ 1; //整数的位运算,异或^
if (e.getSource()==checkbox_italic)
style = style ^ 2;
textarea.setFont(new Font(font.getName(),style,font.getSize()));
}
public void mouseClicked(MouseEvent mec) //单击鼠标时触发
{ //实现MouseListener接口中的方法
if (mec.getModifiers()==mec.BUTTON3_MASK) //单击的是鼠标右键
popupmenu.show(textarea,mec.getX(),mec.getY());//在鼠标单击处显示快捷菜单
}
public void mousePressed(MouseEvent mep) { }
public void mouseReleased(MouseEvent mer) { }
public void mouseEntered(MouseEvent mee) { }
public void mouseExited(MouseEvent mex) { }
public void mouseDragged(MouseEvent med) { }
public static void main(String arg[])
{
new EditorJFrame();
}
}