首页 > 解决方案 > 为什么 main 方法不打印任何东西?

问题描述

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

/*
 * 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");

    }

}

标签: javanetbeansmain

解决方案


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

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);

您需要执行强制转换才能使代码工作这一事实清楚地表明存在其他问题,这可能会导致运行时错误并使您的程序崩溃。也许您应该先阅读如何编写动作侦听器如何使用按钮、复选框和单选按钮,以更好地了解 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课程被标记为“主课程”,如果不是,请使用“浏览”找到它

项目属性


推荐阅读