package Interface; import gongneng.BackGroundPanel; import gongneng.FileTest; import gongneng.selfData; import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.IOException; import java.util.ArrayList; import static java.awt.Toolkit.getDefaultToolkit; public class Introduction { FileTest f = new FileTest(); JFrame jf = new JFrame("编辑资料"); static final int WIDTH = 800; static final int HEIGHT = 650; //组装视图 public void init() throws IOException { Toolkit kit = getDefaultToolkit(); Dimension screenSize; screenSize = kit.getScreenSize(); int width = screenSize.width; int height = screenSize.height; int x = (width - WIDTH) / 2; int y = (height - HEIGHT) / 2; jf.setBounds(x, y, WIDTH, HEIGHT); jf.setResizable(false); jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); jf.setIconImage(ImageIO.read(new File("images/beijing.jpg"))); BackGroundPanel bgPanel = new BackGroundPanel(ImageIO.read(new File("images/beijing.jpg"))); //组装登录相关的内容 Box userBox = Box.createVerticalBox(); //昵称和QQ、 Box nickBox = Box.createHorizontalBox(); JLabel nickLabel = new JLabel("昵称"); nickLabel.setFont(new Font("华文行楷",Font.BOLD,30)); JTextField nickField = new JTextField("",15); JLabel qqLabel = new JLabel("qq"); qqLabel.setFont(new Font("华文行楷",Font.BOLD,30)); JTextField qqField = new JTextField("",15); nickBox.add(nickLabel); nickBox.add(Box.createHorizontalStrut(50)); nickBox.add(nickField); nickBox.add(Box.createHorizontalStrut(10)); nickBox.add(qqLabel); nickBox.add(Box.createHorizontalStrut(20)); nickBox.add(qqField); //性别、微信 Box sexBox = Box.createHorizontalBox(); JLabel sexLabel = new JLabel("性别"); sexLabel.setFont(new Font("华文行楷",Font.BOLD,30)); JRadioButton male = new JRadioButton("男",false); JRadioButton female = new JRadioButton("女",false); ButtonGroup group = new ButtonGroup(); group.add(male); group.add(female); JLabel weiXinLabel = new JLabel("微信"); nickLabel.setFont(new Font("华文行楷",Font.BOLD,30)); JTextField weiXinField = new JTextField("",15); sexBox.add(sexLabel); sexBox.add(Box.createHorizontalStrut(50)); sexBox.add(male); sexBox.add(Box.createHorizontalStrut(50)); sexBox.add(female); sexBox.add(Box.createHorizontalStrut(90)); sexBox.add(weiXinLabel); sexBox.add(Box.createHorizontalStrut(30)); sexBox.add(weiXinField); //生日 Box birthBox = Box.createHorizontalBox(); JLabel birthLabel =new JLabel("生日"); birthLabel.setFont(new Font("华文行楷",Font.BOLD,30)); JLabel yearLabel =new JLabel("年"); JLabel monthLabel =new JLabel("月"); JLabel dayLabel =new JLabel("日"); String year[] = {"1987","1988","1989","1990","1991","1992","1993","1994","1995", "1996","1997","1998","1999","2000","2001","2002","2003","2004","2005"}; JComboBox cb1 = new JComboBox(year); String month[] = {"1","2","3","4","5","6","7","8","9","10","11","12"}; JComboBox cb2 = new JComboBox(month); String day[] = {"1","2","3","4","5","6","7","8","9","10", "11","12","13","14","15","16","17","18","19","20", "21","22","23","24","25","26","27","28","29","30","31"}; JComboBox cb3 = new JComboBox(day); birthBox.add(birthLabel); birthBox.add(Box.createHorizontalStrut(50)); birthBox.add(cb1); birthBox.add(Box.createHorizontalStrut(10)); birthBox.add(yearLabel); birthBox.add(Box.createHorizontalStrut(10)); birthBox.add(cb2); birthBox.add(Box.createHorizontalStrut(10)); birthBox.add(monthLabel); birthBox.add(Box.createHorizontalStrut(10)); birthBox.add(cb3); birthBox.add(Box.createHorizontalStrut(10)); birthBox.add(dayLabel); //职业 Box jobBox = Box.createHorizontalBox(); JLabel jobLabel = new JLabel("职业"); jobLabel.setFont(new Font("华文行楷",Font.BOLD,30)); JTextField jobField = new JTextField("",15); jobBox.add(jobLabel); jobBox.add(Box.createHorizontalStrut(50)); jobBox.add(jobField); //爱好 Box hobbyBox = Box.createHorizontalBox(); JLabel hobbyLabel = new JLabel("爱好"); hobbyLabel.setFont(new Font("华文行楷",Font.BOLD,30)); JCheckBox sing = new JCheckBox("唱歌"); JCheckBox dance = new JCheckBox("跳舞"); JCheckBox basketball = new JCheckBox("打篮球"); JCheckBox draw = new JCheckBox("画画"); JCheckBox play = new JCheckBox("打游戏"); JCheckBox read = new JCheckBox("阅读"); JCheckBox act = new JCheckBox("表演"); JCheckBox eat = new JCheckBox("吃"); JCheckBox design = new JCheckBox("设计"); hobbyBox.add(hobbyLabel); hobbyBox.add(Box.createHorizontalStrut(50)); JCheckBox[] hobbyCheck = {basketball,play,read,act,dance,sing,eat,draw,design}; for(int i = 0;i<9;i++){ hobbyBox.add(hobbyCheck[i]); } //签名 Box signBox = Box.createHorizontalBox(); JLabel signLabel = new JLabel("签名"); signLabel.setFont(new Font("华文行楷",Font.BOLD,30)); JTextField signField = new JTextField("",15); signBox.add(signLabel); signBox.add(Box.createHorizontalStrut(50)); signBox.add(signField); //个人说明 Box pgBox = Box.createHorizontalBox(); JLabel pgLabel = new JLabel("个人说明"); pgLabel.setFont(new Font("华文行楷",Font.BOLD,30)); JTextArea pg=new JTextArea("",4,4); pg.setLineWrap(true); //设置文本域中的文本为自动换行 pgBox.add(pgLabel); signBox.add(Box.createHorizontalStrut(20)); pgBox.add(pg); //按钮 Box annvBox = Box.createHorizontalBox(); JButton addButton = new JButton("添 加"); addButton.setFont(new Font("华文行楷",Font.BOLD,25)); JButton shanchuButton = new JButton("删 除"); shanchuButton.setFont(new Font("华文行楷",Font.BOLD,25)); JButton checkButton = new JButton("查看"); checkButton.setFont(new Font("华文行楷",Font.BOLD,25)); annvBox.add(addButton); annvBox.add(Box.createHorizontalStrut(50)); annvBox.add(shanchuButton); annvBox.add(Box.createHorizontalStrut(50)); annvBox.add(checkButton); Box jianjieBox = Box.createHorizontalBox(); JTextArea jianjie = new JTextArea(); jianjie.setVisible(false); jianjieBox.add(jianjie); selfData data = new selfData(); class myListener implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { String sex = ""; Object source = e.getSource(); if(source == addButton){ if(f.find(nickField.getText())!=-1){ JOptionPane.showMessageDialog(null,"已存在!"); nickField.setText(""); qqField.setText(""); sex = ""; weiXinField.setText(""); signField.setText(""); pg.setText(""); return ; } if(male.isSelected()){ sex = "男"; } if(female.isSelected()){ sex = "女"; } String hobby = ""; if(draw.isSelected()) hobby += "画画"; if(basketball.isSelected()) hobby += "打篮球"; if(act.isSelected()) hobby += "表演"; if(eat.isSelected()) hobby += "吃"; if(play.isSelected()) hobby += "打游戏"; if(dance.isSelected()) hobby += "跳舞"; if(sing.isSelected()) hobby += "唱歌"; if(design.isSelected()) hobby += "设计"; if(read.isSelected()) hobby += "阅读"; String c1,c2,c3,job; c1=(String)cb1.getSelectedItem(); c2=(String)cb2.getSelectedItem(); c3=(String)cb3.getSelectedItem(); String birth = c1+"年"+c2+"月"+c3+"日"; String nick = nickField.getText(); String qq = qqField.getText(); String weiXin = weiXinField.getText(); String Job = jobField.getText(); String sign = signField.getText(); String PG = pg.getText(); if (nick.equals("")) nick = null; if (sex.equals("")) sex = null; if (qq.equals("")) qq = null; if (weiXin.equals("")) weiXin = null; if (birth.equals("")) birth = null; if (Job.equals("")) Job = null; if (hobby.equals("")) hobby = null; if (sign.equals("")) sign = null; if (PG.equals("")) PG = null; data.setNick(nick); data.setSex(sex); data.setQq(qq); data.setWeixin(weiXin); data.setBirth(birth); data.setJob(Job); data.setHobby(hobby); data.setSign(sign); data.setPg(PG); f.add(data); f.writefile(); JOptionPane.showMessageDialog(jf,"保存成功!"); } if(source == shanchuButton){ String nick = nickField.getText(); int flag = f.find(nick); if(flag == -1){ JOptionPane.showMessageDialog(null,"没有找到该昵称的人!"); } else{ f.delete(nick); f.writefile(); JOptionPane.showMessageDialog(null,"删除成功!"); } } if(source == checkButton){ new chazhao(); jf.dispose(); } } } myListener my = new myListener(); addButton.addActionListener(my); //删除监听事件 shanchuButton.addActionListener(my); //查找监听事件 checkButton.addActionListener(my); createVStruct(userBox, nickBox, sexBox, birthBox, jobBox); createVStruct(userBox, hobbyBox, signBox, pgBox, annvBox); userBox.add(jianjieBox); bgPanel.add(userBox); jf.add(bgPanel); jf.setVisible(true); } private void createVStruct(Box userBox, Box hobbyBox, Box signBox, Box pgBox, Box annvBox) { userBox.add(Box.createVerticalStrut(30)); userBox.add(hobbyBox); userBox.add(Box.createVerticalStrut(30)); userBox.add(signBox); userBox.add(Box.createVerticalStrut(30)); userBox.add(pgBox); userBox.add(Box.createVerticalStrut(30)); userBox.add(annvBox); } public static void main(String[] args) throws IOException { new Introduction().init(); } }
标签:总结,Box,23.11,27,JCheckBox,add,new,JLabel,createHorizontalStrut From: https://www.cnblogs.com/laobing6214/p/17860374.html