简体   繁体   English

如何将数据从 JFrame 输出到 JPanel

[英]How can I output my data to JPanel from my JFrame

I have a JFrame named MainGUI .我有一个名为MainGUIJFrame Inside of MainGUI I have passed three LinkedList ll1, ll2, ll3.MainGUI内部,我传递了三个 LinkedList ll1、ll2、ll3。

These LinkedList are full of data and I'm trying to just print one of them on the screen into my JPanel .这些 LinkedList 充满了数据,我试图将其中一个在屏幕上打印到我的JPanel I'm use to just doing a for loop and using System.out.println to print things out onto the screen.我习惯于只做一个 for 循环并使用System.out.println将内容打印到屏幕上。

So right now I have MainGUI which hosts three buttons.所以现在我有MainGUI ,它承载了三个按钮。

New Tasks In Progress Tasks Completed Tasks New Tasks In Progress Tasks Completed Tasks

Each button has a different LinkedList ll1, ll2, ll3 etc.每个按钮都有不同的 LinkedList ll1、ll2、ll3 等。
I want to be able to click the button and have the data elements listed below in the JPanel I created which rests under the buttons.我希望能够单击按钮并在我创建的JPanel列出下面列出的数据元素,该JPanel位于按钮下方。

Any help is deeply appreciated.任何帮助深表感谢。

Since you provided no code, I assume you are having trouble understanding how LinkedList can interact in programs that have a GUI.由于您没有提供任何代码,我假设您无法理解LinkedList如何在具有 GUI 的程序中进行交互。

First off, when using buttons you always need to instruct them to do something when they are clicked by adding an ActionListener , as explained in this answer .首先,当使用按钮时,您总是需要通过添加ActionListener来指示它们在被单击时执行某些操作,如本答案中所述
Secondly, if you want to add the list data to the JPanel , there are a few ways you can do it.其次,如果要将列表数据添加到JPanel ,有几种方法可以做到。 A JList , or if you'd like the user to be able to copy and paste the data (I find it to be very handy), a JTextArea , ... Just make sure to call setEditable(false) in order to stop the user from fiddling with the data you provide.一个JList ,或者如果您希望用户能够复制和粘贴数据(我发现它非常方便),一个JTextArea ,...只需确保调用setEditable(false)以停止用户从摆弄您提供的数据。 Considering a JTextArea , here's what that would look like, if ll1 contained Strings:考虑到JTextArea ,如果ll1包含字符串,则ll1

Adding somewhere that our JPanel contains a JTextArea :在我们的JPanel包含JTextArea地方添加:

JTextArea txtArea = new JTextArea();
txtArea.setEditable(false);
panel.add(txtArea);

Now, we order the button to do something when clicked:现在,我们命令按钮在点击时做一些事情:

btn.addActionListener(new ActionListener() { 
  public void actionPerformed(ActionEvent e) { 
    txtArea.setText(null); //clear out old text
    for(String str: ll1) {
      txtArea.append(str+"\n");
    }
    panel.revalidate(); //repaint JPanel
  } 
});

This way, you can click the button as many times as you want.这样,您可以根据需要多次单击该按钮。 Note that if you add more content to ll1 after it is displayed it won't get visually updated by itself, you will always need to click the button again or look further into Listeners.请注意,如果您在ll1显示后向其添加更多内容,它本身不会在视觉上更新,您将始终需要再次单击该按钮或进一步查看 Listeners。

You can try adding a JTextArea or whichever JComponent that suits what you want to display to the JPanel that you want to display the data from.您可以尝试将JTextArea或任何适合您想要显示JTextArea JComponent 添加到您想要从中显示数据的 JPanel。 Write the data from your linked list to that JComponent using its method eg append() if you're using JTextArea .如果您使用的是JTextArea则使用其方法append()数据从您的链接列表写入该 JComponent,例如append()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM