导航:首页 > 编程语言 > javagui添加文字

javagui添加文字

发布时间:2023-01-27 02:30:55

『壹』 java的GUI图形用户界面加上注释还有加上一个组合按钮关闭程序

importjava.awt.*;
importjava.awt.event.*;
importjava.applet.Applet;
//Applet类实现接口键盘事件响应器
publicclassExa6_{
Buttonb;

publicvoidinit(){
setLayout(newFlowLayout());//流布局
b=newButton("组合键改变按钮位置");//创建一个按钮对象
b.addKeyListener(this);//添加键盘事件响应器
add(b);//加入到程序中
}

publicvoidkeyPressed(KeyEvente){//键盘按下事件,当按键被按下时响应
Buttonbutton=(Button)e.getSource();
intx=0,y=0,w=0,h=0;//x,y代表位置w,h代表宽和高
x=button.getBounds().x;//取得按钮的x坐标
y=button.getBounds().y;//取得按钮的y坐标
w=button.getBounds().width;//取得按钮的宽
h=button.getBounds().height;//取得按钮的高
if(e.getModifiers()==InputEvent.SHIFT_MASK&&e.getKeyCode()==KeyEvent.VK_X){//当键盘同时按下SHIFT和X键时
button.setLocation(y,x);//把按钮的原位置x,y变化成为y,x位置
button.setLabel("组合键SHIFT_X");//按钮上的文字改变成为组合键SHIFT_X
}elseif(e.getModifiers()==InputEvent.CTRL_MASK&&e.getKeyCode()==KeyEvent.VK_X){//当键盘同时按下CTRL和X键时
button.setLocation(h,w);//把按钮的高h,宽w设置成为按钮的位置
button.setLabel("组合键CTRL_X");//按钮上的文字改变成为组合键CTRL_X
}elseif(e.getModifiers()==InputEvent.ALT_MASK&&e.getKeyCode()==KeyEvent.VK_X){//当键盘同时按下ALT和X键时
System.exit(0);//系统退出
}
}

publicvoidkeyTyped(KeyEvente){//(点击按钮)键盘按下,然后释放。
}

publicvoidkeyReleased(KeyEvente){//按键释放,弹起
}
}

『贰』 用JAVA编写一个GUI记事本程序,实现文本的输入,保存,修改,打开操作

代码如下:

importjava.io.*;
importjava.awt.*;
importjava.awt.event.*;

publicclassjtxtfm{
publicstaticvoidmain(Stringargs[]){
jtxtfrmfm=newjtxtfrm();
}
}
{
FileDialogop,sv;
Buttonbtn1,btn2,btn3;
TextAreatarea;
jtxtfrm(){
super("读写文件");
setLayout(null);
setBackground(Color.cyan);
setSize(600,300);
setVisible(true);
btn1=newButton("打开");
btn2=newButton("保存");
btn3=newButton("关闭");
tarea=newTextArea("");
add(btn1);add(btn2);add(btn3);add(tarea);
tarea.setBounds(30,50,460,220);
btn1.setBounds(520,60,50,30);
btn2.setBounds(520,120,50,30);
btn3.setBounds(520,180,50,30);
op=newFileDialog(this,"打开",FileDialog.LOAD);
sv=newFileDialog(this,"保存",FileDialog.SAVE);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
addWindowListener(
newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
setVisible(false);
System.exit(0);
}
}
);
}

publicvoidactionPerformed(ActionEvente){
if(e.getSource()==btn1){
Stringstr;
op.setVisible(true);
try{
Filef1=newFile(op.getDirectory(),op.getFile());
FileReaderfr=newFileReader(f1);
BufferedReaderbr=newBufferedReader(fr);
tarea.setText("");
while((str=br.readLine())!=null)tarea.append(str+' ');
fr.close();
}
catch(Exceptione1)
{}
}

if(e.getSource()==btn2){
sv.setVisible(true);
try{
Filef1=newFile(sv.getDirectory(),sv.getFile());
FileWriterfw=newFileWriter(f1);
BufferedWriterbw=newBufferedWriter(fw);
Stringgt=tarea.getText();
bw.write(gt,0,gt.length());
bw.flush();
fw.close();
}
catch(Exceptione2)
{}
}

if(e.getSource()==btn3){
System.exit(0);
}

}
}

效果图:

『叁』 Java GUI中怎么把JtextPanel中的文本写入到指定的文件中用那个方法

exec >指定日志,作用整个shell
单命令重定向只作用于那个单命令

阅读全文

与javagui添加文字相关的资料

热点内容
qq怎么清楚历史记录防止被盗 浏览:263
发送手机里的录音文件在哪里 浏览:866
js获取下一个兄弟元素 浏览:293
js模板引擎原理 浏览:72
linuxo文件运行 浏览:713
什么免费备份数据 浏览:342
测量大师导入底图找不到文件 浏览:313
小红伞安装程序要求版本6 浏览:799
全民k歌pcm文件夹 浏览:224
c图片写入数据库 浏览:466
c串口程序 浏览:500
中农金融注册app有哪些 浏览:629
回收站文件清理不在电脑上有痕迹 浏览:886
dx版本更新 浏览:738
主机集群教程 浏览:939
苹果6英版好不好 浏览:959
nodejs抓取网站音频 浏览:772
app上下载的软件在哪里 浏览:36
起凡保存的照片在哪个文件夹 浏览:354
数学建模如何把模型编程 浏览:176

友情链接