来自.NET我习惯在桌面应用程序中调用Alert().但是在这个java桌面应用程序中,我只想提醒一条消息"谢谢你使用java"我必须经历这么多苦难:
(使用JOptionPane)
有没有更简单的方法?
有没有办法创建多个输入JOptionPane.showInputDialog
而不是只有一个输入?
JOptionPane
可用于从用户获取字符串输入,但在我的情况下,我想在中显示密码字段showInputDialog
.
我需要的方式是应该屏蔽用户给出的输入并且返回值必须在char[]
.我需要一个带有消息,密码字段和两个按钮的对话框.可以这样做吗?谢谢.
我在JFrame上有一个按钮,当单击时我想要一个弹出的对话框,其中有多个文本区域供用户输入.我一直在四处寻找如何做到这一点,但我一直在变得更加困惑.有人可以帮忙吗?
如何打开警告/信息/错误对话框?
我需要带有"确定"按钮和"红叉"图像的标准错误对话框.即模拟org.eclipse.jface.dialogs.MessageDialog.openError()
我正在使用以下代码在我的swing应用程序中显示错误消息
try {
...
} catch (Exception exp) {
JOptionPane.showMessageDialog(this, exp.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
Run Code Online (Sandbox Code Playgroud)
错误对话框的宽度很长,具体取决于消息.有没有办法包装错误信息?
我在Java中实现了一个"另存为"对话框,提示用户文件是否已存在,并且我希望默认情况下选择"否"选项.我该怎么做呢?
这是我目前的代码:
JFileChooser chooser = new JFileChooser()
{
public void approveSelection()
{
File selectedFile = getSelectedFile();
if (selectedFile != null && selectedFile.exists( ) )
{
int response = JOptionPane.showConfirmDialog(
this,
"The file " + selectedFile.getName() + " already exists."
+ " Do you want to replace the existing file?",
getDialogTitle(),
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE);
if (response != JOptionPane.YES_OPTION )
{
return;
}
}
super.approveSelection();
}
};
Run Code Online (Sandbox Code Playgroud) 问题遵循横向规则; 我自己的答案先于它.
在Oscar Reyes的帮助下,我精心设计了这个解决方案:
import javax.swing.JOptionPane;
import javax.swing.JFrame;
public class MyApp extends JFrame {
public static void main(String [] args) {
new MyApp();
}
public MyApp() {
super("MyApp");
setUndecorated(true);
setVisible(true);
setLocationRelativeTo(null);
String i = JOptionPane.showInputDialog(this, "Enter your name:", getTitle(), JOptionPane.QUESTION_MESSAGE);
if(null != i) {
JOptionPane.showInputDialog(this, "Your name is:", getTitle(), JOptionPane.INFORMATION_MESSAGE, null, null, i.concat(i));
}
dispose();
}
}
Run Code Online (Sandbox Code Playgroud)
注意我也在JOptionPane.showInputDialog中显示输出.这样输出在文本字段中突出显示,因此我只需按CTRL+ C即可将输出复制到系统剪贴板,然后按下ESC以关闭应用程序.
我为我的琐碎应用程序创建了一个简单的GUI.我的应用程序提示单个输入a JOptionPane.showInputDialog
,执行计算,然后用a显示单个输出JOptionPane.showMessageDialog
.我有时会切换到最大化的浏览器窗口或其他要复制的内容,然后想切换回我的JOptionPane对话框进行粘贴.
我希望我的JOptionPane对话框显示为任务栏上的任务,因此我可以像几乎任何其他正在运行的程序一样切换到它.我更喜欢JOptionPane的简单性,而不是必须创建JFrame,FlowLayout,Icon,JTextField,JButton,ActionListener等等.
JOptionPane.show[whatever]dialog()
吗?我想在Java中创建一个输入表单,以便用户可以输入详细信息.
像这样的东西:
import java.awt.GridLayout;
import javax.swing.*;
class JOptionPaneTest {
public static void main(String[] args) {
String[] items = {"One", "Two", "Three", "Four", "Five"};
JComboBox combo = new JComboBox(items);
JTextField field1 = new JTextField("1234.56");
JTextField field2 = new JTextField("9876.54");
JPanel panel = new JPanel(new GridLayout(0, 1));
panel.add(combo);
panel.add(new JLabel("Field 1:"));
panel.add(field1);
panel.add(new JLabel("Field 2:"));
panel.add(field2);
int result = JOptionPane.showConfirmDialog(null, panel, "Test",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
System.out.println(combo.getSelectedItem()
+ " " + field1.getText()
+ " " + field2.getText());
} …
Run Code Online (Sandbox Code Playgroud) 如何将自定义文本添加到JOptionPane.showInputDialog的按钮?
我知道这个问题JOptionPane showInputDialog有自定义按钮,但它没有回答问题,它只是将它们引用到JavaDocs,它没有回答它.
代码:
__CODE__
Object[] options1 = {"Try This Number",
"Choose A Random Number",
"Quit"};
JOptionPane.showOptionDialog(null,
"Enter a number between 0 and 10000",
"Enter a Number",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE,
null,
options1,
null);
Run Code Online (Sandbox Code Playgroud)
我想为此添加一个文本字段.