JOptionPane.showMessageDialog 的可选替代方案 [英] Selectable alternative to JOptionPane.showMessageDialog

查看:35
本文介绍了JOptionPane.showMessageDialog 的可选替代方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景信息:

我在海军有一个朋友,他想知道我是否可以为他开发一个小应用程序,该应用程序可以在他执行警卫任务时进行计算,因为显然依靠日历很难.我使用 JOptionPane.showMessageDialog 给他输出日期.我就是这样做的.

I have a buddy of mine in the Navy, and he wanted to know if I could whip him up a small app that would calcualte when he has his guard duty, because apparently counting on a calendar is hard. I used JOptionPane.showMessageDialog to give him the output of the dates. Here's how I'm doing that.

GregorianCalendar knownDate = new GregorianCalendar(year,month,day);

GregorianCalendar[] futureDates = new GregorianCalendar[10];
for(int i = 0; i < 10; i++) {
    futureDates[i] = new GregorianCalendar(year,month,day);
    futureDates[i].add(Calendar.DAY_OF_MONTH,10*(i+1)); // duty every 10 days
}
String newline = System.getProperty("line.separator");
StringBuilder sb = new StringBuilder("Jakes duty dates:").append(newline);
for(GregorianCalendar d : futureDates) {
    sb.append(months[d.get(Calendar.MONTH)]).append(" ");
    sb.append(d.get(Calendar.DAY_OF_MONTH)).append(newline);
}
JOptionPane.showMessageDialog(null,sb.toString());

唯一的问题"是您无法选择显示的文本.他想为 IM 和电子邮件选择它,因为只有半懒惰有什么意义,对吧?(唯一的问题是在引号中,因为我觉得他会把这个范围蠕动到死......哈哈)

The 'only problem' is you can't select the text that is displayed. He'd like to select it for IM and email, because what's the point in only being half lazy, right? (Only problem is in quotes because I have a feeling he'll scope creep this to death... haha)

我的问题:

是否有单行解决方案"来制作可选择的 showMessageDialog?

Is there a "one-line solution" to making a selectable showMessageDialog?

推荐答案

我能够以垃圾神的回答为基础.虽然他建议使用 JList,但我使用的是 JTextArea(它提供了我需要的选择.)

I was able to build on trashgod's answer. While he suggested using a JList, I'm instead using a JTextArea (which gives the kind of selection I need.)

这是我正在做的事情:

JTextArea text = new JTextArea(sb.toString());
JOptionPane.showMessageDialog(null,text);

它的作用就像一个魅力!

And it's working like a charm!

=================================================

================================================

经过一些实验后,我做到了:

After a little experimentation I did this:

DefaultListModel model = new DefaultListModel();
for(GregorianCalendar g : futureDates) {
    String m = months[g.get(Calendar.MONTH)];
    String d = String.valueOf(g.get(Calendar.DAY_OF_MONTH));
    model.addElement(m + " " + d);
}
JList jlist = new JList(model);

JOptionPane.showMessageDialog(null,jlist);

JOptionPane.showMessageDialog(null,jlist.getSelectedValue());

第二个框显示了我在第一个框上选择的内容.我真的对此印象深刻.现在授予,这不是我想要的功能(顶部是),但这并没有让它变得不那么棒!:-)

And the second box displayed what I had selected on the first one. I was really impressed with that. Now granted, this isn't the functionality I was going for (the top section is) but that doesn't make it any less awesome! :-)

这篇关于JOptionPane.showMessageDialog 的可选替代方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