如何通过SwingJava实现自定义对话框的创建与长尾词设计?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1351个文字,预计阅读时间需要6分钟。
在Swing中,创建自定义对话框框可以通过以下步骤实现:
1. 创建一个`JDialog`对象,这将作为对话框的容器。
2.在对话框中添加一个`JTextField`用于用户输入文本。
3.将对话框设置为模态对话框,这样用户必须先关闭对话框才能继续操作。
4.在JFrame中添加一个按钮,并为该按钮添加一个事件监听器,当按钮被点击时,显示对话框。
以下是一个简单的示例代码:
java
import javax.swing.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class CustomDialogExample { public static void main(String[] args) { // 创建 JFrame 实例 JFrame frame=new JFrame(Swing 对话框示例); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 200);
// 创建按钮 JButton button=new JButton(打开对话框);
// 添加按钮到 JFrame frame.getContentPane().add(button);
// 创建对话框 JDialog dialog=new JDialog(frame, 自定义对话框, true); dialog.setSize(200, 100); dialog.setLocationRelativeTo(frame); // 居中显示
// 创建文本框 JTextField textField=new JTextField(20);
// 将文本框添加到对话框 dialog.getContentPane().add(textField);
// 创建按钮,用于关闭对话框 JButton dialogButton=new JButton(确定); dialogButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { dialog.dispose(); // 关闭对话框 } });
// 将按钮添加到对话框 dialog.getContentPane().add(dialogButton);
// 为 JFrame 添加按钮点击事件监听器 button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { dialog.setVisible(true); // 显示对话框 } });
// 显示 JFrame frame.setVisible(true); }}
这段代码创建了一个包含一个按钮的JFrame,当按钮被点击时,会弹出一个自定义的对话框,其中包含一个文本框和一个确定按钮。用户可以在文本框中输入文本,点击确定按钮后,对话框会关闭。
swing-Java-如何创建自定义对话框我在JFrame上有一个按钮单击该按钮后我希望对话框弹出并带有多个文本区域供用户输入。我一swing-Java-如何创建自定义对话框
我在JFrame上有一个按钮单击该按钮后我希望对话框弹出并带有多个文本区域供用户输入。 我一直在四处寻找解决方法但我一直感到困惑。 有人可以帮忙吗
6个解决方案
80 votes
如果您不需要太多自定义行为则JOptionPane可以节省大量时间。 它负责确定/取消选项的放置和本地化并且是一种无需定义自己的类即可显示自定义对话框的快捷方法。 大多数情况下JOptionPane中的“ message”参数是一个字符串但您也可以传入JComponent或JComponents数组。
例
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);
}
Sam Barnum answered 2020-08-06T16:46:36Z
3 votes
尝试使用以下简单类自定义您喜欢的对话框
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 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("" messageText "");
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 "";
}
}
BullyWiiPlaza answered 2020-08-06T16:46:56Z
1 votes
Java教程中的这一课详细说明了每个Swing组件并提供了示例和API链接。
bdl answered 2020-08-06T16:47:17Z
1 votes
如果使用NetBeans IDE(当前最新版本为6.5.1)则可以使用File-> New Project创建一个基本的GUI Java应用程序然后选择Java类别然后选择Java Desktop Application。
创建后您将拥有一个简单的裸露GUI应用程序其中包含一个About框可以使用菜单选择来打开该框。 您应该能够使其适应您的需求并了解如何通过单击按钮来打开对话框。
您将能够直观地编辑对话框。 删除其中的项目并添加一些文本区域。 试一试如果遇到问题还会遇到更多问题:)
Arnold Spence answered 2020-08-06T16:47:47Z
1 votes
好吧您实质上就是创建一个JDialog添加您的文本组件并使它可见。 如果缩小所遇到的特定位可能会有所帮助。
Neil Coffey answered 2020-08-06T16:48:07Z
1 votes
我创建了一个自定义对话框API。 在此处检查[github.com/MarkMyWord03/CustomDialog。]它支持消息和确认框。 就像joptionpane中一样input和option对话框将很快实现。
CUstomDialog API的示例错误对话框CustomDialog错误消息
MarkMyWord03 answered 2020-08-06T16:48:34Z
本文共计1351个文字,预计阅读时间需要6分钟。
在Swing中,创建自定义对话框框可以通过以下步骤实现:
1. 创建一个`JDialog`对象,这将作为对话框的容器。
2.在对话框中添加一个`JTextField`用于用户输入文本。
3.将对话框设置为模态对话框,这样用户必须先关闭对话框才能继续操作。
4.在JFrame中添加一个按钮,并为该按钮添加一个事件监听器,当按钮被点击时,显示对话框。
以下是一个简单的示例代码:
java
import javax.swing.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class CustomDialogExample { public static void main(String[] args) { // 创建 JFrame 实例 JFrame frame=new JFrame(Swing 对话框示例); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 200);
// 创建按钮 JButton button=new JButton(打开对话框);
// 添加按钮到 JFrame frame.getContentPane().add(button);
// 创建对话框 JDialog dialog=new JDialog(frame, 自定义对话框, true); dialog.setSize(200, 100); dialog.setLocationRelativeTo(frame); // 居中显示
// 创建文本框 JTextField textField=new JTextField(20);
// 将文本框添加到对话框 dialog.getContentPane().add(textField);
// 创建按钮,用于关闭对话框 JButton dialogButton=new JButton(确定); dialogButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { dialog.dispose(); // 关闭对话框 } });
// 将按钮添加到对话框 dialog.getContentPane().add(dialogButton);
// 为 JFrame 添加按钮点击事件监听器 button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { dialog.setVisible(true); // 显示对话框 } });
// 显示 JFrame frame.setVisible(true); }}
这段代码创建了一个包含一个按钮的JFrame,当按钮被点击时,会弹出一个自定义的对话框,其中包含一个文本框和一个确定按钮。用户可以在文本框中输入文本,点击确定按钮后,对话框会关闭。
swing-Java-如何创建自定义对话框我在JFrame上有一个按钮单击该按钮后我希望对话框弹出并带有多个文本区域供用户输入。我一swing-Java-如何创建自定义对话框
我在JFrame上有一个按钮单击该按钮后我希望对话框弹出并带有多个文本区域供用户输入。 我一直在四处寻找解决方法但我一直感到困惑。 有人可以帮忙吗
6个解决方案
80 votes
如果您不需要太多自定义行为则JOptionPane可以节省大量时间。 它负责确定/取消选项的放置和本地化并且是一种无需定义自己的类即可显示自定义对话框的快捷方法。 大多数情况下JOptionPane中的“ message”参数是一个字符串但您也可以传入JComponent或JComponents数组。
例
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);
}
Sam Barnum answered 2020-08-06T16:46:36Z
3 votes
尝试使用以下简单类自定义您喜欢的对话框
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 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("" messageText "");
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 "";
}
}
BullyWiiPlaza answered 2020-08-06T16:46:56Z
1 votes
Java教程中的这一课详细说明了每个Swing组件并提供了示例和API链接。
bdl answered 2020-08-06T16:47:17Z
1 votes
如果使用NetBeans IDE(当前最新版本为6.5.1)则可以使用File-> New Project创建一个基本的GUI Java应用程序然后选择Java类别然后选择Java Desktop Application。
创建后您将拥有一个简单的裸露GUI应用程序其中包含一个About框可以使用菜单选择来打开该框。 您应该能够使其适应您的需求并了解如何通过单击按钮来打开对话框。
您将能够直观地编辑对话框。 删除其中的项目并添加一些文本区域。 试一试如果遇到问题还会遇到更多问题:)
Arnold Spence answered 2020-08-06T16:47:47Z
1 votes
好吧您实质上就是创建一个JDialog添加您的文本组件并使它可见。 如果缩小所遇到的特定位可能会有所帮助。
Neil Coffey answered 2020-08-06T16:48:07Z
1 votes
我创建了一个自定义对话框API。 在此处检查[github.com/MarkMyWord03/CustomDialog。]它支持消息和确认框。 就像joptionpane中一样input和option对话框将很快实现。
CUstomDialog API的示例错误对话框CustomDialog错误消息
MarkMyWord03 answered 2020-08-06T16:48:34Z

