我的JButtons无法正常工作,我的GUI应用程序有什么问题?

时间:2017-03-15 00:16:57

标签: java swing jbutton

我试图解决这个问题大约2个小时,但似乎无法弄清楚我的代码有什么问题。如果有人能帮助我那会很棒。下面是代码,如果你运行它并点击"创建"或"退出"都行不通。

private JPanel contentPane;
private JTextField textField; 
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
private JTextField textField_5;
private JTextField textField_7;
private JTextField textField_8;
private JTextField textField_9;
private JTextField textField_10;
protected String msg;
private JLabel label;

/**
 * Launch Application.
 */

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                ProjectPart1 frame = new ProjectPart1();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    }

/**
 * Create the frame.
 */

public ProjectPart1() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 795, 555);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    // html tag
    msg = "";
    HTMLEditorKit kit = new HTMLEditorKit();

    // it prints html message and java text
    JTextPane textPane = new JTextPane(); //msgPane
    textPane.setBackground(Color.blue); 
    textPane.setBounds(359, 11, 410, 494);
    textPane.setEditorKit(kit);
    textPane.setText(msg);
    contentPane.add(textPane);

    // Prints the text in the label
    JLabel madLib = new JLabel("Mad LIB");
    madLib.setFont(new Font("Tahoma", Font.BOLD, 18));
    madLib.setBounds(49, 4, 126, 32);
    contentPane.add(madLib);

    //Prints the text in the label
    JLabel label1 = new JLabel("Enter your name ");
    label1.setFont(new Font("Onyx", Font.BOLD, 12));
    label1.setBounds(10, 47, 135, 15);
    contentPane.add(label1);

    // Generates textField
    textField = new JTextField();
    textField.setBounds(150, 47, 95, 20);
    contentPane.add(textField);
    textField.setColumns(10);

    // Prints the text in the label
    JLabel label2 = new JLabel("Enter a random name ");
    label2.setFont(new Font("Onyx", Font.BOLD, 12));
    label2.setBounds(10, 73, 206, 14);
    contentPane.add(label2);

    // Generates input1
    JTextField input1 = new JTextField();
    input1.setBounds(150, 71, 86, 20);
    contentPane.add(input1); //frame.add
    input1.setColumns(10);

    // Prints the text in label
    JLabel label3 = new JLabel("Enter a state (EX. NJ) ");
    label3.setFont(new Font("Onyx", Font.BOLD, 12));
    label3.setBounds(10, 99, 152, 14);
    contentPane.add(label3);

    // Generates input2
    JTextField input2 = new JTextField();
    input2.setBounds(150, 97, 86, 20);
    contentPane.add(input2);
    input2.setColumns(10);

    //Prints the text in label
    JLabel label4 = new JLabel("Enter an adjective ");
    label4.setFont(new Font("Onyx", Font.BOLD, 12));
    label4.setBounds(10, 124, 174, 15);
    contentPane.add(label4);

    //Generates input3
    JTextField input3 = new JTextField();
    input3.setBounds(150, 122, 86, 20);
    contentPane.add(input3);
    input3.setColumns(10);

    // Prints the text in label
    JLabel label5 = new JLabel("Enter a noun ");
    label5.setFont(new Font("Onyx", Font.BOLD, 12));
    label5.setBounds(10, 151, 152, 14);
    contentPane.add(label5);

    // Prints the text in label
    JLabel label6 = new JLabel("Enter a verb ");
    label6.setFont(new Font("Onyx", Font.BOLD, 12));
    label6.setBounds(10, 176, 183, 14 );
    contentPane.add(label6);

    // Prints the text in label
    JLabel label7 = new JLabel("Enter a noun ");
    label7.setFont(new Font("Onyx", Font.BOLD, 12));
    label7.setBounds(10, 201, 157, 14);
    contentPane.add(label7);

    // Prints the text in label
    JLabel label8 = new JLabel("Enter a verb ");
    label8.setFont(new Font("Onyx", Font.BOLD, 12));
    label8.setBounds(10, 226, 135, 14);
    contentPane.add(label8);

    // Prints the text in label
    JLabel label9 = new JLabel("Enter an adjective ");
    label9.setFont(new Font("Onyx", Font.BOLD, 12));
    label9.setBounds(10, 274, 189, 14);
    contentPane.add(label9);

    //Result print
    label = new JLabel();


    label.setBounds(200, 150, 50, 50);
    contentPane.add(label);

    // Prints the date and time
    DateFormat dt = new SimpleDateFormat("MM/dd/yy HH:mm");
    Date date = new Date();

    JButton btnCreateMSG = new JButton("Create");
    btnCreateMSG.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            String msg1 = msg;

            // Get text from user html inside java
            String txte1 = "<b>" + textField.getText() + "</b>" + "<br />" + "128 Congress Street" + "<br />" + "Jersey City NJ 07003";
            String txte2 = "<p>March 14, 2017 </p>";
            String txte3 = "" + "<br />" + "<b>" + textField_1.getText() + "</b>" + "<br />" + "184 Newark Street" + "<br />" + "Jersey City";
            String txte4 = "<p>Dear <b>" + textField_1.getText() + "</b></p>";
            String txte5 = "<p> Hey, I am writing this <b>" + textField_9.getText() + 
                    "</b> to invite you to my house during this spring break. Since we did a lot of work during the semester"
                    + textField_5.getText() + "</b>, but we would also do a lot of fun activities like playing games <b>" + textField_8.getText()
                    + "</b>, going out for dinner and travel to <b>" + textField_2.getText()
                    + "</b>. We will also study together for <b>" + textField_7.getText()
                    + "</b> exam, and we will also read funny <b>" + textField_4.getText()
                    + "</b> as well. I guarantee that you would have an amazing spring break. It would be <b>"
                    + textField_3.getText() + "</b> if you come to my house on <b>" + dt.format(date)
                    + "</b>, and I am <b>" + textField_10.getText()
                    + "</b> to meet you over the spring break. See you soon.</p>";
            String txte6 = "<p> Best Regards,</p>";
            String txte7 = "<p><b>" + "<br />" + "" + textField.getText() + "</b></p>";

            msg1 += txte1;
            msg1 += txte2;
            msg1 += txte3;
            msg1 += txte4;
            msg1 += txte5;
            msg1 += txte6;
            msg1 += txte7;

            madLib.setText(msg1);

        }
    });

    btnCreateMSG.setBounds(26,  311,  100,  30);
    contentPane.add(btnCreateMSG);

    JButton btnExit = new JButton("Exit");

    btnExit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            // A reset button
            textField.setText("");
            textField_1.setText("");
            textField_2.setText("");
            textField_3.setText("");
            textField_4.setText("");
            textField_5.setText("");
            textField_7.setText("");
            textField_8.setText("");
            textField_9.setText("");
            textField_10.setText("");
        }
    });
    btnExit.setBounds(235,  309,  95,  32);
    contentPane.add(btnExit);

    // Generates input4
    JTextField textField_4 = new JTextField();
    textField_4.setBounds(150, 149, 86, 20);
    contentPane.add(textField_4);
    textField_4.setColumns(10);

    // Generates input5
    JTextField textField_5 = new JTextField();
    textField_5.setBounds(150, 174, 86, 20);
    contentPane.add(textField_5);
    textField_5.setColumns(10);

    // Generates input6
    JTextField textField_7 = new JTextField();
    textField_7.setBounds(150, 199, 86, 20);
    contentPane.add(textField_7);
    textField_7.setColumns(10);

    // Generates input7
    JTextField textField_8 = new JTextField();
    textField_8.setBounds(150, 224, 86, 20);
    contentPane.add(textField_8);
    textField_8.setColumns(10);

    // Generates input8
    JTextField textField_9 = new JTextField();
    textField_9.setBounds(150, 247, 86, 20);
    contentPane.add(textField_9);
    textField_9.setColumns(10);

    // Generates input9
    JTextField textField_10 = new JTextField();
    textField_10.setBounds(150, 272, 86, 20);
    contentPane.add(textField_10);
    textField_10.setColumns(10);

0 个答案:

没有答案