Java - 如何创建自定义对话框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/789517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java - How to create a custom dialog box?
提问by
I have a button on a JFrame that when clicked I want a dialog box to popup with multiple text areas for user input. I have been looking all around to try to figure out how to do this but I keep on getting more confused. Can anyone help?
我在 JFrame 上有一个按钮,单击该按钮时,我希望弹出一个包含多个文本区域供用户输入的对话框。我一直在四处寻找试图弄清楚如何做到这一点,但我越来越困惑。任何人都可以帮忙吗?
采纳答案by Sam Barnum
If you don't need much in the way of custom behavior, JOptionPane is a good time saver. It takes care of the placement and localization of OK / Cancel options, and is a quick-and-dirty way to show a custom dialog without needing to define your own classes. Most of the time the "message" parameter in JOptionPane is a String, but you can pass in a JComponent or array of JComponents as well.
如果您不需要太多的自定义行为,JOptionPane 是一个很好的节省时间的方法。它负责 OK / Cancel 选项的放置和本地化,并且是一种无需定义自己的类即可显示自定义对话框的快捷方式。大多数情况下,JOptionPane 中的“消息”参数是一个字符串,但您也可以传入 JComponent 或 JComponent 数组。
Example:
例子:
JTextField firstName = new JTextField();
JTextField lastName = new JTextField();
JPasswordField password = new JPasswordField();
final JComponent[] inputs = new JComponent[] {
new JLabel("First"),
firstName,
new JLabel("Last"),
lastName,
new JLabel("Password"),
password
};
int result = JOptionPane.showConfirmDialog(null, inputs, "My custom dialog", JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
System.out.println("You entered " +
firstName.getText() + ", " +
lastName.getText() + ", " +
password.getText());
} else {
System.out.println("User canceled / closed the dialog, result = " + result);
}
回答by bdl
This lessonfrom the Java tutorial explains each Swing component in detail, with examples and API links.
Java 教程中的这一课通过示例和 API 链接详细解释了每个 Swing 组件。
回答by Arnold Spence
If you use the NetBeans IDE(latest version at this time is 6.5.1), you can use it to create a basic GUI java application using File->New Project and choose the Java category then Java Desktop Application.
如果您使用NetBeans IDE(此时的最新版本是 6.5.1),您可以使用它创建一个基本的 GUI java 应用程序,使用 File->New Project 并选择 Java 类别然后选择 Java Desktop Application。
Once created, you will have a simple bare bones GUI app which contains an about box that can be opened using a menu selection. You should be able to adapt this to your needs and learn how to open a dialog from a button click.
创建后,您将拥有一个简单的基本 GUI 应用程序,其中包含一个可以使用菜单选择打开的关于框。您应该能够根据自己的需要进行调整,并学习如何通过单击按钮打开对话框。
You will be able to edit the dialog visually. Delete the items that are there and add some text areas. Play around with it and come back with more questions if you get stuck :)
您将能够直观地编辑对话框。删除那里的项目并添加一些文本区域。玩玩它,如果你卡住了,请回来提出更多问题:)
回答by Neil Coffey
Well, you essentially create a JDialog, add your text components and make it visible. It might help if you narrow down which specific bit you're having trouble with.
好吧,您实际上创建了一个 JDialog,添加了您的文本组件并使其可见。如果您缩小遇到问题的特定位的范围,这可能会有所帮助。
回答by BullyWiiPlaza
Try this simple class for customizing a dialog to your liking:
试试这个简单的类来自定义一个你喜欢的对话框:
import java.util.ArrayList;
import java.util.List;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRootPane;
public class CustomDialog
{
private List<JComponent> components;
private String title;
private int messageType;
private JRootPane rootPane;
private String[] options;
private int optionIndex;
public CustomDialog()
{
components = new ArrayList<>();
setTitle("Custom dialog");
setMessageType(JOptionPane.PLAIN_MESSAGE);
setRootPane(null);
setOptions(new String[] { "OK", "Cancel" });
setOptionSelection(0);
}
public void setTitle(String title)
{
this.title = title;
}
public void setMessageType(int messageType)
{
this.messageType = messageType;
}
public void addComponent(JComponent component)
{
components.add(component);
}
public void addMessageText(String messageText)
{
JLabel label = new JLabel("<html>" + messageText + "</html>");
components.add(label);
}
public void setRootPane(JRootPane rootPane)
{
this.rootPane = rootPane;
}
public void setOptions(String[] options)
{
this.options = options;
}
public void setOptionSelection(int optionIndex)
{
this.optionIndex = optionIndex;
}
public int show()
{
int optionType = JOptionPane.OK_CANCEL_OPTION;
Object optionSelection = null;
if(options.length != 0)
{
optionSelection = options[optionIndex];
}
int selection = JOptionPane.showOptionDialog(rootPane,
components.toArray(), title, optionType, messageType, null,
options, optionSelection);
return selection;
}
public static String getLineBreak()
{
return "<br>";
}
}
回答by MarkMyWord03
i created a custom dialog API. check it out here https://github.com/MarkMyWord03/CustomDialog. It supports message and confirmation box. input and option dialog just like in joptionpane will be implemented soon.
我创建了一个自定义对话框 API。在这里查看https://github.com/MarkMyWord03/CustomDialog。它支持消息和确认框。就像在 joptionpane 中一样的输入和选项对话框将很快实现。
Sample Error Dialog from CUstomDialog API: CustomDialog Error Message
来自 CUstomDialog API 的示例错误对话框: CustomDialog 错误消息