<img src="http://img.blog.csdn.net/20160306200550019?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
package UserJFrame;import java.awt.BorderLayout;import java.awt.Button;import java.awt.Color;import java.awt.Dimension;import java.awt.FileDialog;import java.awt.Font;import java.awt.GraphicsEnvironment;import java.awt.GridLayout;import java.awt.Label;import java.awt.MenuBar;import java.awt.Panel;import java.awt.TrayIcon.MessageType;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.font.TextLayout;import javax.swing.ButtonGroup;import javax.swing.GroupLayout;import javax.swing.GroupLayout.Group;import javax.swing.JButton;import javax.swing.JCheckBox;import javax.swing.JComboBox;import javax.swing.JDialog;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JPopupMenu;import javax.swing.JRadioButton;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JTextField;import javax.swing.JToolBar;import javax.swing.LayoutStyle;import javax.swing.filechooser.FileNameExtensionFilter;import javax.swing.plaf.basic.BasicBorders.RadioButtonBorder;import javax.swing.text.StyledEditorKit.BoldAction;public class UserJFrame extends JFrame implements ActionListener, MouseListener {private JButton btnAdd;private JTextArea textarea;private JRadioButton btnMan,btnFelman;private JTextField tfdNumber,tfdName;private JComboBox comProvince,comCitys;private int number=1;private Object[][] citys;private JTextField tfd1;private JMenuItem jmenuItemcolor[]=null;private JDialog jdialog1,jdialogsave;private Font font1;private JPopupMenu jpopupmenu;private JPanel jpanel1,jpanel2;private JComboBox combofonename,combofonesize;private JCheckBox checkBold,checkItalic;private JRadioButton radioColor[];private Font font;public UserJFrame(Object[] province, Object[][] citys) {super("个人信息统计");this.citys=citys;this.setBounds(200, 200, 650, 400);this.getContentPane().setBackground(Color.lightGray);this.getContentPane().setLayout(new BorderLayout());this.setDefaultCloseOperation(EXIT_ON_CLOSE);//设置菜单栏addMymenu();//调用下面声明的菜单栏相关代码jpanel1=new JPanel();//用来存放工具栏的面板jpanel2=new JPanel();//用来搞下面的文本区和信息添加区的面板JToolBar jtoolbar=new JToolBar();//给工具栏开空间//以下为工具栏的内容,包括字体大小设置,字体设置,字形选择框//获取系统字体GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();String fonesName[]=ge.getAvailableFontFamilyNames();combofonename=new JComboBox(fonesName);combofonename.addActionListener(this);//添加字体的监听jtoolbar.add(combofonename);//字体大小设置String fonesSize[]={"20","30","40","50","60","70"};combofonesize=new JComboBox(fonesSize);combofonesize.addActionListener(this);jtoolbar.add(combofonesize);combofonesize.setEditable(true);//字体选择checkBold=new JCheckBox("粗体");checkItalic=new JCheckBox("斜体");jtoolbar.add(checkBold);jtoolbar.add(checkItalic);//颜色选择String colorStr[]={"红","绿","蓝","黑"};radioColor=new JRadioButton[colorStr.length];ButtonGroup group=new ButtonGroup();for(int i=0;i<colorStr.length;i++){radioColor[i]=new JRadioButton(colorStr[i]);jtoolbar.add(radioColor[i]);group.add(radioColor[i]);radioColor[i].addActionListener(this);}jpanel1.add(jtoolbar);//工具栏加到jpanel1中this.getContentPane().add(jpanel1, BorderLayout.NORTH);//设定jpanel1的位置并添加到整个面板中this.getContentPane().add(jpanel2);//jpanel2添加进去jpanel2.setLayout(new GridLayout(1, 2));//把界面1分2,左边为文本框眼,右边为信息添加部分栏textarea=new JTextArea();//textarea.setEditable(false); //如果有这条代码,则文本框不可编辑jpanel2.add(new JScrollPane(textarea));JPanel jpanel=new JPanel(new GridLayout(7, 1));jpanel2.add(jpanel);//把各种元素加进右边面板tfdNumber=new JTextField("1");tfdNumber.setEditable(false);jpanel.add(tfdNumber);tfdName=new JTextField("姓名:");jpanel.add(tfdName);JPanel jpanelrd=new JPanel(new GridLayout(1,2));btnMan=new JRadioButton("男",true);btnFelman=new JRadioButton("女");jpanelrd.add(btnMan);jpanelrd.add(btnFelman);jpanel.add(jpanelrd);ButtonGroup btngroup=new ButtonGroup();// 使其中的按键(男,女)互斥,只能选择一个,所以把他们放在同一个按键组里btngroup.add(btnMan);btngroup.add(btnFelman);//省份组合框comProvince=new JComboBox(province);comProvince.addActionListener(this);jpanel.add(comProvince);comCitys=new JComboBox(citys[0]);jpanel.add(comCitys);tfd1=new JTextField("备注:");jpanel.add(tfd1);btnAdd=new JButton("添加");btnAdd.addActionListener(this);jpanel.add(btnAdd);//添加鼠标右键快捷菜单jpopupmenu=new JPopupMenu();String popumenuItem[]={"粘贴","复制","剪切"};JMenuItem popumenuItemadd[]=new JMenuItem[popumenuItem.length];for(int i=0;i<popumenuItem.length;i++){popumenuItemadd[i]=new JMenuItem(popumenuItem[i]);jpopupmenu.add(popumenuItemadd[i]);popumenuItemadd[i].addActionListener(this);}textarea.add(jpopupmenu);textarea.addMouseListener(this);this.setVisible(true);}private void addMymenu() {JMenuBar jmenubar=new JMenuBar();setJMenuBar(jmenubar);String menustr[]={"文件","编辑","帮助"};JMenu menu[]=new JMenu[menustr.length];for(int i=0;i<menustr.length;i++){menu[i]=new JMenu(menustr[i]);jmenubar.add(menu[i]);}//"文件"菜单的子菜单JMenuItem open=new JMenuItem("打开");open.setActionCommand("open");open.addActionListener(this);menu[0].add(open);JMenuItem save=new JMenuItem("保存");menu[0].add(save);menu[0].addSeparator();JMenuItem past=new JMenuItem("粘贴");past.addActionListener(this);menu[0].add(past);JMenuItem copy=new JMenuItem("复制");copy.addActionListener(this);menu[0].add(copy);JMenuItem cut=new JMenuItem("剪切");cut.addActionListener(this);menu[0].add(cut);menu[0].addSeparator();JMenuItem exit=new JMenuItem("退出");exit.setActionCommand("exit");exit.addActionListener(this);menu[0].add(exit);//"编辑 "菜单的子菜单JMenu wordstyle=new JMenu("字形");menu[1].add(wordstyle);JMenu colorstyle=new JMenu("颜色设置");menu[1].add(colorstyle);//设置“字形的二级菜单”String wordstyle1[]={"粗体","斜体"};JMenuItem jmenuitem[]=new JMenuItem[wordstyle1.length];for(int i=0;i<wordstyle1.length;i++){jmenuitem[i]=new JMenuItem(wordstyle1[i]);wordstyle.add(jmenuitem[i]);jmenuitem[i].addActionListener(this);}//设置“颜色设置”二级菜单String colorstyle1[]={"红","绿","蓝","黑"};jmenuItemcolor=new JMenuItem[colorstyle1.length];for(int i=0;i<colorstyle1.length;i++){jmenuItemcolor[i]=new JMenuItem(colorstyle1[i]);colorstyle.add(jmenuItemcolor[i]);jmenuItemcolor[i].addActionListener(this);}JMenuItem jmenuitemhelp=new JMenuItem("相关帮助咨询");jmenuitemhelp.addActionListener(this);menu[2].add(jmenuitemhelp);}@Overridepublic void actionPerformed(ActionEvent e) {if(e.getSource()==btnAdd){String aline;//添加名字和编号aline=this.number+","+tfdName.getText();//添加性别if(btnMan.isSelected()){aline=aline+","+btnMan.getText();}if(btnFelman.isSelected()){aline=aline+","+btnFelman.getText();}//添加省份aline=aline+","+comProvince.getSelectedItem();//添加市级aline=aline+","+comCitys.getSelectedItem();//添加备注aline=aline+","+tfd1.getText();tfd1.setText("备注:");textarea.append(aline+"\n");this.number++;tfdNumber.setText(""+this.number);tfdName.setText("姓名:");}else if(e.getSource()==comProvince){int i=comProvince.getSelectedIndex();comCitys.removeAllItems();System.out.println(citys[i].length);for(int j=0;j<citys[i].length;j++){comCitys.addItem(citys[i][j]);}}else if(e.getSource() instanceof JMenuItem||e.getSource() instanceof JRadioButton){//设置颜色的监听Color color=null; //用传参的思维做颜色if(e.getActionCommand().equals("红")){color=new Color(255,0,0);}if(e.getActionCommand().equals("绿")){color=new Color(0,255,0);}if(e.getActionCommand().equals("蓝")){color=new Color(0,0,255);}if(e.getActionCommand().equals("黑")){color=new Color(0,0,0);}textarea.setForeground(color);//设置帮助的监听if(e.getActionCommand().equalsIgnoreCase("相关帮助咨询")){jdialog1=new JDialog(this,true);jdialog1.setSize(330, 100);int lx=getX()+20;int ly=getY()+20;jdialog1.setLocation(lx, ly);jdialog1.add(new Label("相关帮助请咨询电话10010,最终解释权归联通公司所有!"));jdialog1.setVisible(true);}//设置字形的监听if(e.getActionCommand().equals("粗体")){font1=new Font(getName(),20, 20);textarea.setFont(font1);}if(e.getActionCommand().equals("斜体")){}//退出的监听if(e.getActionCommand().equalsIgnoreCase("exit")){int option=JOptionPane.showConfirmDialog(this, "你确定保存并退出么?");if(option==JOptionPane.OK_OPTION){System.out.println("保存中...");System.exit(0);}if(option==JOptionPane.NO_OPTION){System.exit(0);}if(option==JOptionPane.CANCEL_OPTION){return;}}//保存的监听if(e.getActionCommand().equalsIgnoreCase("复制")){textarea.copy();}//粘贴监听if(e.getActionCommand().equalsIgnoreCase("粘贴")){textarea.paste();}//剪切监听if(e.getActionCommand().equalsIgnoreCase("剪切")){textarea.cut();}//打开的监听if(e.getActionCommand().equals("open")){openFile();}}//字形的监听/*if(e.getSource() instanceof JComboBox ){String fontName=(String) combofonename.getSelectedItem();int fontSize=0;String strSize=(String) combofonesize.getSelectedItem();int style = changeStyle(e);font=new Font(fontName, style, fontSize);textarea.setFont(font);}}private int changeStyle(ActionEvent e) {// TODO Auto-generated method stubreturn 0;*/}private void openFile() {FileDialog openFileDlg = new FileDialog(this, "打开文件", FileDialog.LOAD); openFileDlg.setVisible(true);}@Overridepublic void mouseClicked(MouseEvent e) {if(e.getModifiers()==MouseEvent.BUTTON3_MASK){jpopupmenu.show(textarea,e.getX(), e.getY());}}public void mousePressed(MouseEvent e) {}public void mouseReleased(MouseEvent e) {}public void mouseEntered(MouseEvent e) {}public void mouseExited(MouseEvent e) {}public static void main(String[] args) {Object province[]={"四川省","湖南省","江苏省"};Object citys[][]={{"成都市","遂宁市","南充市","德阳市","绵阳市"}, {"益阳市","长沙市","株洲市","湘潭市","岳阳市"}, {"南京市","苏州市","无锡市"}};new UserJFrame(province,citys);}}<img src="http://img.blog.csdn.net/20160306201345897?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />