简体   繁体   中英

Java graphics object and scrolling - easiest way to do it?

I'm working on a game that has a chatbox in it, but despite looking into it, (I might not be searching for the right thing), I can't figure out the best way to create a custom 'scroll pane' for a 'textbox' draw on the graphics. This horrible paint representation kind of shows what I'm trying to accomplish:

在此处输入图片说明

If I can give anymore information on this, please ask. This is the best way I could figure out how to explain it.

Edit: OK, so to give more info on this, the JPanel the game uses to display the game. It's done via overriding PaintComponent in the JPanel. I'm not sure if it's possible to add a new component to draw this with. If it is, then that might work. I just need to be able to position it in a specific spot. If adding a new component to this would work, then that would work and I feel like an idiot.

Option 1) Use a JTextArea held in a JScrollPane. Option 2) Use a JList held in a JScrollPane. The JTextArea is a little simpler, but the JList works better and is more responsive if it needs to hold a lot of data. The tutorials can be found here:

Note that this question could have been very quickly answered by you after a quick scan of the Swing Tutorials, and so for future reference, I've included a link to them.


Edit
Please consider these options for possibly a better solution:

  • Remember that your GUI can use more than one JPanel, and in fact can and often will use many .
  • Use your drawing JPanel for just that, drawing and nothing more.
  • In other words, don't have it hold any other components.
  • Place it BorderLayout.CENTER in another JPanel that uses, of course, a BorderLayout.
  • Place your other non-graphic control structures in JPanels that are held by the same BorderLayout-using JPanel described above. This way your control structures, such as your JScrollPane, is displayed in the GUI, but is not over-lying and covering the drawing JPanel.
  • Add your BorderLayout-using main JPanel into your top-level window, or if need be, in another JPanel if you need a more complex GUI.
  • You will want these sub JPanels to use layouts of their own, so that the whole GUI has a pleasing appearance that is easy to change and enhance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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