9. 實(shí)現(xiàn)下面的圖形化界面,完成以下功能:
1. 點(diǎn)擊”open file” 按鈕,打開文件對(duì)話框,選擇文件,在文本框中輸出所選文件的路徑:
2. 點(diǎn)擊”save file” 按鈕,打開文件對(duì)話框,在對(duì)話框中輸入文件名,并在文件框中輸出保存文件的路徑.
請(qǐng)?jiān)趧澗處編寫適當(dāng)?shù)恼Z(yǔ)句,使程序能正確執(zhí)行.
Import javax.swing.JfileChooser;
Import javax.swing.JTextArea;
Import javax.swing.JButton;
Import javax.swing.JFram;
Import javax.swing.JPanel;
Import Java.awt.event.*;
Import java.io.*;
Import java.awt.*;
Public class swingExamples implements ActionListener
{
Private Jframe frame;
Private Jbutton button;
Private Jbutton saveButton;
Private Jtextrea textArea;
Private JfileChoose dia;
Private Jpanel buttonPanel;
Public void initGui()
{
Frame=new JFrame(“file chooser”);
Button=new Jbutton(“open file”);
Button.setActionCommand(“open”);
Button.addActionListener(this);
saveButton=new Jbutton(“save file”);
_____________________________;
saveButton.addActionListener(this);
textArea=new JtextArea(“”,10.,10);
buttonPanel=new Jpanel();
dia=new JfileChooser();
frame.addwindowListener(new windowAdapter()
{
Public void windowClosing (WindowEvent e)
{
System.exit(0);
}
});
ButtonPanel.add(button);
ButtonPanel.add(saveButton);
Frame.getContentPane().add(buttonPanel,BorderLayout.NORTH);
Frame.getContentPane().add(textArea,BorderLayout.CENTER);
Frame.setSize(200,200);
Frame.setVisible(true);
}
Public void actionPerformed(ActionEvent event)
{
If(event.getActionCommand().equals(“open”))
{
Dia.showOpenDialog(frame);
Dia.setVisible(true);
File file=dia.getSelectedFilePath();
textArea.append(“path of selected file:”+fileName+”\r\n”);
}
Else if(event.getActionCommand().equals(“save”))
{
Dia.showSaveDialog(frame);
Dia.setVisible(true);
File file=dia.getSelectedFile();
String fileName=file.getAbsolutePath();
textArea.append(“path of saved file:”+fileName+”\r\n”);
}
}
Public static void main(string arqs[])
{
SwingExample3 example=new SwingExample3();
Example.initGUI();
}
}
答案:saveButton.setActionCommand(“save”)
10. 實(shí)現(xiàn)下圖中的界面,完成功能;點(diǎn)擊鼠標(biāo)時(shí),在文本框中輸出鼠標(biāo)點(diǎn)擊的位置,點(diǎn)擊次數(shù)和點(diǎn)擊的鍵(左鍵或右鍵).請(qǐng)?jiān)趧澗處編寫適當(dāng)?shù)恼Z(yǔ)句,使程序能夠正確執(zhí)行.
Import java.awt.*;
Import java.awt.event.*;
Public class AWTExample6 extends MouseaAdapter
{
Private Frame frame;
Private TextArea textArea;
Private MenuItem about;
Private MenuItem exit;
Private MenuItem popupMenu;
Private Panel panel;
Public void initGUI()
{
Frame=new Frame(“mouse event”);
textArea=new TextArea(“”,10,10);
_______________________________
Frame.add(textArea);
Frame.setSize(200,200);
Frame.setVisible(true);
Frame.addWindowListener(new WindowAdapter()
{
Public void windowClosing(windowEvent e)
{System.exit(0);
}
});
}
Public void mousePressed(MouseEvent event)
{
If(event.getModifiers()==event.BUTTON1_MASK)
{textArea.append(“點(diǎn)擊坐鍵”+”\r\n”);
textArea.append(“點(diǎn)擊位置:x=”+event.getX()+”y=”+event.getY()+”\r\n”);
textArea.append(“點(diǎn)擊次數(shù)”+event.getClickCout()+”\r\n”);
}
Else if (event.getModifiers()==event.BUTTON3_MASK)
{
textArea.append(“點(diǎn)擊右鍵"+”\r\n”);
textArea.append(“點(diǎn)擊位置:x=”+event.getX()+”y=”+event.getY()+”\r\n”;
textArea.append(“點(diǎn)擊次數(shù)”+event.getClickCout()+”\r\n”);
}
}
Public static void main(string args[])
{
AWTEXAMple6 example-new AWTExample6();
Example.initGUI();
}
}
答案:textArea.addMouseListener(this);
相關(guān)推薦:北京 | 天津 | 上海 | 江蘇 | 山東 |
安徽 | 浙江 | 江西 | 福建 | 深圳 |
廣東 | 河北 | 湖南 | 廣西 | 河南 |
海南 | 湖北 | 四川 | 重慶 | 云南 |
貴州 | 西藏 | 新疆 | 陜西 | 山西 |
寧夏 | 甘肅 | 青海 | 遼寧 | 吉林 |
黑龍江 | 內(nèi)蒙古 |