LY20211211 2022-10-31 18:53 采纳率: 100%
浏览 5
已结题

javaGUI编写MadLib小游戏

javaGUI编写MadLib小游戏:

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.SystemColor;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MadLibGUI extends JFrame {    
    private static final long serialVersionUID = 1L;
    private JTextField txtAdj;
    private JTextField txtColor;
    private JTextField txtPassVerb;
    private JTextField txtNoun;
    private JTextArea txtOutput;
    public void MadLibPrint() {
        //获取用户输入四个单词,生成并输出message
        String message = "";
        String adjText = txtAdj.getText();
        String colorText = txtColor.getText();
        String passVerbText = txtPassVerb.getText();
        String nounText = txtNoun.getText();
        
        message ="The " + colorText + " dragon " 
                + passVerbText + " at the " + adjText 
                + " knight, who rode in a sturdy, giant "
                + nounText + ".";
        txtOutput.setText(message);
    }
    public MadLibGUI() {
        //定制游戏GUI界面
        getContentPane().setBackground(new Color(255, 204, 153));
        getContentPane().setForeground(new Color(250, 249, 197));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Luoyu's MADLIBS APP");
        getContentPane().setLayout(null);

        JLabel lblMadLib = new JLabel("LUOYU'S MADLIB APP");
        lblMadLib.setBackground(new Color(255, 255, 153));
        lblMadLib.setFont(new Font("Kristen ITC", Font.BOLD, 22));
        lblMadLib.setHorizontalAlignment(SwingConstants.CENTER);
        lblMadLib.setBounds(134, 0, 488, 41);
        getContentPane().add(lblMadLib);

        JLabel lblAdj = new JLabel("Enter an Adjective:");
        lblAdj.setHorizontalAlignment(SwingConstants.CENTER);
        lblAdj.setFont(new Font("Century Gothic", Font.BOLD, 20));
        lblAdj.setBounds(27, 82, 206, 29);
        getContentPane().add(lblAdj);

        JLabel lblPassVerb = new JLabel("Enter a Verb ending in -ed:");
        lblPassVerb.setHorizontalAlignment(SwingConstants.CENTER);
        lblPassVerb.setFont(new Font("Century Gothic", Font.BOLD, 18));
        lblPassVerb.setBounds(10, 194, 256, 29);
        getContentPane().add(lblPassVerb);

        JLabel lblColor = new JLabel("Enter a Color:");
        lblColor.setHorizontalAlignment(SwingConstants.CENTER);
        lblColor.setFont(new Font("Century Gothic", Font.BOLD, 20));
        lblColor.setBounds(459, 82, 143, 29);
        getContentPane().add(lblColor);

        JLabel lblNoun = new JLabel("Enter a Noun:");
        lblNoun.setHorizontalAlignment(SwingConstants.CENTER);
        lblNoun.setFont(new Font("Century Gothic", Font.BOLD, 20));
        lblNoun.setBounds(459, 196, 149, 24);
        getContentPane().add(lblNoun);
        
        //btnClic按钮监听并调用执行游戏
        JButton btnClick = new JButton("Press Here to View Your MadLib creation!");
        btnClick.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                MadLibPrint();
            }
        });
        btnClick.setForeground(Color.BLUE);
        btnClick.setBackground(SystemColor.inactiveCaption);
        btnClick.setFont(new Font("Times New Roman", Font.BOLD, 18));
        btnClick.setBounds(177, 300, 403, 29);
        getContentPane().add(btnClick);


        txtAdj = new JTextField();
        txtAdj.setFont(new Font("Tahoma", Font.BOLD, 16));
        txtAdj.setBounds(278, 84, 86, 29);
        getContentPane().add(txtAdj);
        txtAdj.setColumns(10);

        txtColor = new JTextField();
        txtColor.setFont(new Font("Tahoma", Font.BOLD, 16));
        txtColor.setBounds(612, 84, 94, 29);
        getContentPane().add(txtColor);
        txtColor.setColumns(10);

        txtPassVerb = new JTextField();
        txtPassVerb.setFont(new Font("Tahoma", Font.BOLD, 16));
        txtPassVerb.setBounds(278, 196, 86, 29);
        getContentPane().add(txtPassVerb);
        txtPassVerb.setColumns(10);

        txtNoun = new JTextField();
        txtNoun.setFont(new Font("Tahoma", Font.BOLD, 16));
        txtNoun.setBounds(612, 196, 94, 29);
        getContentPane().add(txtNoun);
        txtNoun.setColumns(10);
        
        JTextArea txtOutput = new JTextArea();
        txtOutput.setFont(new Font("FoHarrington", Font.BOLD, 18));
        txtOutput.setBackground(new Color(255, 255, 204));
        txtOutput.setBounds(0, 354, 757, 174);
        getContentPane().add(txtOutput);
        //txtOutput.setText(message);
    }
    public static void main(String[] args) {
        //执行theGame
        // TODO Auto-generated method stub
        MadLibGUI theGame = new MadLibGUI();
        theGame.setSize(new Dimension(780,600));
        theGame.setVisible(true);
    }
}

试运行时,GUI界面可以呈现,单输入单词后,无法输出message。
运行报错:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at MadLibGUI.MadLibPrint(MadLibGUI.java:33)
at MadLibGUI$1.actionPerformed(MadLibGUI.java:78)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
以为是逻辑错误,尝试调整代码顺序,但调整完之后还是如此。
求解答。

  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 11月8日
    • 创建了问题 10月31日

    悬赏问题

    • ¥15 CSS通配符清除内外边距为什么可以覆盖默认样式?
    • ¥15 SPSS分类模型实训题步骤
    • ¥15 求解决扩散模型代码问题
    • ¥15 工创大赛太阳能电动车项目零基础要学什么
    • ¥20 limma多组间分析最终p值只有一个
    • ¥15 nopCommerce开发问题
    • ¥15 torch.multiprocessing.spawn.ProcessExitedException: process 1 terminated with signal SIGKILL
    • ¥15 QuartusⅡ15.0编译项目后,output_files中的.jdi、.sld、.sof不更新怎么解决
    • ¥15 pycharm输出和导师的一样,但是标红
    • ¥15 想问问富文本拿到的html怎么转成docx的