问答题

以下程序是一个简单文本处理器,菜单项可以打开、编辑、保存一个文件。文件内容显示在下面的文本区域中(提示,打开文件通过文件选择器来完成)。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。运行结果如下图所示。
注意:不改动程序的结构,不得增行或删行。 import java.awt.*; import java.awt.event.*; import java.io.* ; import javax.swing.*; class FileFrame extends JFrame { File file; JTextPane textpane; FileInputStream readStream; JScrollPane scroll; public FileFrame() { super ("文件浏览"); JMenu fileM = new JMenu("文件"); OpenAction open = new OpenAction (); SaveAction clear = new SaveAction (); ExitAction exit = new ExitAction(); JMenuBar mb = new JMenuBar(); fileM.add(open); fileM.add(clear); fileM.add(exit); mb.add(fileM); textpane=new JTextPane(); scroll=new JScrollPane(textpane); getContentPane().add(scroll); getContentPane().addJMenuBar(mb); } class OpenAction extends AbstractAction { public OpenAction () { super("打开"); } public void actionPerformed( ActionEvent e ) { JFileChooser chooser=new JFileChooser(); int state=chooser.showOpenDialog(null); file=chooser.selectedFile(); if(file!=null&&state==JFileChooser.APPROVE_OPTION) { try { readStream=new FileInputStream(file); textpane.read(readStream, this); readStream.close(); } catch(IOException ioE){} } } } class SaveAction extends AbstractAction { public SaveAction() { super("保存"); } public void actionPerformed( ActionEvent e ) { if(file==null) return; try{ FileWriter out = new FileWriter(file); out.read(textpane.getText()); out.close(); } catch (IOException ioE) {} } } class ExitAction extends AbstractAction { public ExitAction() { super("退出"); } public void actionPerformed( ActionEvent e ) { System.exit(0); } } public static void main(String argv[]) { FileFrame f = new FileFrame(); f.setDefaultCloseOperation(JFrame. EXIT ON CLOSE); f.setSize(300,400); f.show(); } }

【参考答案】

①setJMenuBar(mb)②file=chooser.getSelectedFile()③out.write(te......

(↓↓↓ 点击下方‘点击查看答案’看完整答案 ↓↓↓)
热门 试题

问答题
请完成下面的程序:实现一个可以每秒跳动的时钟。运行如下图所示。请填写横线处的内容。注意:请勿改动main主方法和其他已有语句内容,仅在下划线处填入适当的语句。import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.util.*;public class Example2_12 extends JFrame (1) implements Runnable{Thread thread1;Color handColor;Color numberColor;JLabel jlabell = new JLabel();public Example2_12(){enableEvents(AWTEvent.WINDOW_EVENT_MASK);try{getContentPane().add(jlabell, BorderLayout. SOUTH);}catch (Exception e){System.out.println(e.getMessage());}}public static void main(String args[]){Example2 12_clock1 = new Example2_12();clock1.init();clock1.start();clock1.setSize(260, 230);clock1.setResizable(false);clock1.show();}public void init(){int x, y;handColor = Color.blue;numberColor = Color.darkGray;setBackground(Color.white);}public void paint (Graphics g){int xh, yh, xm, ym, xs, ys, s = 0, m = 10, h = 10, xpoint, ypoint;String today;Calendar c1 = ______;s = c1.get(Calendar.SECOND);m = c1.get(Calendar.MINUTE);h = c1.get(Calendar.HOUR);int day, month, year, weekday;day = c1.get(Calendar.DATE);month = c1.get(Calendar.MONTH) + 1;year = c1. get (Calendar. YEAR);weekday = c1.get (Calendar. DAY_OF_WEEK);jlabell.setText( Today is + year + + month + + day + + Time: + h + : + m + : + s);xpoint = 130;ypoint = 100;xs =(int) (Math.cos(s * 3.14f 30 - 3.14f 2) * 45 + xpoint);ys =(int) (Math.sin(s * 3.14f 30 - 3.14f 2) * 45 + ypoint);xm =(int) (Math.cos (m * 3.14f 30 - 3.14f 2) * 40 + xpoint);ym =(int) (Math.sin(m * 3.14f 30 - 3.14f 2) * 40 + ypoint);xh =(int) (Math.cos( (h * 30 + m 2) * 3.14f 180 - 3.14f 2) * 30 + xpoint);yh =(int) (Math.sin( (h * 30 + m 2) * 3.14f 180 - 3.14f 2) * 30 + ypoint);g.setColor (handColor);g.clearRect(0, 0, 260, 200);g.drawOval(xpoint 2 + 10, ypoint 2 - 5, 110, 110);g.setColor(numberColor);g.drawString( 9 , xpoint - 45, ypoint + 3);g.drawString( 3 , xpoint + 40, ypoint + 3);g.drawString( 12 , xpoint - 5, ypoint - 37);g.drawString( 6 , xpoint - 3, ypoint + 45);g.setColor(getBackground ( ) );g.setColor(numberColor);g.drawString( , 5, 125);g.drawLine(xpoint, ypoint, xs, ys);g.setColor(handColor);g.drawLine(xpoint, ypoint, xm, ym);g.drawLine(xpoint, ypoint, xh, xh);}public void start(){thread1 = new Thread(this);thread1. ______;}public void stop (){thread1=null;}public void update (Graphics g){paint (g);}public void run (){Thread me = Thread. currentThread ();while (thread1 == me){try{Thread. currentThread ( ) . sleep ( 100 );}catch (InterruptedException e){e.printStackTrace();}repaint ( );}}protected void processWindowEvent(WindowEvent e){super.processWindowEvent(e);if (e.getID() == WindowEvent.WINDOW_CLOSING)System.exit(0);}}