在 JOptionPane.showOptionDialog 中使用 Arraylist 作为消息
Use an Arraylist as message in JOptionPane.showOptionDialog
我正在开发一个由其他人更改单词的程序,我正在尝试让用户在按下 "add" 时可以根据需要添加更多行来更改任意数量的单词。我会试着解释一下,程序应该是这样的:
如果我按添加:
如果我一直按 "add" 它应该会添加越来越多 "lines".
为此,我使用了这个:
JTextField oldword = new JTextField(5);
JTextField newword = new JTextField(5);
JPanel Panel = new JPanel();
List<JPanel> message = new ArrayList<JPanel>();
String[] options = new String[] {"Ok", "Cancel", "Add", "Delete"};
Panel.add(new JLabel("Change: "));
Panel.add(oldword);
Panel.add(Box.createHorizontalStrut(15));
Panel.add(new JLabel("by:"));
Panel.add(newword);
message.add(Panel);
while(response!=0 && response!=1){
response = JOptionPane.showOptionDialog(null, message, "Title",
JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
if(response==2){
message.add(Panel);
}
}
但是当它到达 "response = JOptionPane.showOptionDialog" 时,我只得到一个盒子:
如果我按添加,它会开始重复该消息。
对不起,如果我没有清楚地解释我的问题,我的语言很糟糕。
您正在使用 ArrayList<JPanel>
作为消息。由于message
参数不是String
,而是Object
,所以接受任何对象。
在您的情况下,该列表包含 JPanel
s,其中 toString()
不适合您的消息(它更适合调试)。
调用对象的 toString()
并显示其结果,这是完全合法的代码,但很可能不是您想要的。
也许您想使用 StringBuilder 构建要显示的字符串。
例如
StringBuilder builder = new StringBuilder();
builder.append("Change: ")
.append(oldword.getText())
.append("by:")
.append(newword.getText())
.toString();
编辑:
其实在message中显示一个JPanel
是可以的,当你使用Component
时,它会显示出来(但是当使用ArrayList
时,它就不会显示):
JPanel panel = new JPanel();
JTextField oldword = new JTextField(5);
JTextField newword = new JTextField(5);
String[] options = new String[] {"Ok", "Cancel", "Add", "Delete"};
panel.add(new JLabel("Change: "));
panel.add(oldword);
panel.add(new JLabel("by:"));
panel.add(newword);
JOptionPane.showOptionDialog(null, panel, "Title",
JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
如果要显示多个Panel,可以像Stack swing elements from top to bottom
一样把它们放到一个super-Panel中然后堆叠起来
我找到了一种方法,我确信这不是最好的方法,但是...
public String[] setwords(){
int response=-12;
String[] options = new String[] {"Ok", "Cancel", "Add", "Delete"};
JPanel[] panel;
JTextField[] oldword = null;
JTextField[] newword = null;
JTextField[] oldwordaux=null;
JTextField[] newwordaux=null;
int size=1;
Boolean firsttime = true;
Boolean noword = true;
String[] words;
while(response!=0 && response!=JOptionPane.CLOSED_OPTION && response!=1){
panel = new JPanel[size];
oldword = new JTextField[size];
newword = new JTextField[size];
for(int i=0; i<size; i++){
panel[i] = new JPanel();
oldword[i] = new JTextField(3);
newword[i] = new JTextField(3);
panel[i].add(new JLabel("Change: "));
panel[i].add(oldword[i]);
panel[i].add(Box.createHorizontalStrut(15));
panel[i].add(new JLabel("by: "));
panel[i].add(newword[i]);
if(firsttime!=true){
if(i<oldwordaux.length){
oldword[i].setText(oldwordaux[i].getText());
newword[i].setText(newwordaux[i].getText());
}
}
}
firsttime = false;
response = JOptionPane.showOptionDialog(null, panel, "Title", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
if(response==2){
size++;
oldwordaux=oldword;
newwordaux=newword;
}else if(response==3){
size=(size>1? size-1:size);
oldwordaux = Arrays.copyOfRange(oldword, 0, size);
newwordaux = Arrays.copyOfRange(newword, 0, size);
}
}
for(int j=0; j<oldword.length; j++){
if(oldword[j].getText().length()!=0){
noword=false;
}
}
if(noword){
return null;
}else{
int w=0;
words = new String[oldword.length*2];
for(int i=0; i<oldword.length; i++){
words[i]=oldword[i].getText();
w=i+1;
}
for(int i=0; i<newword.length; i++){
words[w]=newword[i].getText();
w++;
}
return words;
}
}
当按下 "add" 时,我创建了一个比上一个更大的新数组,并显示了新数组。
我正在开发一个由其他人更改单词的程序,我正在尝试让用户在按下 "add" 时可以根据需要添加更多行来更改任意数量的单词。我会试着解释一下,程序应该是这样的:
如果我按添加:
如果我一直按 "add" 它应该会添加越来越多 "lines".
为此,我使用了这个:
JTextField oldword = new JTextField(5);
JTextField newword = new JTextField(5);
JPanel Panel = new JPanel();
List<JPanel> message = new ArrayList<JPanel>();
String[] options = new String[] {"Ok", "Cancel", "Add", "Delete"};
Panel.add(new JLabel("Change: "));
Panel.add(oldword);
Panel.add(Box.createHorizontalStrut(15));
Panel.add(new JLabel("by:"));
Panel.add(newword);
message.add(Panel);
while(response!=0 && response!=1){
response = JOptionPane.showOptionDialog(null, message, "Title",
JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
if(response==2){
message.add(Panel);
}
}
但是当它到达 "response = JOptionPane.showOptionDialog" 时,我只得到一个盒子:
如果我按添加,它会开始重复该消息。
对不起,如果我没有清楚地解释我的问题,我的语言很糟糕。
您正在使用 ArrayList<JPanel>
作为消息。由于message
参数不是String
,而是Object
,所以接受任何对象。
在您的情况下,该列表包含 JPanel
s,其中 toString()
不适合您的消息(它更适合调试)。
调用对象的 toString()
并显示其结果,这是完全合法的代码,但很可能不是您想要的。
也许您想使用 StringBuilder 构建要显示的字符串。
例如
StringBuilder builder = new StringBuilder();
builder.append("Change: ")
.append(oldword.getText())
.append("by:")
.append(newword.getText())
.toString();
编辑:
其实在message中显示一个JPanel
是可以的,当你使用Component
时,它会显示出来(但是当使用ArrayList
时,它就不会显示):
JPanel panel = new JPanel();
JTextField oldword = new JTextField(5);
JTextField newword = new JTextField(5);
String[] options = new String[] {"Ok", "Cancel", "Add", "Delete"};
panel.add(new JLabel("Change: "));
panel.add(oldword);
panel.add(new JLabel("by:"));
panel.add(newword);
JOptionPane.showOptionDialog(null, panel, "Title",
JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
如果要显示多个Panel,可以像Stack swing elements from top to bottom
一样把它们放到一个super-Panel中然后堆叠起来我找到了一种方法,我确信这不是最好的方法,但是...
public String[] setwords(){
int response=-12;
String[] options = new String[] {"Ok", "Cancel", "Add", "Delete"};
JPanel[] panel;
JTextField[] oldword = null;
JTextField[] newword = null;
JTextField[] oldwordaux=null;
JTextField[] newwordaux=null;
int size=1;
Boolean firsttime = true;
Boolean noword = true;
String[] words;
while(response!=0 && response!=JOptionPane.CLOSED_OPTION && response!=1){
panel = new JPanel[size];
oldword = new JTextField[size];
newword = new JTextField[size];
for(int i=0; i<size; i++){
panel[i] = new JPanel();
oldword[i] = new JTextField(3);
newword[i] = new JTextField(3);
panel[i].add(new JLabel("Change: "));
panel[i].add(oldword[i]);
panel[i].add(Box.createHorizontalStrut(15));
panel[i].add(new JLabel("by: "));
panel[i].add(newword[i]);
if(firsttime!=true){
if(i<oldwordaux.length){
oldword[i].setText(oldwordaux[i].getText());
newword[i].setText(newwordaux[i].getText());
}
}
}
firsttime = false;
response = JOptionPane.showOptionDialog(null, panel, "Title", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
if(response==2){
size++;
oldwordaux=oldword;
newwordaux=newword;
}else if(response==3){
size=(size>1? size-1:size);
oldwordaux = Arrays.copyOfRange(oldword, 0, size);
newwordaux = Arrays.copyOfRange(newword, 0, size);
}
}
for(int j=0; j<oldword.length; j++){
if(oldword[j].getText().length()!=0){
noword=false;
}
}
if(noword){
return null;
}else{
int w=0;
words = new String[oldword.length*2];
for(int i=0; i<oldword.length; i++){
words[i]=oldword[i].getText();
w=i+1;
}
for(int i=0; i<newword.length; i++){
words[w]=newword[i].getText();
w++;
}
return words;
}
}
当按下 "add" 时,我创建了一个比上一个更大的新数组,并显示了新数组。