嗒酥 2015-01-14 06:35 采纳率: 0%
浏览 1759

求大神帮我将代码简化(本人还处在学习阶段,都是自学的)

package Test;

import java.awt.BorderLayout;

public class Ftest extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JTextArea jTextArea = null;
private JPanel controlPanel = null;
private JButton openButton = null;
private JButton closeButton = null;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Ftest thisclass = new Ftest();
                thisclass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);;
                thisclass.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });


}

/**
 * Create the frame.
 */
public Ftest() {
    initianlize();

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    controlPanel = new JPanel();
    controlPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(controlPanel);
    controlPanel.setLayout(null);

    final JTextArea jTextArea = new JTextArea();
    jTextArea.setBounds(10, 10, 414, 215);
    controlPanel.add(jTextArea);

    JButton openButton = new JButton("\u5199\u5165\u6587\u4EF6");//写入文件
    openButton.setBounds(72, 229, 93, 23);
    controlPanel.add(openButton);

        openButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                File file = new File("D:/Workspase/Input_Output/world.txt");
                try{
                    FileWriter out = new FileWriter(file);
                    String s = jTextArea.getText();
                    out.write(s);
                    out.close();
                }catch(Exception e1){
                    e1.printStackTrace();
                }
            }
        });


    JButton closeButton = new JButton("\u8BFB\u53D6\u6587\u4EF6");//读取文件
    closeButton.setBounds(247, 229, 93, 23);
    controlPanel.add(closeButton);
    closeButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            File file = new File("D:/Workspase/Input_Output/world.txt");
            try{
                FileReader in = new FileReader(file);
                char byt[] = new char[1024];
                int len = in.read(byt);
                jTextArea.setText(new String(byt,0,len));
                in.close();
            }catch(Exception e1){
                e1.printStackTrace();
            }
        }
    });


}
private void initianlize() {
    // TODO Auto-generated method stub
    this.setSize(300,200);
    this.setContentPane(getContentPane());
    this.setTitle("JFrame");
}


private JPanel getJContentPane(){
    if(jContentPane == null){
        jContentPane = new JPanel();
        jContentPane.setLayout(new BorderLayout());
        jContentPane.add(getJTextArea(),BorderLayout.CENTER);
        jContentPane.add(getControlPanel(),BorderLayout.SOUTH);
    }
    return jContentPane;
}

private Component getControlPanel() {
    // TODO Auto-generated method stub
    return null;
}

private Component getJTextArea() {
    // TODO Auto-generated method stub
    return null;
}

}

  • 写回答

2条回答 默认 最新

  • lx624909677 2015-01-14 13:19
    关注

    做100件事的代码量一般是和做1件事的代码量不一样的,简化的话功能就少了

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大