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

熱點內容
shell編輯文件內容 瀏覽:614
u盤驅動在哪個文件夾里 瀏覽:938
華為手機跟蘋果手機怎麼克隆app 瀏覽:172
cad文件為什麼顯示文件名是否正確 瀏覽:174
程序配置文件能自動轉換成txt 瀏覽:985
r4燒錄卡不同版本 瀏覽:962
怎麼升級qq群為2000 瀏覽:347
微信己冊除的文件怎樣找回來 瀏覽:715
蘋果美國賬號共享2017 瀏覽:252
ps文件修改後打開還是原圖 瀏覽:220
燒卡機蘋果4微信qq哪裡下載 瀏覽:780
ug120找不到指定文件 瀏覽:522
cda文件怎麼轉換成mp3格式 瀏覽:702
青島hpv疫苗在哪個app預約 瀏覽:881
雙十一成交額2021數據在哪裡看 瀏覽:631
蘋果手機怎麼打開移動數據 瀏覽:637
文件掃描成圖片是彩色的嗎 瀏覽:50
編程里的comply什麼意思 瀏覽:91
電腦上面點了禁用網路怎麼改回來 瀏覽:28
javacatch執行 瀏覽:182

友情鏈接