即使编译成功也无法执行我的应用程序

时间:2019-03-10 19:30:39

标签: java swing entry-point

即使下面的class编译成功,我也无法运行我的代码。 GUI只是没有出现。简而言之,我只希望用户将任意int输入到javax.swing.JTextField中,然后向用户显示其“ italian” 表示形式。这是我的应用程序的全部代码。

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

public class Example extends JFrame implements ActionListener {

    public ItalianStringNumberConversionFrame() {
        Container c = getContentPane();
        JButton button1 = new JButton("Convert");
        JTextField textField = new JTextField("Enter Integer: ");
        JPanel panel = new JPanel();
        JFrame frame = new JFrame();
        frame.setVisible(true);
        panel.setVisible(true);
        JLabel label1, label2;
        label1 = new JLabel("Enter an Integer to convert to Italian String:" );
        label2 = new JLabel("The text version of the number entered in Italian is: ");
        panel.add(label1,label2);
        panel.add(button1);
        panel.add(textField);
        c.add(panel);
    }

    public void actionPerformed(ActionEvent e) {
        new Example();
    }
}

1 个答案:

答案 0 :(得分:0)

以您的示例为例,我注意到了一些事情。

该示例的主要问题是没有入口点。没有入口点,您将无法运行代码。在 Java 中定义有效入口点的方法是在现有public static void main(java.lang.String[])上添加class方法。

public class Example
{
    public Example()
    {
        System.out.println("Hello world.");
    }

    public static void main(final String arguments)
    {
        new Example();
    }
}

如果您使用IntelliJ之类的集成开发环境,则只需单击public static void main(java.lang.String[])方法旁边的绿色箭头即可执行程序。

接下来,您正在使用 Swing 。根据它的文档,除非另有明确记录,否则您需要在 Event Dispatch Thread 中调用与 G- / UI 相关的代码。

  

通常,Swing不是线程安全的。除非另有说明,否则所有Swing组件和相关类都必须在事件分发线程上进行访问。

让我们将这些新发现的知识应用于您的应用程序!

public class ItalianStringNumberConversionFrame extends ...
{
    ...

    public static void main(final String[] arguments)
    {
        javax.swing.SwingUtilities.invokeLater(ItalianStringNumberConversionFrame::new);
    }
}

你去了。不过,我们还没有完成。您的 Swing 代码本身可能无法正常运行。

让我们从初始化功能javax.swing.JFrame开始。首先,永远不要直接扩展javax.swing.JFrame类。那只是非常糟糕的设计。而是尝试这样的事情。

import java.awt.*;

import javax.swing.*;

public class Example
{
    public Example()
    {
        // Allocate a new instance of the class in question into memory. 
        JFrame jFrame = new JFrame("Hello world.");
        // Terminate the underlying VM when the user is trying to close the window.
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        // Define a custom width and height for the window.
        jFrame.setSize(new java.awt.Dimension(384, 288));
        // In a nutshell: center the window relative to the primary monitor.
        jFrame.setLocationRelativeTo(null);
        // Let's use a FlowLayout manager for the content pane. If you'd like to know more about layout managers or Swing in general, take a look at the link below.
        jFrame.getContentPane().setLayout(new FlowLayout());
        // Let's instantiate a few components for our content pane.
        JButton convert = new JButton("Perform the conversion!");
        JTextField number = new JTextField("");
        // Ensure that the text field is big enough.
        number.setPreferredSize(new Dimension(jFrame.getWidth() / 2, 16));
        JLabel description = new JLabel("Enter an int to convert into an italian string literal: ");
        JLabel result = new JLabel("And the resulting string literal is: \"?\".");
        // Add each component in the correct order to the content pane.
        jFrame.getContentPane().add(description);
        jFrame.getContentPane().add(number);
        jFrame.getContentPane().add(convert);
        jFrame.getContentPane().add(result);
        /* 
         * I saw that you tried to add an action listener to your button.
         * Because of that, I wanted to add a little example of how you can interact with each component by adding such a listener.
         */
        convert.addActionListener(lambda -> {
               System.out.println("Performing the conversion!");
               /*
                * I don't quite know what you mean by converting a number into an "italian" string literal, but here's my interpretation of that:
                * Here's how you can get the text of the text field. Note: if the text is not a number, an exception is thrown!
                */
               int not_an_italian_number_yet = Integer.valueOf(number.getText());
               // Let's change the text of the "result" component.
               result.setText("And the resulting string literal is: \"" + (not_an_italian_number_yet + " *insert italian accent here*") + "\".");
               // That's already it!
        });
        // Finally, show the window to the user!
        jFrame.setVisible(true);
    }

    public static void main(final String[] arguments)
    {
        SwingUtilities.invokeLater(Example::new);
    }
}
相关问题