问题描述
//JFrameDemo.javaimportjava.awt.FlowLayout;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjavax.swing.JFrame;importjavax.swing.JButton;importjavax.swing.JTextField;publicclassJFrameDemoextendsJFrame{privateJButtonbutton1=newJButton("增加联系人");privateJButtonbutton2=newJButton("删除联系人");privateJButtonbutton3=newJButton("修改联系人");privateJButtonbutton4=newJButton("查看联系人");privateJTextFieldtf1=newJTextField;publicJFrameDemo(){setSize(300,200);setLocation(400,200);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setLayout(newFlowLayout());add(button1);//增加联系人button1.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){JFrameframe=newJFrame("增加联系人");frame.setSize(300,200);frame.setLocation(600,200);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setLayout(newFlowLayout());frame.setVisible(true);}});add(button2);//删除联系人button2.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){JFrameframe=newJFrame("删除联系人");frame.setSize(300,200);frame.setLocation(600,200);frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);add(button1);}});add(button3);//修改联系人button3.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){JFrameframe=newJFrame("修改联系人");frame.setSize(300,200);frame.setLocation(600,200);frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);frame.setVisible(true);}});add(button4);//查看联系人button4.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){JFrameframe=newJFrame("查看联系人");frame.setSize(300,200);frame.setLocation(600,200);frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);frame.setVisible(true);}});}publicstaticvoidmain(Stringarg[]){JFrameDemoframe=newJFrameDemo();frame.setVisible(true);}}我现在想在增加联系人的新窗口定义连个单行文档输入框和一个确认按钮不知道怎么实现,求大神~!
解决方案
解决方案二:
1)编译不过,privateJTextFieldtf1=newJTextField();2)根据我的写swing/awt的经验,你需要去分析一个较大的应用程序的源代码。看人家怎么写的,然后熟悉API,及技巧。事半功倍。3)写这样的程序,要规划好layout,否则写不出产品级的好程序。4)慢慢来,祝你成为一个快乐的程序员