java swing自定义对话框_swing-Java-如何创建自定义对话框?

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。 在此处检查[https://github.com/MarkMyWord03/CustomDialog。]它支持消息和确认框。 就像joptionpane中一样,input和option对话框将很快实现。

CUstomDialog API的示例错误对话框:CustomDialog错误消息

MarkMyWord03 answered 2020-08-06T16:48:34Z

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值