日期:2014-05-16  浏览次数:20425 次

JScrollPane和JTextArea,JMenuBar、JMenu和JMenuItem之间的组合关系

package menuta;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import ioutil.*;
public class DemoMenusAndTextAreas extends JFrame{
????? //////////////////////////////////////////////////////// main
??? public static void main(String[] args){
????? JFrame theFrame = new DemoMenusAndTextAreas();
????? theFrame.setSize(350, 250);
????? theFrame.setVisible(true);??????? ?
?? }
??? //////////////////////////////////////////////////////// Model
??? // none
//////////////////////////////////////////////////////// View
// Create controls
?? private JTextArea taDisplay???? = new JTextArea("Greetings!\n");
? ?
?? private JMenu???? muFile??????? = new JMenu("File");???? ?
?? private JMenuItem miFileNew???? = new JMenuItem("New");
? ?
?? private JMenu???? muEdit??????? = new JMenu("Edit");????? ?
?? private JMenuItem miEditAppend? = new JMenuItem("Append");
?? private JMenuItem miEditReplace = new JMenuItem("Replace");
? public DemoMenusAndTextAreas(){
??? // Set title
????? setTitle("Menus and TextArea Demo");
????? // Create container and layout
????? Container contentPane = getContentPane();
????? EasyGridLayout layout = new EasyGridLayout();
????? contentPane.setLayout (layout);
? // Set constraints and add controls to container
????? JScrollPane spDisplay = new JScrollPane(taDisplay);
????? layout.setConstraints(spDisplay? , 1,1,1,1);
????? contentPane.add(spDisplay);
?//
1.组合关系,JScrollPane组件的使用,add其它组件后,使它成为JScrollPane的属性,所以容器添加JScrollPane对象即可。
?? // Create the menu bar and add menus and menu items
????? JMenuBar menuBar = new JMenuBar();
???? this.setJMenuBar(menuBar);/
/this是theFrame对象,和contentPane是有区别的。
????? menuBar.add(muFile);
????? menuBar.add(muEdit);
????? muFile.add(miFileNew);
//2.组合关系,将JMenu作为JmenuBar的属性,然后将JMenuItem作为JMenu的属性,能够使得JMenuItem作为MenuBar的属性。
????? muEdit.add(miEditAppend);
????? muEdit.add(miEditReplace);

????? // Specify listeners
????? miFileNew.addActionListener(new FileNewListener());
????? miEditAppend.addActionListener(new EditAppendListener());
????? miEditReplace.addActionListener(new EditReplaceListener());
????? this.addWindowListener(new MyWindowAdapter());? ?
?? }
???? //////////////////////////////////////////////////////// Controller
// File-new listener
?? private class FileNewListener implements ActionListener{
????? public void actionPerformed(ActionEvent e){
???????? taDisplay.setText("");//
新建文件是利用JComponentText对象setText(" ")空,或实现replace文件内容的功能。
????? }
?? }
?// Edit-append listener
?? private class EditAppendListener implements ActionListener{
????? public void actionPerformed(ActionEvent e){
???????? taDisplay.append("Appending one more line.\n");//
利用JTextArea的append方法实现后面添加。
????? }
?? }
?// Edit-modify listener
?? private class EditReplaceListener implements ActionListener{
????? public void actionPerformed(ActionEvent e){
???????? taDisplay.setText("Setting a new line of text.\n");//setText覆盖。
????? }
?? }
??? // Window listener
?? private class MyWindowAdapter extends WindowAdapter{
????? public void windowClosing (WindowEvent e){
???????? System.exit(0);
????? }
?? }
}