導航:首頁 > 編程語言 > 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添加文字相關的資料

熱點內容
cad的列印到文件怎麼設置 瀏覽:902
智行app鉑金會員怎麼還有期限 瀏覽:581
win10用子文件夾改名 瀏覽:234
ae鋼筆工具在哪裡 瀏覽:460
gn105數據線插哪裡 瀏覽:916
破鎖屏密碼方法 瀏覽:835
股票數據放哪裡 瀏覽:576
m格式庫文件 瀏覽:279
天際通數據服務怎麼開票 瀏覽:430
寫小說發哪個網站比較好 瀏覽:244
小米電視3藍牙文件路徑 瀏覽:111
shell讀取文件值 瀏覽:909
文件夾路徑欄消失 瀏覽:795
律師哪些業務不能代替大數據 瀏覽:952
lol哪些文件可以刪除 瀏覽:701
匯編程序中del是什麼意思 瀏覽:183
幼兒園免費網站模板下載 瀏覽:210
w619線刷教程 瀏覽:759
怎麼培養編程思想 瀏覽:697
手機捆綁app的軟體怎麼卸載 瀏覽:32

友情鏈接