qq_36707607 2017-12-06 11:04 采纳率: 50%
浏览 789
已采纳

我编写的这段代码为什么两个按钮只出现了一个。。

import javax.swing.*;
import java.awt.event.*;
import java.io.*;
class fileoperator implements ActionListener{
JFrame jf=new JFrame("fileoperator");
JTextArea ta=new JTextArea(10,10);
fileoperator(){
JPanel p=new JPanel();
JButton open=new JButton("读文件");
JButton save=new JButton("写文件");
jf.add(ta);jf.add(open);jf.add(save);jf.add(p,"South");
open.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser jfc=new JFileChooser();
jfc.showOpenDialog(jf);
File file=jfc.getSelectedFile();
try {
FileInputStream fis=new FileInputStream(file);
InputStreamReader isr=new InputStreamReader(fis);
BufferedReader br=new BufferedReader(isr);
String s;
ta.setText("");
while((s=br.readLine())!=null)
ta.append(s+"\n");
br.close();
}catch(Exception ee) {
System.out.println("文件不存在!");
}
}

     });
     save.addActionListener(this);
     jf.setBounds(200, 200, 200,200);
     jf.setVisible(true);
 }
 public void actionPerformed(ActionEvent e) {
     JFileChooser jfc=new JFileChooser();
     jfc.showSaveDialog(jf);
     File file=jfc.getSelectedFile();
     try {
         FileOutputStream fos=new FileOutputStream(file);
         OutputStreamWriter osw=new OutputStreamWriter(fos);
         BufferedWriter bw=new BufferedWriter(osw);
         String s;
         bw.write(ta.getText());
         bw.close();

     }catch(Exception ee){
         System.out.println("没有存储空间!");

     }
 }

}
class FiletoUnicode{
public static void main(String []ppp) {
new fileoperator();
}
}图片说明

  • 写回答

3条回答 默认 最新

  • ooo-ooo 2017-12-07 02:28
    关注

    可能是布局问题吧,你试下把save按钮的渲染注释掉,看看open按钮有没有出来。如果出来了说明是布局问题,在同一位置上,open按钮被save按钮盖住了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • qq_36707607 2017-12-06 11:08
    关注

    读文件的按钮怎么被覆盖掉了??

    评论
  • 小于先森 2017-12-06 12:20
    关注

    JPanel 需要设置layout 而且 JFrame 一般都会设置宽高的吧?

    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 相同型号电脑与配置,发现主板有一台貌似缺少了好多元器件似的,会影响稳定性和使用寿命吗?
  • ¥15 要求编写稀疏矩阵A的转置矩阵的算法
  • ¥15 编写满足以下要求的停车场管理程序,设停车场只有一个可停放n辆车的狭窄通道且只有一个大门可供车辆进出。
  • ¥15 C语言:数据子序列基础版
  • ¥20 powerbulider 导入excel文件,显示不完整
  • ¥15 用keil调试程序保证结果进行led相关闪烁
  • ¥15 paddle训练自己的数据loss降不下去
  • ¥20 用matlab的pdetool解决以下三个问题
  • ¥15 单个福来轮的平衡与侧向滑动是如何做到的?
  • ¥15 嵌入式Linux固件,能直接告诉我crc32校验的区域在哪不,内核的校验我已经找到了,uboot没有