【问题标题】:Why won't main method print anything?为什么 main 方法不打印任何东西?
【发布时间】:2019-02-20 20:17:34
【问题描述】:

我一直试图找出我的代码有什么问题。我必须构建一个 GUI,并且没有错误。程序构建成功,但没有弹出 GUI。所以在 main 方法中,我注释掉了 GUI 编程并添加了一个简单的System.out.println("hello");,但它做同样的事情,即它构建成功,但不打印任何东西。有人可以告诉我有什么问题吗?谢谢!

/*
 * To change this license header, choose License Headers in Project 
Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import java.awt.*;
import javax.swing.*;
import java.awt.Event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GUI extends JFrame {

    GridLayout g = new GridLayout(5, 2);
    private JLabel baseIn = new JLabel("Base Input");
    private JLabel heightIn = new JLabel("Height Input");
    private JTextField base = new JTextField();
    private JTextField height = new JTextField();
    private JTextField area = new JTextField();
    private JButton calc = new JButton("Calculate Area");

    public GUI() {
        super("Triangle Area Calculator");
        setSize(500, 300);
        setLayout(g);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        add(baseIn);
        add(heightIn);
        add(base);
        add(height);
        add(area);
        add(calc);
        area.setEditable(false);
        calc.addActionListener((ActionListener) this);

        pack();
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        try {
            double bInput = Integer.valueOf(base.getText());
            double hInput = Integer.valueOf(height.getText());
            double aOutput = 0.5*bInput*hInput;
            area.setText("Area of your triangle is: " + aOutput);
        } catch (NumberFormatException n) {
            System.out.println(n.getMessage());
        }
    }


    public static void main(String[] args) {
        /*JFrame frame = new JFrame();
        GUI one = new GUI();

        frame.getContentPane().add(one);
        frame.pack();
        frame.setVisible(true);*/

        System.out.println("hello world");

    }

}

【问题讨论】:

  • 你的代码是如何运行的?
  • @Mureinik 我正在使用 netbeans,绿色播放按钮(运行项目,(F6))
  • 您还必须告诉 NetBeans 您的项目的主要类是什么。在您的情况下,您的主要课程似乎是gui.GUI。你做到了吗?
  • @MikeNakis 我刚刚检查了项目属性,主类已经是gui.GUI,所以我不确定它为什么会这样做。
  • 另外,您希望输出显示在哪里?确认您没有无意中隐藏了 NetBeans 终端。

标签: java netbeans main


【解决方案1】:

首先,回到基本代码...

public class GUI extends JFrame {
    //...

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        GUI one = new GUI();

        frame.getContentPane().add(one);
        frame.pack();
        frame.setVisible(true);
    }

}

将失败,因为您无法将基于窗口的组件添加到窗口。作为一般的经验法则,您应该避免直接覆盖JFrame(和其他顶级容器),而倾向于使用不太复杂的东西,例如JPanel

public class GUI extends JPanel {
    //...
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new GUI());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

下一步...

calc.addActionListener((ActionListener) this);

您需要执行强制转换才能使代码工作这一事实清楚地表明存在其他问题,这可能会导致运行时错误并使您的程序崩溃。或许您应该先阅读 How to write a Action ListenerHow to Use Buttons, Check Boxes, and Radio Buttons 以更好地了解 API 的工作原理

这通过使用@Override 注释得到进一步支持,当你“认为”你正在实现或覆盖现有功能时应该使用它......

@Override
public void actionPerformed(ActionEvent e) {
    //...
}

这将无法编译,因为您没有实现任何现有功能。此功能由您未实现的 ActionListener 接口描述。

虽然您可以直接实现此接口,但我更愿意避免这样做,因为它公开了其他类不应访问的功能,并且您冒着构建“上帝”方法的风险,这绝不是一个好主意.

相反,我更喜欢使用 Java 的 Anonymous Classes,它为将功能隔离到单个用例提供了更好的方法,例如...

calc.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        try {
            double bInput = Integer.valueOf(base.getText());
            double hInput = Integer.valueOf(height.getText());
            double aOutput = 0.5 * bInput * hInput;
            area.setText("Area of your triangle is: " + aOutput);
        } catch (NumberFormatException n) {
            System.out.println(n.getMessage());
        }
    }
});

可运行示例

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class GUI extends JPanel {

    GridLayout g = new GridLayout(5, 2);
    private JLabel baseIn = new JLabel("Base Input");
    private JLabel heightIn = new JLabel("Height Input");
    private JTextField base = new JTextField();
    private JTextField height = new JTextField();
    private JTextField area = new JTextField();
    private JButton calc = new JButton("Calculate Area");

    public GUI() {
        setLayout(g);
        add(baseIn);
        add(heightIn);
        add(base);
        add(height);
        add(area);
        add(calc);
        area.setEditable(false);
        calc.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    double bInput = Integer.valueOf(base.getText());
                    double hInput = Integer.valueOf(height.getText());
                    double aOutput = 0.5 * bInput * hInput;
                    area.setText("Area of your triangle is: " + aOutput);
                } catch (NumberFormatException n) {
                    System.out.println(n.getMessage());
                }
            }
        });
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new GUI());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

