import java.awt.*;
成都創(chuàng)新互聯(lián)專注于麒麟企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站建設(shè),商城網(wǎng)站開(kāi)發(fā)。麒麟網(wǎng)站建設(shè)公司,為麒麟等地區(qū)提供建站服務(wù)。全流程定制網(wǎng)站設(shè)計(jì),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)
import java.awt.event.*;
import javax.swing.*;
class ConstructFrame extends JFrame
{
private static final long serialVersionUID = 1L;
String value1="",result,value2="";
int flag=0,fix=0,sum=1;
Boolean happy;
JTextField text=new JTextField(30);
int flagsum=0;
Container c=getContentPane();
JButton buttonx;
ConstructFrame()
{ super("計(jì)算器");
c.setLayout(null);
c.setBackground(Color.blue);
this.setSize(400, 400);
c.add(text);
text.setHorizontalAlignment(JTextField.RIGHT);
final JButton buttonx=new JButton("BackSpace");
c.add(buttonx);
buttonx.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
int count=0;
String temp;
if(flag==0)
{
count=value1.length();
if(count!=1)
temp=value1.substring(0, count-1);
else
temp="0";
value1=temp;
}
else
{
count=value2.length();
if(count!=1)
temp=value2.substring(0, count-1);
else
temp="0";
value2=temp;
}
text.setText(temp);
}
});
final JButton buttony=new JButton("CE");
c.add(buttony);
buttony.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
value1="";
value2="";
flag=0;
text.setText("0");
}
});
final JButton button1=new JButton("1");
c.add(button1);
button1.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
String temp;
if(flag==0)
{
value1=value1+1;
temp=value1;
}
else
{
value2=value2+1;
temp=value2;
}
text.setText(temp);
}
});
final JButton button2=new JButton(" 2 ");
c.add(button2);
button2.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
String temp;
if(flag==0)
{
value1=value1+2;
temp=value1;
}
else
{
value2=value2+2;
temp=value2;
}
text.setText(temp);
}
});
final JButton button3=new JButton("3");
c.add(button3);
button3.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
String temp;
if(flag==0)
{
value1=value1+3;
temp=value1;
}
else
{
value2=value2+3;
temp=value2;
}
text.setText(temp);
}
});
final JButton button4=new JButton("4");
c.add(button4);
button4.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
String temp;
if(flag==0)
{
value1=value1+4;
temp=value1;
}
else
{
value2=value2+4;
temp=value2;
}
text.setText(temp);
}
});
final JButton button5=new JButton("5");
c.add(button5);
button5.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
String temp;
if(flag==0)
{
value1=value1+5;
temp=value1;
}
else
{
value2=value2+5;
temp=value2;
}
text.setText(temp);
}
});
final JButton button6=new JButton("6");
c.add(button6);
button6.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
String temp;
if(flag==0)
{
value1=value1+6;
temp=value1;
}
else
{
value2=value2+6;
temp=value2;
}
text.setText(temp);
}
});
final JButton button7=new JButton("7");
c.add(button7);
button7.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
String temp;
if(flag==0)
{
value1=value1+7;
temp=value1;
}
else
{
value2=value2+7;
temp=value2;
}
text.setText(temp);
}
});
final JButton button8=new JButton("8");
c.add(button8);
button8.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
String temp;
if(flag==0)
{
value1=value1+8;
temp=value1;
}
else
{
value2=value2+8;
temp=value2;
}
text.setText(temp);
}
});
final JButton button9=new JButton("9");
c.add(button9);
button9.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
String temp;
if(flag==0)
{
value1=value1+9;
temp=value1;
}
else
{
value2=value2+9;
temp=value2;
}
text.setText(temp);
}
});
final JButton button0=new JButton("0");
c.add(button0);
button0.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
String temp;
if(flag==0)
{
value1=value1+0;
temp=value1;
}
else
{
value2=value2+0;
temp=value2;
}
text.setText(temp);
}
});
final JButton buttonadd=new JButton(" + ");
c.add(buttonadd);
buttonadd.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
flag=1;
fix=1;
flagsum=0;
}
});
final JButton buttonsubtract=new JButton(" - ");
c.add(buttonsubtract);
buttonsubtract.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
flag=1;
fix=2;
flagsum=0;
}
});
final JButton buttoncheng=new JButton(" * ");
c.add(buttoncheng);
buttoncheng.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
flag=1;
fix=3;
flagsum=0;
}
});
final JButton buttonchu=new JButton(" / ");
c.add(buttonchu);
buttonchu.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
flag=1;
flagsum=0;
fix=4;
}
});
final JButton buttonequal=new JButton(" = ");
c.add(buttonequal);
buttonequal.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
double temp1,temp2;
double temp=0;
flagsum=0;
temp1=Double.parseDouble(value1);
temp2=Double.parseDouble(value2);
flag=0;
switch(fix)
{
case 1: temp=temp1+temp2;break;
case 2: temp=temp1-temp2;break;
case 3: temp=temp1*temp2;break;
case 4: temp=temp1/temp2;break;
}
result=Double.valueOf(temp).toString();
value1=result;
value2="";
flag=1;
text.setText(result);
}
});
final JButton buttonpoint=new JButton(".");
c.add(buttonpoint);
buttonpoint.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{ if(flagsum==0)
{
String temp;
if(flag==0 )
{
value1=value1+".";
temp=value1;
}
else
{
value2=value2+".";
temp=value2;
}
flagsum=1;
text.setText(temp);
}
}
});
JButton buttonz=new JButton("Start");
c.add(buttonz);
buttonz.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{ if(sum%2==1)
{
happy=true;
text.setText("0.");
flag=0;
}
else
{
happy=false;
value1="";
value2="";
text.setText("");
}
text.setEnabled(happy);
button1.setEnabled(happy);
button2.setEnabled(happy);
button3.setEnabled(happy);
button4.setEnabled(happy);
button5.setEnabled(happy);
button6.setEnabled(happy);
button7.setEnabled(happy);
button8.setEnabled(happy);
button9.setEnabled(happy);
button0.setEnabled(happy);
buttonx.setEnabled(happy);
buttony.setEnabled(happy);
buttonadd.setEnabled(happy);
buttonsubtract.setEnabled(happy);
buttonpoint.setEnabled(happy);
buttonequal.setEnabled(happy);
buttoncheng.setEnabled(happy);
buttonchu.setEnabled(happy);
sum++;
}
});
button1.setEnabled(false);
button2.setEnabled(false);
button3.setEnabled(false);
button4.setEnabled(false);
button5.setEnabled(false);
button6.setEnabled(false);
button7.setEnabled(false);
button8.setEnabled(false);
button9.setEnabled(false);
button0.setEnabled(false);
buttonx.setEnabled(false);
buttony.setEnabled(false);
buttonadd.setEnabled(false);
buttonsubtract.setEnabled(false);
buttonpoint.setEnabled(false);
buttonequal.setEnabled(false);
buttoncheng.setEnabled(false);
buttonchu.setEnabled(false);
text.setEnabled(false);
text.setBounds(20, 20, 200, 40);
buttonx.setBounds(20, 60,100, 40);
buttony.setBounds(140, 60,100, 40);
buttonz.setBounds(260, 60,80, 40);
button1.setBounds(20, 120,60, 40);
button2.setBounds(100, 120,60, 40);
button3.setBounds(180, 120,60, 40);
buttonadd.setBounds(260, 120,60, 40);
button4.setBounds(20, 180,60, 40);
button5.setBounds(100, 180,60, 40);
button6.setBounds(180, 180,60, 40);
buttonsubtract.setBounds(260, 180,60, 40);
button7.setBounds(20, 240,60, 40);
button8.setBounds(100, 240,60, 40);
button9.setBounds(180, 240,60, 40);
buttoncheng.setBounds(260,240,60,40);
button0.setBounds(20, 300,60, 40);
buttonpoint.setBounds(100, 300, 60, 40);
buttonequal.setBounds(180,300,60, 40);
buttonchu.setBounds(260, 300,60, 40);
setVisible(true);
}
class MYMouseEvent extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
value1=e.toString();
text.setText(value1);
}
}
}
class Calutator
{
public static void main(String[] args)
{
new ConstructFrame();
}
}
你自己慢慢的看吧!
/*這個(gè)相當(dāng)詳細(xì)了.
程序也不算太難.而且給老師看的時(shí)候效果比較好.因?yàn)橛袌D形化界面,又實(shí)現(xiàn)一個(gè)比較實(shí)用的功能.老師會(huì)比較高興的.
建立一個(gè)文件名為Change.java就可以編譯了*/
/*
* 這個(gè)程序?qū)崿F(xiàn)輸入身高算出標(biāo)準(zhǔn)體重,輸入體重,算出身高的功能
*/
import java.awt.*; //導(dǎo)入相關(guān)類包,這才樣使用相應(yīng)awt圖形界面的類
import java.awt.event.*;//同上
public class Change extends Frame { //定義一個(gè)類Change, 父類是Frame(圖形界面的)
Button b = new Button("互查"); //創(chuàng)建一個(gè)按鈕的對(duì)象b,顯示為"互查"
Label l1 = new Label("身高(cm)");//創(chuàng)建一個(gè)lable.顯示身高
Label l2 = new Label("體重(kg)");//創(chuàng)建一個(gè)lable 顯示體重
double heigth, weigth; //定義變量
double x, y; //定義變量
TextField tf1 = new TextField(null, 10);//添加Text框
TextField tf2 = new TextField(null, 10);//添加Text框
public Change() {//類的構(gòu)造函數(shù),完成初始化
super("互查表");//創(chuàng)建窗口,標(biāo)題為互查表
setLayout(new FlowLayout(FlowLayout.LEFT));//設(shè)置布局
add(l1);//把lable 身高放到window里
add(tf1);//把Text 框 放到窗口上
add(l2); //把lable 體重放到window里
add(tf2);//Test放到窗口里
add(b);//把button放到窗口上
pack();//自動(dòng)放到窗口里排列上邊的組件
setVisible(true);//可以讓用戶看到窗口
addWindowListener(new WindowAdapter() {//如果按 X, 關(guān)閉窗口
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
b.addActionListener(new ButtonListener());//添加button監(jiān)聽(tīng)函數(shù)
}
class ButtonListener implements ActionListener {//實(shí)現(xiàn)click button時(shí)功能操作
public void actionPerformed(ActionEvent e) {//當(dāng)click調(diào)用
if (tf1.getText()!=null) {//檢查tf1 test 是否為空
try {//取異常
x = Double.parseDouble(tf1.getText());//字符轉(zhuǎn)為double型
weigth = (x - 100) * 0.9;//算重量
tf2.setText("" + weigth);//顯示重量
} catch (NumberFormatException ex) {
tf1.setText("");//如果輸入不是數(shù)字,設(shè)為空
}
}
if (tf1.getText().equals("")==true){//tf1是否為空
y = Double.parseDouble(tf2.getText());//把tf2里的文本轉(zhuǎn)為double 型 的
heigth = y / 0.9 + 100; //算身高根據(jù)重量
tf1.setText("" + heigth);}//顯示身高
}
}
public static void main(String[] args) {//主函數(shù),程序入口
new Change(); //建立類Change的對(duì)象,并調(diào)用他的構(gòu)造函數(shù)Change().顯示窗口
}
}
package com.ailk.client;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
public class FileTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
String filePath = "d:/text1.txt";
readFile(filePath,99);
}
public static void readFile(String fileName, int grede){
File file = new File(fileName);
BufferedReader reader = null;
BufferedWriter bw = null;???????
try {
System.out.println("以行為單位讀取文件內(nèi)容,一次讀一整行:");
reader = new BufferedReader(new FileReader(file));
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("d:/test2.txt")), "UTF-8"));
String tempString = null;
int line = 1;
// 一次讀入一行,直到讀入null為文件結(jié)束
while ((tempString = reader.readLine()) != null) {
// 顯示行號(hào)
System.out.println("line " + line + ": " + tempString);
if(line==4){
//此處為你自己設(shè)定的文件中代表成績(jī)的位置
tempString = tempString.substring(0, 37)+grede+tempString.substring(39, tempString.length());
System.out.println("line " + line + ": " + tempString);
}
bw.write(tempString);
bw.newLine();
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
if (bw != null) {
try {
bw.close();
} catch (IOException e1) {
}
}
}
}
}
需要自己d盤(pán)建立text1.txt,內(nèi)容你自己根據(jù)要求,代碼中寫(xiě)死的37和39為你要修改的成績(jī)的位置。
效果圖如下,中午給你寫(xiě)的記得采納
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.BorderFactory;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
public class JNotePadUI extends JFrame {
private JMenuItem menuOpen;
private JMenuItem menuSave;
private JMenuItem menuSaveAs;
private JMenuItem menuClose;
private JMenu editMenu;
private JMenuItem menuCut;
private JMenuItem menuCopy;
private JMenuItem menuPaste;
private JMenuItem menuAbout;
private JTextArea textArea;
private JLabel stateBar;
private JFileChooser fileChooser;
private JPopupMenu popUpMenu;
public JNotePadUI() {
super("新建文本文件");
setUpUIComponent();
setUpEventListener();
setVisible(true);
}
private void setUpUIComponent() {
setSize(640, 480);
// 菜單欄
JMenuBar menuBar = new JMenuBar();
// 設(shè)置「文件」菜單
JMenu fileMenu = new JMenu("文件");
menuOpen = new JMenuItem("打開(kāi)");
// 快捷鍵設(shè)置
menuOpen.setAccelerator(
KeyStroke.getKeyStroke(
KeyEvent.VK_O,
InputEvent.CTRL_MASK));
menuSave = new JMenuItem("保存");
menuSave.setAccelerator(
KeyStroke.getKeyStroke(
KeyEvent.VK_S,
InputEvent.CTRL_MASK));
menuSaveAs = new JMenuItem("另存為");
menuClose = new JMenuItem("關(guān)閉");
menuClose.setAccelerator(
KeyStroke.getKeyStroke(
KeyEvent.VK_Q,
InputEvent.CTRL_MASK));
fileMenu.add(menuOpen);
fileMenu.addSeparator(); // 分隔線
fileMenu.add(menuSave);
fileMenu.add(menuSaveAs);
fileMenu.addSeparator(); // 分隔線
fileMenu.add(menuClose);
// 設(shè)置「編輯」菜單
JMenu editMenu = new JMenu("編輯");
menuCut = new JMenuItem("剪切");
menuCut.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_X,
InputEvent.CTRL_MASK));
menuCopy = new JMenuItem("復(fù)制");
menuCopy.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_C,
InputEvent.CTRL_MASK));
menuPaste = new JMenuItem("粘貼");
menuPaste.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_V,
InputEvent.CTRL_MASK));
editMenu.add(menuCut);
editMenu.add(menuCopy);
editMenu.add(menuPaste);
// 設(shè)置「關(guān)于」菜單
JMenu aboutMenu = new JMenu("關(guān)于");
menuAbout = new JMenuItem("關(guān)于JNotePad");
aboutMenu.add(menuAbout);
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(aboutMenu);
setJMenuBar(menuBar);
// 文字編輯區(qū)域
textArea = new JTextArea();
textArea.setFont(new Font("宋體", Font.PLAIN, 16));
textArea.setLineWrap(true);
JScrollPane panel = new JScrollPane(textArea,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
Container contentPane = getContentPane();
contentPane.add(panel, BorderLayout.CENTER);
// 狀態(tài)欄
stateBar = new JLabel("未修改");
stateBar.setHorizontalAlignment(SwingConstants.LEFT);
stateBar.setBorder(
BorderFactory.createEtchedBorder());
contentPane.add(stateBar, BorderLayout.SOUTH);
popUpMenu = editMenu.getPopupMenu();
fileChooser = new JFileChooser();
}
private void setUpEventListener() {
// 按下窗口關(guān)閉鈕事件處理
addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
closeFile();
}
}
);
// 菜單 - 打開(kāi)
menuOpen.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
openFile();
}
}
);
// 菜單 - 保存
menuSave.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
saveFile();
}
}
);
// 菜單 - 另存為
menuSaveAs.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
saveFileAs();
}
}
);
// 菜單 - 關(guān)閉文件
menuClose.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
closeFile();
}
}
);
// 菜單 - 剪切
menuCut.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
cut();
}
}
);
// 菜單 - 復(fù)制
menuCopy.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
copy();
}
}
);
// 菜單 - 粘貼
menuPaste.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
paste();
}
}
);
// 菜單 - 關(guān)于
menuAbout.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 顯示對(duì)話框
JOptionPane.showOptionDialog(null,
"程序名稱:\n JNotePad \n" +
"程序設(shè)計(jì):\n \n" +
"簡(jiǎn)介:\n 一個(gè)簡(jiǎn)單的文字編輯器\n" +
" 可作為驗(yàn)收J(rèn)ava的實(shí)現(xiàn)對(duì)象\n" +
" 歡迎網(wǎng)友下載研究交流\n\n" +
" /",
"關(guān)于JNotePad",
JOptionPane.DEFAULT_OPTION,
JOptionPane.INFORMATION_MESSAGE,
null, null, null);
}
}
);
// 編輯區(qū)鍵盤(pán)事件
textArea.addKeyListener(
new KeyAdapter() {
public void keyTyped(KeyEvent e) {
processTextArea();
}
}
);
// 編輯區(qū)鼠標(biāo)事件
textArea.addMouseListener(
new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
if(e.getButton() == MouseEvent.BUTTON3)
popUpMenu.show(editMenu, e.getX(), e.getY());
}
public void mouseClicked(MouseEvent e) {
if(e.getButton() == MouseEvent.BUTTON1)
popUpMenu.setVisible(false);
}
}
);
}
private void openFile() {
if(isCurrentFileSaved()) { // 文件是否為保存狀態(tài)
open(); // 打開(kāi)
}
else {
// 顯示對(duì)話框
int option = JOptionPane.showConfirmDialog(
null, "文件已修改,是否保存?",
"保存文件?", JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE, null);
switch(option) {
// 確認(rèn)文件保存
case JOptionPane.YES_OPTION:
saveFile(); // 保存文件
break;
// 放棄文件保存
case JOptionPane.NO_OPTION:
open();
break;
}
}
}
private boolean isCurrentFileSaved() {
if(stateBar.getText().equals("未修改")) {
return false;
}
else {
return true;
}
}
private void open() {
// fileChooser 是 JFileChooser 的實(shí)例
// 顯示文件選取的對(duì)話框
int option = fileChooser.showDialog(null, null);
// 使用者按下確認(rèn)鍵
if(option == JFileChooser.APPROVE_OPTION) {
try {
// 開(kāi)啟選取的文件
BufferedReader buf =
new BufferedReader(
new FileReader(
fileChooser.getSelectedFile()));
// 設(shè)定文件標(biāo)題
setTitle(fileChooser.getSelectedFile().toString());
// 清除前一次文件
textArea.setText("");
// 設(shè)定狀態(tài)欄
stateBar.setText("未修改");
// 取得系統(tǒng)相依的換行字符
String lineSeparator = System.getProperty("line.separator");
// 讀取文件并附加至文字編輯區(qū)
String text;
while((text = buf.readLine()) != null) {
textArea.append(text);
textArea.append(lineSeparator);
}
buf.close();
}
catch(IOException e) {
JOptionPane.showMessageDialog(null, e.toString(),
"開(kāi)啟文件失敗", JOptionPane.ERROR_MESSAGE);
}
}
}
private void saveFile() {
// 從標(biāo)題欄取得文件名稱
File file = new File(getTitle());
// 若指定的文件不存在
if(!file.exists()) {
// 執(zhí)行另存為
saveFileAs();
}
else {
try {
// 開(kāi)啟指定的文件
BufferedWriter buf =
new BufferedWriter(
new FileWriter(file));
// 將文字編輯區(qū)的文字寫(xiě)入文件
buf.write(textArea.getText());
buf.close();
// 設(shè)定狀態(tài)欄為未修改
stateBar.setText("未修改");
}
catch(IOException e) {
JOptionPane.showMessageDialog(null, e.toString(),
"寫(xiě)入文件失敗", JOptionPane.ERROR_MESSAGE);
}
}
}
private void saveFileAs() {
// 顯示文件對(duì)話框
int option = fileChooser.showSaveDialog(null);
// 如果確認(rèn)選取文件
if(option == JFileChooser.APPROVE_OPTION) {
// 取得選擇的文件
File file = fileChooser.getSelectedFile();
// 在標(biāo)題欄上設(shè)定文件名稱
setTitle(file.toString());
try {
// 建立文件
file.createNewFile();
// 進(jìn)行文件保存
saveFile();
}
catch(IOException e) {
JOptionPane.showMessageDialog(null, e.toString(),
"無(wú)法建立新文件", JOptionPane.ERROR_MESSAGE);
}
}
}
private void closeFile() {
// 是否已保存文件
if(isCurrentFileSaved()) {
// 釋放窗口資源,而后關(guān)閉程序
dispose();
}
else {
int option = JOptionPane.showConfirmDialog(
null, "文件已修改,是否保存?",
"保存文件?", JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE, null);
switch(option) {
case JOptionPane.YES_OPTION:
saveFile();
break;
case JOptionPane.NO_OPTION:
dispose();
}
}
}
private void cut() {
textArea.cut();
stateBar.setText("已修改");
popUpMenu.setVisible(false);
}
private void copy() {
textArea.copy();
popUpMenu.setVisible(false);
}
private void paste() {
textArea.paste();
stateBar.setText("已修改");
popUpMenu.setVisible(false);
}
private void processTextArea() {
stateBar.setText("已修改");
}
public static void main(String[] args) {
new JNotePadUI();
}
}
1.b應(yīng)該是加''(單引號(hào))
2.加了單引號(hào)也是錯(cuò)誤的,因?yàn)閟etCharAt實(shí)際上是已經(jīng)把s修改了,然后返回void,自然不能把void賦值給t啊,所以,改成下面的就可以了
StringBuffer s=new StringBuffer("Java Applet");
System.out.println("s="+s);
s.setCharAt(3,'b');
System.out.println(s);