Let's say you want something that looks like this. (I found the Java examples here.) You want a border around the code example and you want a caption with a number that increments automatically.
You can go ahead and put the code in a frame. Just Insert > Frame, click OK, stretch it to the right size, paste the code inside. Then click in the frame and choose Insert > Caption, and bam, you're good.
Now, I'm embarrassed to say that I don't quite remember why frames aren't always a good solution. I was working with someone who wanted boxes around code and frames weren't quite working for him. (We needed to put frames inside frames because of some formatting goals.)
However, frames can be just a little annoying to deal with because sometimes when you click on the frame you can't type in it, you have to click outside the frame and then inside it again, etc.
And why use a graphic element when there's a paragraph attribute that lets you just put a border around any paragraph? Plus, then you can save all that paragraph formatting as a style, so you can type or paste in the code example text, select it all, and just apply your CodeExample style. It's more Elegant somehow. Plus it's easier to update. (You can do frame styles but the updating process isn't nearly as cooperative as the paragraph styles.) And the automatically numbering caption that you insert with this approach is very nearly as easy to do as when you skip styles.
1. Get your code example in. You can copy and paste from your actual code that you've got running, or choose Insert > File to just bring it straight in without copying and pasting.
2. Format it the way you want it. Courier font, font size, indented a half inch, whatever.
3. Then select it all and choose Format > Paragraph, Border tab. Select the border icon on the left for all four sides, in the middle set border formatting options, and on the right, maybe increase the distance from the content to the border. Click OK.
4. The code example will have a border.
5. With your mouse still selecting the code example, choose Format > Styles and Formatting. Click and hold down on the far right icon and choose New Style From Selection.
6. Give the new style a name and click OK.
You have your paragraph style for code examples now (in this document only) and can modify and reapply it easily.
7. Now to insert a caption for just some plain text, click below it and type whatever you want the caption to be, like Code Example: arrays or whatever.
8. Click where you want the code example number to appear, like between "example" and the colon.
9. Choose Insert > Fields > Other and click the Variables tab.
10. Select Number Range on the left. Now choose one of the numbering range systems set up already. Just select Drawing, Illustration, whatever you want. The word "Drawing" won't appear, they're just named differently to indicate that they're separate and increment independently. After you do, type whatever you chose, like Drawing or Illustration, followed by +1, at the bottom of the window in the Value field. Then select the numbering type on the right like Arabic, ABC, etc.
Then click Insert. The number appears. The gray indicates that the number automatically increments.
11. To make more captions, either copy and paste the results of the first one, and modify the text, or repeat those steps. The number will automatically increment. However, it won't be affected by other numbering like page or other number ranges you set up for tables or illustrations.
12. To apply the formatting to more code examples, just get the text in, select the whole piece of code, and choose Format > Styles and Formatting. Double-click the style you created.