即使下面的class
编译成功,我也无法运行我的代码。 GUI
没有露面。简而言之,我只是希望用户将任意int
输入到javax.swing.JTextField
中,然后向用户显示它的“意大利语”表示。这是我的应用程序的完整代码。
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();
}
}
看看你的例子,我注意到了一些事情。
该示例的主要问题是没有入口点。如果没有入口点,您将无法运行代码。在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。根据它的文档,除非另有明确说明,否则您需要在事件调度线程中调用与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);
}
}