⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch7_e7_31.java

📁 各种关于JAVA的初级编程
💻 JAVA
字号:
import java.awt.*; 
import java.awt.event.*;

public class ch7_e7_31
{
    public static void main(String args[])
    {
        new MyFrame();
    }
}

class MyFrame extends Frame implements ActionListener,
    FocusListener, TextListener
{
    TextArea myTextArea;
    MenuBar m_MenuBar;
    Menu menuFile;
    MenuItem mi_File_Exit;
    boolean textModified = false;
    String orgMsg = "原始信息";
    
    Dialog modifyConfirmDialog;
    Button confirmBtn,denyBtn,otherBtn;
    
    
    MyFrame()
    {
        super("我的窗口");
        
        myTextArea = new TextArea(10,45);
        myTextArea.setText(orgMsg);
        myTextArea.addTextListener(this);
        myTextArea.addFocusListener(this);
        
        confirmBtn = new Button("是");
        denyBtn = new Button("否");
        confirmBtn.addActionListener(this);
        denyBtn.addActionListener(this);
        
        m_MenuBar = new MenuBar();
        menuFile = new Menu("文件");
        mi_File_Exit = new MenuItem("退出");
        mi_File_Exit.setActionCommand("退出");
        mi_File_Exit.addActionListener(this);
        menuFile.add(mi_File_Exit);
        m_MenuBar.add(menuFile);
        
        this.setMenuBar(m_MenuBar);
        addWindowListener(new closeWindow());
        
        otherBtn = new Button("把焦点给我");
        setLayout(new FlowLayout());
        add(myTextArea);
        add(otherBtn);
        pack();
        show();
    }
    
    public void focusGained(FocusEvent fe)
    {
        textModified = false;
    }
    
    public void focusLost(FocusEvent fe)
    {
        if(textModified)
        {
            modifyConfirmDialog = new Dialog(this,
                "确认修改",true);
            modifyConfirmDialog.setLayout(new FlowLayout());
            modifyConfirmDialog.add(new Label(
                "文本内容已经被修改,要保留这些修改吗?"));
            modifyConfirmDialog.add(confirmBtn);
            modifyConfirmDialog.add(denyBtn);
            modifyConfirmDialog.pack();
            modifyConfirmDialog.show();
        }
    }
    
    public void textValueChanged(TextEvent te)
    {
        if(te.getSource() == myTextArea)
        {
            textModified = true;
        }
    }
    
    public void actionPerformed(ActionEvent ae)
    {
        if(ae.getSource() == confirmBtn)
        {
            orgMsg = myTextArea.getText();
            textModified = false;
            modifyConfirmDialog.dispose();
        }
        else if(ae.getSource() == denyBtn)
        {
            myTextArea.setText(orgMsg);
            textModified = false;
            modifyConfirmDialog.dispose();
        }
        else if(ae.getActionCommand().equals("退出"))
        {
            dispose();
            System.exit(0);
        }
    }
}

class closeWindow extends WindowAdapter
{
    public void windowClosing(WindowEvent we)
    {
        Frame frm = (Frame)we.getWindow();
        frm.dispose();
        System.exit(0);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -