Head mention: Write a notepad
Experiment code:
package Rectangle;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import static javax.swing.KeyStroke.*;
public class TextPad {
JFrame frame;
JMenuBar bar;
JMenu fileMenu,editMenu;
JMenuItem newItem,openItem,saveItem,closeItem;
ImageIcon newIcon,openIcon,saveIcon,closeIcon;
JScrollPane scroll;
JTextArea area;
JFileChooser chooser;
File file;
public TextPad(){
frame =new JFrame("记事本");
bar =new JMenuBar();
fileMenu =new JMenu("文件");
editMenu =new JMenu("编辑");
newIcon=new ImageIcon("d:"+File.separator+"Test"+File.separator+"A.png");
openIcon=new ImageIcon("d:"+File.separator+"Test"+File.separator+"B.png");
saveIcon=new ImageIcon("d:"+File.separator+"Test"+File.separator+"C.png");
closeIcon=new ImageIcon("d:"+File.separator+"Test"+File.separator+"D.png");
newItem=new JMenuItem("新建(N)",newIcon);
openItem=new JMenuItem("打开(O)",openIcon);
saveItem=new JMenuItem("另存为(A)",saveIcon);
closeItem=new JMenuItem("关闭(X)",closeIcon);
area =new JTextArea();
scroll=new JScrollPane(area);
newItem.setMnemonic(KeyEvent.VK_N);
openItem.setMnemonic(KeyEvent.VK_O);
saveItem.setMnemonic(KeyEvent.VK_A);
closeItem.setMnemonic(KeyEvent.VK_X);
newItem.setAccelerator(KeyStroke.getKeyStroke((char) KeyEvent.CTRL_MASK));
fileMenu.add(newItem);
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.addSeparator();
fileMenu.add(closeItem);
bar.add(fileMenu);
bar.add(editMenu);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent arg0){
System.exit(1);
}});
frame.setJMenuBar(bar);
frame.add(scroll);
frame.setVisible(true);
frame.setSize(300,180);
frame.setLocation(300,200);
}
}
Screenshot results
Experimental summary
This notebook class when the teacher talked about a lot, but very fast, but also to understand the front, to the back, and some knowledge forgotten, so I can only write a
relatively simple notepad out, there are many features require follow-up perfect.
Lessons Learned
Radio button JRadioButton
A plurality of radio buttons in a given display information is specified in a select
ItemListener interface methods
Common methods and classes constants ItemEvent
Checkbox JCheckBox
You can select multiple
Menu component
While many buttons that appear a single interface, you can use the menu layout unified management, it can make
the interface more feature-rich.
1.JMenu 和 JMenuBar
Menu component to be used to achieve Jmenu. The functional component is used to display JMenuBar JMenu assembly, when established
after JMenu plurality of components, the components need to be added to JMenu by JMenuBar window assembly.
2.JMenultem
Function of this component is to add menu items to the menu.
3. Select the text box JFileChooser
He can use to select the file you want to open or save.
Self-summary:
This Wednesday, the teacher talked about a notepad, my God, comprehensive and good strong front that also understand, to the back
to those times when the components add functionality of my ignorant force, which fully reflects my knowledge did not grasp firmly , like a
chain, in some places broken, really hard to join, I want the chain to weld, do not say, fire.