JOptionPane:showInputDialog
JOptionPane 类提供了多种类型的对话框,其中包括允许数据输入的输入对话框。JOptionPane 类的 showInputDialog 方法允许您输入字符串或从 JComboBox 列表中进行选择。此函数的返回调用是一个字符串。输入的字符数或实例数 Object如果您使用的是 JComboBox,否则在 数据条目无效。
java.swing.JOptionPane 类对对话框消息的显示由以下方法提供:
字符串响应 = showInputDialog(组件、消息、标题、类型);
- Component:是调用 Component 窗口或对象;
- message:要显示的消息;
- title:对话框的标题;
- type:是JOptionPane:
- ERROR_MESSAGE
- PLAIN_MESSAGE
- WARNING_MESSAGE
- QUESTION_MESSAGE
- INFORMATION_MESSAGE
- response的消息类型: 返回值.
下面是测试 showInputDialog():
import javax.swing.JFrame;Output
import javax.swing.JOptionPane;
public class Inputdialog {
public static void main(String[] args) {
JFrame frame = new JFrame(”);
String return = JOptionPane.showInputDialog(
frame, 输入你的名字”,Dialog”,
JOptionPane.PLAIN_MESSAGE);
System.exit(0);
}
}
JOptionPane with a JComboBox
另一种方法允许我们创建一个包含组合选项框的对话框 JComboBox with showInputDialog:
Object object object = showInputDialog(Component parent,
对象消息,
标题字符串,
int typeMessage,
图标图标, // 插入 ImageIcon
对象 [] 选择, // 以 Object
类型的数组形式选择值 InitialValue Object) // 将显示的初始对象
import javax.swing.JFrame;Execution:
import javax.swing.JOptionPane;
public class Inputdialog {
public static void main(String[] args) {
JFrame frame = new JFrame(”);
对象 [] 选择 = {1,2,3,4,5,6};
String return = (String) JOptionPane.showInputDialog(
frame, 选择索引”,
Dialog”,
JOptionPane.QUESTION_MESSAGE,
null, selection, selection[0]);
System.exit(0);
}
}
在此示例中,消息类型为 a QUESTION_MESSAGE,默认情况下,徽标显示为感叹号。
我们执行了强制转换 (String),因为该方法返回 Object.
在 showInputDialog
在前面的示例中,我们设置了 null,而不是创建显示的 ImageIcon,而不是默认图标。此代码将 null 替换为变量 icon:ImageIcon icon = new ImageIcon(question.png”);Execution:
对象[] 选择 = {1,2,3,4,5,6};
String back = (String) JOptionPane.showInputDialog(frame, 选择索引”,
Dialog”,JOptionPane.QUESTION_MESSAGE,
icon, selection, selection[0]);
upmf-grenoble: 摆动: JOp
Java doc: showInputDialog 方法