自动生成mvp代码过程中,需要根据输入决定模块的名称跟注释等,所以使用弹窗的方式获取。

一、新建Dialog文件

image.png
image.png会添加两个文件,一个是java代码文件,一个是dialog的布局问题

二、布置Dialog布局

image.png
打开Dialog.form文件,会出现布局页面,直接拖动右边的控件即可。

三、代码文件

  1. import javax.swing.*;
  2. import java.awt.event.*;
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. public class MvpDialog extends JDialog {
  7. private JPanel contentPane;
  8. private JButton buttonOK;
  9. private JButton buttonCancel;
  10. private JTextField textField1;
  11. private JTextField textField2;
  12. private JTextField textField3;
  13. private JRadioButton activityRadioButton;
  14. private JRadioButton fragmentRadioButton;
  15. private DialogCallback mCallback;
  16. public MvpDialog(DialogCallback callback) {
  17. mCallback = callback;
  18. setTitle("Mvp create Helper");
  19. setContentPane(contentPane);
  20. setModal(true);
  21. getRootPane().setDefaultButton(buttonOK);
  22. setSize(400, 310);
  23. setLocationRelativeTo(null);
  24. buttonOK.addActionListener(new ActionListener() {
  25. public void actionPerformed(ActionEvent e) {
  26. onOK();
  27. }
  28. });
  29. buttonCancel.addActionListener(new ActionListener() {
  30. public void actionPerformed(ActionEvent e) {
  31. onCancel();
  32. }
  33. });
  34. // call onCancel() when cross is clicked
  35. setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
  36. addWindowListener(new WindowAdapter() {
  37. public void windowClosing(WindowEvent e) {
  38. onCancel();
  39. }
  40. });
  41. // call onCancel() on ESCAPE
  42. contentPane.registerKeyboardAction(new ActionListener() {
  43. public void actionPerformed(ActionEvent e) {
  44. onCancel();
  45. }
  46. }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
  47. }
  48. private void onOK() {
  49. int type=0;
  50. if(activityRadioButton.isSelected()){
  51. type=0;
  52. }
  53. else if(fragmentRadioButton.isSelected()){
  54. type=1;
  55. }
  56. // add your code here
  57. if (null != mCallback) {
  58. mCallback.ok(textField1.getText().trim(), textField2.getText().trim(),textField3.getText().trim(),type);
  59. }
  60. dispose();
  61. }
  62. private void onCancel() {
  63. // add your code here if necessary
  64. dispose();
  65. }
  66. public static void main(String[] args) {
  67. MvpDialog dialog = new MvpDialog(new DialogCallback() {
  68. @Override
  69. public void ok(String moduleName,String author, String intro,int type) {
  70. System.out.println("输出:" + author + "," + moduleName);
  71. }
  72. });
  73. dialog.pack();
  74. dialog.setVisible(true);
  75. System.exit(0);
  76. }
  77. public interface DialogCallback {
  78. void ok(String moduleName, String author, String intro, int type);
  79. }
  80. }

这里最终的代码文件,通过接口回调的方式把数据返回来,主要更改onOk接口接口。