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

📄 myframe.java

📁 东软JAVA内部资料
💻 JAVA
字号:
package org.course.gui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.*;
import org.course.io.*;

public class MyFrame extends Frame {
	Panel nPanel,sPanel,wPanel,ePanel;
	Label label;
	TextArea area;
	Button bOpen, bSave, bClose;
	MenuBar menuBar;
	Menu mFile;
	MenuItem iOpen;
	MenuItem iSave;
	MenuItem iClose;
	Menu mHelp;
	
	String openFile;
	
	public MyFrame() {
		super("MyFrame");
		nPanel = new Panel();
		sPanel = new Panel();
		wPanel = new Panel();
		ePanel = new Panel();
		label = new Label("Welcome to my frame");
		area = new TextArea(10,15);
		bOpen = new Button("open");
		bSave = new Button("save");
		bClose = new Button("close");
		
		this.add(nPanel, BorderLayout.NORTH);
		this.add(sPanel, BorderLayout.SOUTH);
		this.add(area, BorderLayout.CENTER);
		this.add(wPanel, BorderLayout.WEST);
		this.add(ePanel, BorderLayout.EAST);
		
		nPanel.add(label);
		sPanel.add(bOpen);
		sPanel.add(bSave);
		sPanel.add(bClose);
		
		addMenu();
		addListener();		
		
		init();
	}
	
	public void close() {
		this.dispose();
	}
	
	private void addMenu() {
		menuBar = new MenuBar();
		mFile = new Menu("file");
		mHelp = new Menu("help");
		iOpen = new MenuItem("open");
		iSave = new MenuItem("save");
		iClose = new MenuItem("close");
		
		this.setMenuBar(menuBar);
		menuBar.add(mFile);
		menuBar.setHelpMenu(mHelp);
		mFile.add(iOpen);
		mFile.add(iSave);
		mFile.add(iClose);
	}
	
	private void init() {
		this.setSize(400, 300);
		this.setVisible(true);
	}
	
	private void addListener() {
		this.addWindowListener(new WindowAction(this));
		CloseAction closeLis = new CloseAction(this);
		bClose.addActionListener(closeLis);
		iClose.addActionListener(closeLis);
		
		OpenAction openLis = new OpenAction(this);
		bOpen.addActionListener(openLis);
		iOpen.addActionListener(openLis);
	}
	
	public void openFile() {
		FileDialog dialog = new FileDialog(this, "Open File", FileDialog.LOAD);;
		dialog.setVisible(true);
		String file = dialog.getDirectory() + dialog.getFile();
		JOptionPane.showMessageDialog(this, file);
		String content = IODemo.readFile(file);
		openFile = file;
		area.setText("");
		area.append(content);
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new MyFrame();
	}

}

class CloseAction implements ActionListener {
	private MyFrame window;
	
	public CloseAction(MyFrame frame) {
		window = frame;
	}
	public void actionPerformed(ActionEvent e) {
		window.close();
		
	}
	
}

class WindowAction extends WindowAdapter {
	private MyFrame window;
	
	public WindowAction(MyFrame frame) {
		window = frame;
	}
	
	public void windowClosing(WindowEvent e) {
		window.close();
	}
}

class OpenAction implements ActionListener {
	private MyFrame window;
	
	public OpenAction(MyFrame frame) {
		window = frame;
	}
	public void actionPerformed(ActionEvent e) {
		window.openFile();
		
	}
	
}

class SaveAction implements ActionListener {
	private MyFrame window;
	
	public SaveAction(MyFrame frame) {
		window = frame;
	}
	public void actionPerformed(ActionEvent e) {
		window.openFile();
		
	}
	
}

⌨️ 快捷键说明

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