在 JList 中写一些东西
嘿,我还有另一个问题。我在主窗口中创建了 JList,现在我想向其中添加一些内容。我这样做...
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)
{
Dodaj_Przedmiot dodaj_przedmiot = new Dodaj_Przedmiot(null, true);
dodaj_przedmiot.setVisible(true);
SterowanieBazy instance = SterowanieBazy.getInstance();
Zmienne_pomocnicze zp = new Zmienne_pomocnicze();
String przedmiot = zp.getPrzechowaj();
instance.dodajPrzedmiot(przedmiot);
String przedm[] = instance.zwrocPrzedmioty();
jList1.setListData(przedm);
}
我想在该列表中写的是我从 jDialogForm: dodaj_przedmiot 收集的内容
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
String sciezka = jTextField1.getText();
if (sciezka.length() > 0)
{
Zmienne_pomocnicze zp = new Zmienne_pomocnicze();
zp.setPrzechowaj(sciezka);
}
this.setVisible(false);
}
,我收集尝试使用此类复制该日期,
public class Zmienne_pomocnicze {
public String n;
public int a;
public void setPrzechowaj (String neew)
{
n = neew;
}
public String getPrzechowaj ()
{
return n;
}
}
我将不胜感激任何如何使其工作的想法。
hey i have another problem. I created JList
in my main window and now i want to add something to it. I do it this way...
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)
{
Dodaj_Przedmiot dodaj_przedmiot = new Dodaj_Przedmiot(null, true);
dodaj_przedmiot.setVisible(true);
SterowanieBazy instance = SterowanieBazy.getInstance();
Zmienne_pomocnicze zp = new Zmienne_pomocnicze();
String przedmiot = zp.getPrzechowaj();
instance.dodajPrzedmiot(przedmiot);
String przedm[] = instance.zwrocPrzedmioty();
jList1.setListData(przedm);
}
what i want to write in that list is what i collect from my jDialogForm: dodaj_przedmiot
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
String sciezka = jTextField1.getText();
if (sciezka.length() > 0)
{
Zmienne_pomocnicze zp = new Zmienne_pomocnicze();
zp.setPrzechowaj(sciezka);
}
this.setVisible(false);
}
and i collect try to copy that date using this class
public class Zmienne_pomocnicze {
public String n;
public int a;
public void setPrzechowaj (String neew)
{
n = neew;
}
public String getPrzechowaj ()
{
return n;
}
}
i would be grateful for any ideas how to make it work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这有点难以理解,但从我收集的信息来看,您在两个地方使用了
Zmienne_pomocnicze
类,而且它们似乎都没有执行任何操作。首先,在
jButton2ActionPerformed
中,您实例化一个新的Zmienne_pomocnicze
并尝试使用getPrzechowaj
方法从中获取数据。这将返回n
,但由于您刚刚实例化了该实例,n
为null
。由于我无法从以下代码的方法名称中推断出,我无法弄清楚您想要对这些数据做什么,但这个操作肯定不是您想要做的。在第二种情况下,
jButton1ActionPerformed
从文本字段获取值,然后测试有效性(legnth 大于 0)。如果验证通过,您将创建一个新的Zmienne_pomocnicze
,使用文本字段值调用setPrezechowaj
,然后让新对象超出范围。再次强调,这肯定不是我们想要的效果。了解程序的流程应该是什么,即哪个按钮触发哪个
jButton[12]ActionPerformed
方法以及您期望它们如何交互,这将是很有趣的。This is somewhat difficult to follow, but from what I gather, you are using your
Zmienne_pomocnicze
class in two places, and both of them seem to do nothing.First, in
jButton2ActionPerformed
you instantiate a newZmienne_pomocnicze
and try to get the data from it using thegetPrzechowaj
method. This will returnn
, but as you have just instantiated the instance,n
isnull
. As I cant infer from the method names of the following code, I cant figure out what you want to do with that data, but this action is most certainly not what you want to do.In the second case,
jButton1ActionPerformed
takes the value from the text field and then test for validity (legnth is greater than 0). If the validation passes, you then create a newZmienne_pomocnicze
, callsetPrezechowaj
with the text field value and then let the new object fall out of scope. Again, this is certainly not the desired effect.It would be interesting to see what the flow of your program is supposed to be, ie what button triggers which
jButton[12]ActionPerformed
methods and how you expect them to interact.下面是向
JList
添加条目的简单示例。Here's a simple example of adding entries to a
JList
.我始终建议阅读 API 以获取基本信息。
如果您阅读过 JList API,您将找到关于“如何使用列表”的 Swing 教程的链接。该示例展示了如何动态添加和删除 ListModel 中的条目。
教程是一个很好的起点,因为您可以找到工作示例以及有关代码如何工作的解释。然后,如果需要,您可以询问有关特定代码段的特定问题。
不仅如此,您现在有了一个可能对解决其他问题派上用场的参考。
I always recommend reading the API for basic information.
If you read the JList API you will find a link to the Swing tutorial on "How to Use Lists". The example there shows how to dynamically add and remove entries from the ListModel.
Tutorials are a good place to start because you find working examples as well as explanations as to how the code works. Then, if required you can ask a specific question about a specific piece of code.
Not only that you now have a reference that might come in handy for other problems.