导入javax.swing.JOptionPane
目前我正在阅读一本书,本章将简要介绍GUI导入javax.swing.JOptionPane,java,swing,import,joptionpane,Java,Swing,Import,Joptionpane,目前我正在阅读一本书,本章将简要介绍GUI import javax.swing.*; import java.awt.event.*; import java.awt.*; /** The MetricConverterWindow class lets the user enter a distance in kilometers. Radio buttons can be selected to convert the kilometers to miles, feet, or inch
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
/**
The MetricConverterWindow class lets the user enter a
distance in kilometers. Radio buttons can be selected to
convert the kilometers to miles, feet, or inches.
*/
public class MetricConverterWindow extends JFrame
{
private JPanel panel; // A holding panel
private JLabel messageLabel; // A message to the user
private JTextField kiloTextField; // To hold user input
private JRadioButton milesButton; // To convert to miles
private JRadioButton feetButton; // To convert to feet
private JRadioButton inchesButton; // To convert to inches
private ButtonGroup radioButtonGroup; // To group radio buttons
private final int WINDOW_WIDTH = 400; // Window width
private final int WINDOW_HEIGHT = 100; // Window height
/**
Constructor
*/
public MetricConverterWindow()
{
// Set the title.
setTitle("Metric Converter");
// Set the size of the window.
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
// Specify an action for the close button.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Build the panel and add it to the frame.
buildPanel();
// Add the panel to the frame's content pane.
add(panel);
// Display the window.
setVisible(true);
}
/**
The buildPanel method adds a label, text field, and
and three buttons to a panel.
*/
private void buildPanel()
{
// Create the label, text field, and radio buttons.
messageLabel = new JLabel("Enter a distance in kilometers");
kiloTextField = new JTextField(10);
milesButton = new JRadioButton("Convert to miles");
feetButton = new JRadioButton("Convert to feet");
inchesButton = new JRadioButton("Convert to inches");
// Group the radio buttons.
radioButtonGroup = new ButtonGroup();
radioButtonGroup.add(milesButton);
radioButtonGroup.add(feetButton);
radioButtonGroup.add(inchesButton);
// Add action listeners to the radio buttons.
milesButton.addActionListener(new RadioButtonListener());
feetButton.addActionListener(new RadioButtonListener());
inchesButton.addActionListener(new RadioButtonListener());
// Create a panel and add the components to it.
panel = new JPanel();
panel.add(messageLabel);
panel.add(kiloTextField);
panel.add(milesButton);
panel.add(feetButton);
panel.add(inchesButton);
}
/**
Private inner class that handles the event when
the user clicks one of the radio buttons.
*/
private class RadioButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String input; // To hold the user's input
String convertTo = ""; // The units we're converting to
double result = 0.0; // To hold the conversion
// Get the kilometers entered.
input = kiloTextField.getText();
// Determine which radio button was clicked.
if (e.getSource() == milesButton)
{
// Convert to miles.
convertTo = " miles.";
result = Double.parseDouble(input) * 0.6214;
}
else if (e.getSource() == feetButton)
{
// Convert to feet.
convertTo = " feet.";
result = Double.parseDouble(input) * 3281.0;
}
else if (e.getSource() == inchesButton)
{
// Convert to inches.
convertTo = " inches.";
result = Double.parseDouble(input) * 39370.0;
}
// Display the conversion.
JOptionPane.showMessageDialog(null, input +
" kilometers is " + result + convertTo);
}
}
/**
The main method creates an instance of the
MetricConverterWindow class, displaying its window.
*/
public static void main(String[] args)
{
new MetricConverterWindow();
}
}
这是本书中的一个直接例子。我最初在一个名为window的包中包含了它,还有一些其他与GUI相关的类。我决定将它移动到默认值,只是为了测试一些东西。到目前为止,我只是导入javax.swing.*而不是具体地导入javax.swing.JOptionPane,仅仅是因为这本书到目前为止是如何完成GUI示例的。当我从窗口包中删除它并将其移动到默认包时,会出现错误“类型JOptionPane的方法showMessageDialog(null,String)未定义”。如果我把它添加回我的GUI示例包中,我就没有问题了。我试着用我能想到的每一种方式来表达它,但我就是找不到一个解决方案,来解释为什么一旦从包中删除它,我就会得到那个错误
我意识到我没有完全理解我想问的问题。我想弄清楚的是,我是否需要同时导入这两个文件?它在包中正常工作是没有意义的,但在默认情况下它会给出一个错误。我想知道您是否创建了另一个
JOptionPane
类,就像练习或其他什么(在默认包中)一样。这将导致错误。在我看来,最好是单独导入所有类,尽管我在运行此代码时不会遇到任何问题。@peeskillet所指出的可能是正确的。我在试图在网上找到答案时确实读到,使用通配符是一个坏习惯。也有可能我只是有一本书提供了一些糟糕的建议。我没有问题单独导入它,现在我只是好奇是什么导致它在我制作的包中工作,而不是不工作。我检查了该包中的所有类,没有一个专门导入了JOptionPane@peeskilletBut您是否自己创建了名为JOptionPane
的类?这是我的观点啊,这正是我的观点,@peeskillet,我现在明白你的意思了。非常感谢。我上了一堂名为JOptionPane的实践课,多亏了我所学的知识,现在完全可以理解为什么它会引发问题。