java 17_java17

1 importjava.awt.FileDialog;2 importjava.awt.Font;3 importjava.awt.event.ActionEvent;4 importjava.awt.event.ActionListener;5 importjava.awt.event.WindowEvent;6 importjava.awt.event.WindowListener;7 importjava.io.File;8 importjava.io.FileReader;9 importjava.io.FileWriter;10 importjavax.swing.JButton;11 importjavax.swing.JFrame;12 importjavax.swing.JMenu;13 importjavax.swing.JMenuBar;14 importjavax.swing.JMenuItem;15 importjavax.swing.JScrollPane;16 importjavax.swing.JTextArea;17

18 public class MainFrame extends JFrame implementsActionListener{19

20 /**

21 *22 */

23 private static final long serialVersionUID = 4253714983535650834L;24 privateJButton btnSave;25 privateJButton btnCancel;26 privateJTextArea txtArea;27 privateJMenuItem miOpen;28 privateJMenuItem miExit;29

30 publicMainFrame() {31 initFrame();32 this.setVisible(true);33 }34

35 private voidinitFrame() {36 //标题

37 this.setTitle("MainFrame");38 //边界

39 this.setBounds(100, 100, 800, 700);40 //绝对布局

41 this.setLayout(null);42 Font font = new Font("宋体", Font.BOLD, 16);43

44

45

46

47 txtArea = newJTextArea();48 //txtArea.setBounds(0, 0, 800, 500);49 //txtArea.setFont(font);50 //txtArea.setBackground(Color.gray);51 //txtArea.setForeground(Color.red);52 //将文本域添加到滚动面板53 //滚动面板

54 JScrollPane scrollPane = newJScrollPane(txtArea);55 scrollPane.setBounds(0, 0, 850, 500);56 this.add(scrollPane);57

58 btnSave = new JButton("保存");59 btnSave.setBounds(500, 510, 100, 50);60 btnSave.setFont(font);61 btnSave.addActionListener(this);62 this.add(btnSave);63

64 btnCancel = new JButton("取消");65 btnCancel.setBounds(610, 510, 100, 50);66 btnCancel.setFont(font);67 btnCancel.addActionListener(this);68 this.add(btnCancel);69

70 //添加窗口事件处理程序,使用适配器

71 this.addWindowListener(newWindowListener() {72

73 @Override74 public voidwindowOpened(WindowEvent e) {75

76 }77

78 @Override79 public voidwindowIconified(WindowEvent e) {80 //TODO Auto-generated method stub

81

82 }83

84 @Override85 public voidwindowDeiconified(WindowEvent e) {86 //TODO Auto-generated method stub

87

88 }89

90 @Override91 public voidwindowDeactivated(WindowEvent e) {92 //TODO Auto-generated method stub

93

94 }95

96 @Override97 public voidwindowClosing(WindowEvent e) {98 System.out.println("关闭");99 System.exit(-1);100

101 }102

103 @Override104 public voidwindowClosed(WindowEvent e) {105 //TODO Auto-generated method stub

106

107 }108

109 @Override110 public voidwindowActivated(WindowEvent e) {111 //TODO Auto-generated method stub

112

113 }114 });115

116 //添加菜单栏

117 JMenuBar menuBar = newJMenuBar();118

119 JMenu menu = new JMenu("文件");120 miOpen = new JMenuItem("打开");121 //添加监听器

122 miOpen.addActionListener(this);123 //添加菜单项

124 menu.add(miOpen);125

126 miExit = new JMenuItem("退出");127 menu.add(miExit);128 miExit.addActionListener(this);129 //添加菜单

130 menuBar.add(menu);131 this.setJMenuBar(menuBar);132 }133

134 /**

135 * 动作处理程序136 */

137 @Override138 public voidactionPerformed(ActionEvent e) {139 //获得事件源

140 Object source =e.getSource();141 if (source ==btnSave) {142 String str =txtArea.getText();143 try{144 //打开保存对话框,定位保存文件位置

145 FileDialog d = new FileDialog(this, "保存", FileDialog.SAVE);146 d.setVisible(true);147 File f = newFile(d.getDirectory() , d.getFile());148 System.out.println(d.getDirectory()+d.getFile());149 FileWriter writer = newFileWriter(f);150 writer.write(str);151 writer.close();152 } catch(Exception e1) {153 e1.printStackTrace();154 }155

156 } else if (source ==btnCancel) {157 this.dispose();158 } else if (source ==miOpen) {159 FileDialog fileDialog = new FileDialog(this, "打开", FileDialog.LOAD);160 fileDialog.setVisible(true);161 String dir =fileDialog.getDirectory();162 String f =fileDialog.getFile();163 if (dir != null && f != null) {164 try{165 txtArea.setText(null);166 FileReader reader = new FileReader(newFile(dir, f));167 char[] cbuf = new char[1024];168 int len = -1;169 while((len = reader.read(cbuf)) != -1) {170 txtArea.setText(txtArea.getText() + new String(cbuf, 0, len));171 }172 reader.close();173 } catch(Exception e1) {174 e1.printStackTrace();175 }176 }177 } else if (source ==miExit) {178 System.exit(-1);179 }180 }181 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值