在JFrame上有一个JList和一个JButton 。当我点击JButton会弹出一个自定义的JDialog。JDialog有一个JTextArea和JButton_2。目的是在JDialog上的JTextArea里面输入数据。点击JButton_2,然后将数据添加到JList上面。功能已经实现。但是有个问题。点击JButton_2之后。数据并没有添加到JList里面(没有显示出来)。而是在下一次点击JButton 的时候,数据才添加进去(显示在JList里面)。找了很久也找不到是啥子原因。请大家帮忙看一下。谢谢大家。代码如下:這個方法是把數據添加進JList的
public void addTheNeededRule(DefaultComboBoxModel boxModel, JList TopList,
String RuleSentence) {
if (RuleSentence != null && (RuleSentence.length()) > 0) { //判断是否为空
boxModel.addElement(RuleSentence);//添加数据进DefaultComboBoxModel
TopList.setModel(boxModel););//添加DefaultComboBoxModel进JList
}
else {
return;
}
}
這個方式是JFrame的JButton事件
addRuleButton.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
AddOrDelRule addOrDelRule = new AddOrDelRule();
AddRuleDialog dialog = new AddRuleDialog();
dialog.setVisible(true);
addOrDelRule.addTheNeededRule(boxModel, TopList,
UIFrame.RuleSentence); //RuleSentence是我在JFrame声明的一个static的全局变量
UIFrame.RuleSentence = "";
}
});這個是JDialog的JButton_2的事件
RuleButton_OK.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
UIFrame.RuleSentence = textRuleArea.getText().toString();
textRuleArea.setText("");
setVisible(false);
}
});
public void addTheNeededRule(DefaultComboBoxModel boxModel, JList TopList,
String RuleSentence) {
if (RuleSentence != null && (RuleSentence.length()) > 0) { //判断是否为空
boxModel.addElement(RuleSentence);//添加数据进DefaultComboBoxModel
TopList.setModel(boxModel););//添加DefaultComboBoxModel进JList
}
else {
return;
}
}
這個方式是JFrame的JButton事件
addRuleButton.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
AddOrDelRule addOrDelRule = new AddOrDelRule();
AddRuleDialog dialog = new AddRuleDialog();
dialog.setVisible(true);
addOrDelRule.addTheNeededRule(boxModel, TopList,
UIFrame.RuleSentence); //RuleSentence是我在JFrame声明的一个static的全局变量
UIFrame.RuleSentence = "";
}
});這個是JDialog的JButton_2的事件
RuleButton_OK.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
UIFrame.RuleSentence = textRuleArea.getText().toString();
textRuleArea.setText("");
setVisible(false);
}
});
楼主【lirenhong0108】截止到2008-06-25 10:39:20的历史汇总数据(不包括此帖):
发帖数:1 发帖分:0
结贴数:1 结贴分:0
未结数:0 未结分:0
结贴率:100.00% 结分率:-------------------
敬礼!
关注
一个JFrame,包含一个JList,JButton_1.
一个JDialog,包含一个JTextArea,JButton_2.
点击JButton_1,弹出JDialog,在JTextArea输入数据。点击JButton_2。数据显示在JList上。
功能已经实现。但效果有问题:点击JButton_1,弹出JDialog,在JTextArea输入“What's wrong with my application”.点击JButton_2.数据不显示。再次点击JButton_1.刚才输入的数据就显示出来了。
请大家帮忙解决一下。我已经头昏了。
因为你把数据加到jlist中去的时候, jlist还没有刷新,当你再点击button2的时候,就触发了jlist的刷新事件,所以会显示出来,但这个时候应该要显示两条,但实际上显示的似乎中人有条吧,所以你在往jlist加数据之后,就重绘下jlist
是在jdialog上的啊,
也就是repaint是在jbutton2的监听中不知道你是怎么写的.....