Netbeans 属性...

现在,如果仍然无法为您带来结果,您需要确保将您的 GUI 类配置为“主类”。

首先右键单击 Netbeans 项目节点并选择“属性”(位于底部)。

在“项目属性”中,从左侧下方的“构建”选项中选择“运行”。

确保您的GUI 类被标记为“主类”,如果不是,请使用“浏览”查找它

【讨论】:

    【解决方案2】:

    试试这个

    calc.addActionListener(new OptionButtonHandler());
    

    我添加了一个实现 ActionListener 的 optionButtonHandler 类。我检查了我的 IDE,我能够得到三角形的面积。

    private class OptionButtonHandler implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        try {
            double bInput = Integer.valueOf(base.getText());
            double hInput = Integer.valueOf(height.getText());
            double aOutput = 0.5 * bInput * hInput;
            area.setText("Area of your triangle is: " + aOutput);
        } catch (NumberFormatException n) {
            System.out.println(n.getMessage());
        }
    }
    }
    

    在您的情况下,GUI 不是 ActionListener,因此会失败。

    【讨论】:

      【解决方案3】:
      1. 你能在Netbeans中用main方法右击文件吗,你应该在那里看到运行选项,选择它。这将允许您设置您的主要方法。在此之后,对绿色播放按钮的后续点击应该可以工作。

      2. 您不需要将 Frame 类强制转换为 ActionListener。相反,让它实现 ActionListener 接口,您的按钮操作代码应该可以工作。但在未来,最好添加逻辑来检测触发动作的组件。

      【讨论】:

        【解决方案4】:

        我不知道,但是你怎么写这个:

        calc.addActionListener((ActionListener) this);
        

        你的对象(this)是一个JFrame,你应该先添加'implements ActionListener',或者创建一个单独的实现......

        下一个错误,你做:

        GUI one = new GUI();
        frame.getContentPane().add(one);
        

        GUI扩展了JFrame,它是一个JFrame,你不能在另一个JFrame中添加一个JFrame!

        我通过添加“implements ActionListener”进行了测试,它运行了,但仍然存在一些错误;) 复制/粘贴需要智慧,哼^^

        编辑

        这段代码并不完美(而且非常丑陋),但它适用于您的示例:

        package com.mead.helmet.core;
        
        import java.awt.GridLayout;
        import java.awt.event.ActionEvent;
        import java.awt.event.ActionListener;
        
        import javax.swing.JButton;
        import javax.swing.JFrame;
        import javax.swing.JLabel;
        import javax.swing.JPanel;
        import javax.swing.JTextField;
        import javax.swing.WindowConstants;
        
        public class GUI extends JPanel implements ActionListener {
        
        public static void main(final String[] args) {
        
            JFrame frame = new JFrame("Triangle Area Calculator");
        
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            GUI one = new GUI();
        
            frame.getContentPane().add(one);
            frame.pack();
            frame.setVisible(true);
        
            System.out.println("hello world");
        
        }
        
        GridLayout g = new GridLayout(5, 2);
        private final JLabel baseIn = new JLabel("Base Input");
        private final JLabel heightIn = new JLabel("Height Input");
        private final JTextField base = new JTextField();
        private final JTextField height = new JTextField();
        private final JTextField area = new JTextField();
        
        private final JButton calc = new JButton("Calculate Area");
        
        public GUI() {
            super();
            setSize(500, 300);
            setLayout(g);
            add(baseIn);
            add(heightIn);
            add(base);
            add(height);
            add(area);
            add(calc);
            area.setEditable(false);
            calc.addActionListener((ActionListener) this);
        
            setVisible(true);
        }
        
        /**
         *
         * @param event
         */
        
        @Override
        public void actionPerformed(final ActionEvent e) {
            try {
                double bInput = Integer.valueOf(base.getText());
                double hInput = Integer.valueOf(height.getText());
                double aOutput = 0.5 * bInput * hInput;
                area.setText("Area of your triangle is: " + aOutput);
            } catch (NumberFormatException n) {
                System.out.println(n.getMessage());
            }
        }
        
        }
        

        【讨论】:

        • 我尝试添加动作听 calc 像 calc.addActionListener(this);最初,但它不会让我发布。如果我使用实现 ActionListener,它会给我错误。我正在为一堂课这样​​做,但对我的同学却没有这样做,所以我想知道有什么问题。其他错误也是如此。你有什么解决办法吗?
        猜你喜欢
        • 2012-12-30
        • 2014-07-03
        • 1970-01-01
        • 2011-05-27
        • 2011-10-27
        • 1970-01-01
        • 1970-01-01
        • 2013-04-06
        • 2019-05-18
        相关资源
        最近更新 更多