组件之旅

组件之旅

1 源程序

package com.jiemian;
import javax.swing.JFrame;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.Color;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.ButtonGroup;
import javax.swing.JDialog;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JToolBar;
import javax.swing.JComboBox;
import javax.swing.JCheckBox;
import javax.swing.JTextArea;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JScrollBar;
import javax.swing.JPopupMenu;
import java.awt.FlowLayout;
import javax.swing.JTable;
import javax.swing.JSpinner;
import javax.swing.JTree;
import javax.swing.JPanel;
import javax.swing.JList;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
public class ThirdUI  {
 public static void main(String[] args){
  ThirdUI tUI=new ThirdUI();
  tUI.showUI();
 }
 public void showUI(){
  JFrame frame=new JFrame();//创建一窗口对象
  frame.setAlwaysOnTop(true);//设置总在上方
  frame.setTitle("文本编辑器");//标题
  frame.setSize(500,400);//大小
  frame.setDefaultCloseOperation(3);//窗口关闭时程序关闭
  frame.setResizable(true);//可改变窗口大小
  BorderLayout f1=new BorderLayout();
  frame.setLayout(f1);//设置界布局
  
  /**框架上添加菜单栏 注意用set*/
     JMenuBar menuBar=new JMenuBar();
     frame.setJMenuBar(menuBar);
     /**添加菜单到菜单栏*/
     String[] a=new String[]{"文件","编辑","帮助"};
     JMenu[] menu=new JMenu[a.length];
     for(int i=0;i<a.length;i++){
      menu[i]=new JMenu(a[i]);//创建菜单对象
      menuBar.add(menu[i]);//把菜单对象添加到菜单栏中
     }
   
     JMenuItem menuItem1=new JMenuItem("打开");//创建菜单项目对象
     menuItem1.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    showDialog();
   }
  });
    
     JMenuItem menuItem2=new JMenuItem("保存");
     menuItem2.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    showFrame();
   }
  });
     JMenuItem menuItem3=new JMenuItem("退出");
     menuItem3.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    System.exit(0);//系统调用退出
   }
  });
     /**添加菜单下的JMenuItem菜单项目*/
     menu[0].add(menuItem1);
     menu[0].add(menuItem2);
     menu[0].addSeparator();//保存与退出间添加分割线
     menu[0].add(menuItem3);
     JMenu menu_style=new JMenu("字体");//创建子菜单
     JMenu menu_color=new JMenu("颜色");
     /**添加菜单下的JCheckBoxMenuItem复选菜单项目*/
     JCheckBoxMenuItem menuItem4=new JCheckBoxMenuItem("粗体");
     //menuItem4.addActionListener(new ActionListener(){
  // public void actionPerformed(ActionEvent e){
  //  textarea.setFont(new Font(bold));
  // }
  //});
     JCheckBoxMenuItem menuItem5=new JCheckBoxMenuItem("斜体");
     menu_style.add(menuItem4);
     menu_style.add(menuItem5);
    /**添加菜单下的JRadioButtonMenuItem单选菜单项目*/
     JRadioButtonMenuItem menuItem6=new  JRadioButtonMenuItem("红");
     menuItem6.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    textarea.setForeground(new Color(255,0,0));//文本区调用设置字体颜色
   }
  });
     JRadioButtonMenuItem menuItem7=new  JRadioButtonMenuItem("绿");
     menuItem7.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    textarea.setForeground(new Color(0,255,0));//文本区调用设置
   }
  });
     JRadioButtonMenuItem menuItem8=new  JRadioButtonMenuItem("蓝");
     menuItem8.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    textarea.setForeground(new Color(0,0,255));//文本区调用设置
   }
  });
     ButtonGroup buGroup=new ButtonGroup();
     buGroup.add(menuItem6);
     buGroup.add(menuItem7);
     buGroup.add(menuItem8);
     //不能直接把按钮组添加到菜单menu_color.add(buGroup);
     menu_color.add(menuItem6);
     menu_color.add(menuItem7);
     menu_color.add(menuItem8);
     menu[1].add(menu_style);
     menu[1].add(menu_color);
     /**添加工具栏*/
     JToolBar toolBar=new JToolBar();
     frame.getContentPane().add(toolBar,"North");//将工具栏添加到界面北部
     String[] font=new String[]{"宋体","楷体","微软雅黑"};
  JComboBox  combobox1=new JComboBox(font);
  toolBar.add(combobox1);
  
     String[] size=new String[]{"一号","二号","三号","四号","五号"};
  JComboBox combobox2=new JComboBox(size);
  toolBar.add(combobox2);
  
  JCheckBox checkbox1=new JCheckBox("粗体");
  JCheckBox checkbox2=new JCheckBox("斜体");
  toolBar.add(checkbox1);
  toolBar.add(checkbox2);
  
  JRadioButton radiobutton1=new JRadioButton("红");
  radiobutton1.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
     textarea.setForeground(new Color(255,0,0));//文本区调用设置字体颜色
    }
   });
  JRadioButton radiobutton2=new JRadioButton("绿");
  radiobutton2.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
     textarea.setForeground(new Color(0,255,0));//文本区调用设置
    }
   });
  JRadioButton radiobutton3=new JRadioButton("蓝");
  radiobutton3.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
     textarea.setForeground(new Color(0,0,255));//文本区调用设置
    }
   });
  ButtonGroup bg=new ButtonGroup();
  bg.add(radiobutton1);
  bg.add(radiobutton2);
  bg.add(radiobutton3);
  toolBar.add(radiobutton1);
  toolBar.add(radiobutton2);
  toolBar.add(radiobutton3);
  
  JScrollPane scrollpane=new JScrollPane(textarea);//滚动窗格中添加文本区,当文本内容超过窗口大小则自动显示滚动条
  frame.add(scrollpane);
  //frame.getContentPane().add(textarea,"center");
  /**创建快捷菜单*/
  final JPopupMenu popupmenu=new JPopupMenu();
  JMenuItem mi1=new JMenuItem("复制");
  //添加复制事件
  mi1.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    textarea.copy();//注意由textarea调用copy()
   }
  });
  JMenuItem mi2=new JMenuItem("黏贴");
  //添加黏贴事件
  mi2.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    textarea.paste();
   }
  });
  JMenuItem mi3=new JMenuItem("剪切");
  //添加剪切事件
  mi3.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    textarea.cut();
   }
  });
  popupmenu.add(mi1);
  popupmenu.add(mi2);
  popupmenu.add(mi3);
  textarea.add(popupmenu);
  textarea.addMouseListener(new MouseAdapter(){//文本区中添加事件(鼠标右击文本区则在此弹出快捷菜单)
   public void mouseClicked(MouseEvent e){
    if(e.getModifiers()==MouseEvent.BUTTON3_MASK){//判断单击的是不是鼠标右键
     popupmenu.show(textarea,e.getX(),e.getY());
    }
   }
  });
  frame.setVisible(true);//可见
   }
 public void showDialog(){
  JDialog dialog=new JDialog();
  FlowLayout f2=new FlowLayout();
  dialog.setLayout(f2);//设置界布局
  dialog.setTitle("我的对话");
  dialog.setAlwaysOnTop(true);
  dialog.setSize(400, 400);
  dialog.setDefaultCloseOperation(1);
  JLabel label1=new JLabel("欢迎进入陈雪平的世界");
  dialog.add(label1);
  JTable table=new JTable(4,4);
  dialog.add(table);
  JTree tree=new JTree();
  dialog.add(tree);
  JSpinner spinner=new JSpinner();
  dialog.add(spinner);
  JButton button=new JButton("确定");
  button.setSize(20, 20);
  JPanel panel=new JPanel();
  panel.setSize(40, 50);
  panel.setBackground(java.awt.Color.BLUE);//设置背景色
  panel.add(button);
  dialog.add(panel);
  String[] a=new String[]{"我","你","她"};
  JList list=new JList(a);
  dialog.add(list);
  dialog.setVisible(true);
  
 }
 
 public void showFrame(){
  JFrame frame1=new JFrame("请先登录");
  FlowLayout f1=new FlowLayout();
  frame1.setLayout(f1);//设置界布局
  frame1.setSize(200, 200);
  JLabel label1=new JLabel("账号");//创建一个标签对象并设置名字为账号
  JLabel label2=new JLabel("密码");
  JTextField tfield1=new JTextField(20);//创建一个单行文本编辑框
  JPasswordField tfield2=new JPasswordField(20);
  JButton button1=new JButton("登陆");//创建一个按钮名字为登陆
  frame1.add(label1);
  frame1.add(tfield1);
  frame1.add(label2);
  frame1.add(tfield2);
  frame1.add(button1);
  frame1.setVisible(true);
  
 }
 final JTextArea textarea=new JTextArea();
 
}

2 运行结果
  组件之旅_第1张图片 组件之旅_第2张图片 组件之旅_第3张图片 组件之旅_第4张图片 组件之旅_第5张图片 组件之旅_第6张图片 组件之旅_第7张图片
3看程序分析

你可能感兴趣的:(组件之旅)