java gui 读取文件夹_java Swing GUI 入门-文件读写器
java Swing GUI 入門-文件讀寫器
覺得有用的話,歡迎一起討論相互學(xué)習(xí)~
首先創(chuàng)建一個(gè)獨(dú)立的窗口
public CoupPad(){}
public static void main(String[] args) {
CoupPad window = new CoupPad();
window.setSize(400, 200);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}//end main
精細(xì)化窗口
需要使用Contariner容器向其中添加組件
容器Container是一個(gè)類,實(shí)際上是Component的子類,因此容器本身也是一個(gè)組件,具有組件的所有性質(zhì),但它的主要內(nèi)容是容納其他組件和容器,在其可視區(qū)顯示這些組件。容器的各種的組件的大小和位置是由容器的布局管理器進(jìn)行控制。
其實(shí)就是獲取內(nèi)容面板,JFrame無法直接添加組件需要getContentPane()獲取面板,然后再內(nèi)容面板上添加組件。
因此平時(shí)添加的窗口組件都是添加到ContentPane里的, 通常都是分開寫的
Container c=this.getContentPane();//初始化一個(gè)容器
c.add(****); //在容器上添加控件..
或是:
this.getContentPane().add();
首先向innerWindow這個(gè)組件中添加元素,使用網(wǎng)格布局
innerWindow.setLayout(new GridLayout(2, 2, 1, 1)); //設(shè)置JPanel的布局
innerWindow.add(read);//JButton
innerWindow.add(write);//JButton
innerWindow.add(nameField);//JTextField
innerWindow.add(file);//JLabel
//向Jframe類型的對(duì)象中添加一個(gè)布局并且添加組件
//邊界布局具體參考博客
// https://xuzhiwei.blog.csdn.net/article/details/111302347
this.getContentPane().setLayout(new BorderLayout());
// 關(guān)于BorderLayout()邊界布局法,主要是按照東南西北中的順序進(jìn)行布局
this.getContentPane().add("North", innerWindow);//在上部分添加一個(gè)Jpanel
this.getContentPane().add(new JScrollPane(textArea));//添加一個(gè)滑動(dòng)控件
this.getContentPane().add("Center", textArea);//添加一個(gè)文本區(qū)域
調(diào)節(jié)一下顏色格式
innerWindow.setBackground(Color.red);
textArea.setBackground(Color.green);
//設(shè)置文本區(qū)域
textArea.setFont(new Font("Serif", Font.ITALIC, 20));
目前的效果是這樣的,但是現(xiàn)在還沒有加入函數(shù)響應(yīng)的效果.
添加一個(gè)動(dòng)作監(jiān)聽器
public void actionPerformed(ActionEvent evt) {
String fileName = nameField.getText();// 獲取文本框中的文件名稱
if (evt.getSource() == read) {
// 如果此時(shí)事件的來源是read
// 調(diào)用read函數(shù)
textArea.setText("");
readTextFile(textArea, fileName);//讀取相應(yīng)的文件名稱并將其顯示到testArea中
} else {
// 如果此時(shí)事件的來源不是read,也就是說事件的來源是write調(diào)用以下函數(shù)
writeTextFile(textArea, fileName);
}
}//end actionPerformed()
讀和寫特定文件
public CoupPad() {
...
read.addActionListener(this);
write.addActionListener(this);
}
//writes to a text file. IntelliJ will look for it at the Project Folder
private void writeTextFile(JTextArea textArea, String fileName) {
try {
FileWriter outStream = new FileWriter(fileName);
outStream.write(textArea.getText());
outStream.close();
} catch (IOException e) {
textArea.setText("IOERROR: " + e.getMessage() + "\n");
e.printStackTrace();
}
} // end writeTextFile()
//watches the button and waits until it is clicked
public void actionPerformed(ActionEvent evt) {
String fileName = nameField.getText();// 獲取文本框中的文件名稱
if (evt.getSource() == read) {
// 如果此時(shí)事件的來源是read
// 調(diào)用read函數(shù)
textArea.setText("");
readTextFile(textArea, fileName);//讀取相應(yīng)的文件名稱并將其顯示到testArea中
} else {
// 如果此時(shí)事件的來源不是read,也就是說事件的來源是write調(diào)用以下函數(shù)
writeTextFile(textArea, fileName);
}
}//end actionPerformed()
效果演示
保存文件
查看文件
總結(jié)
以上是生活随笔為你收集整理的java gui 读取文件夹_java Swing GUI 入门-文件读写器的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java oscache 使用_OSca
- 下一篇: java泛型约束_JAVA泛型 - 约