1

In my app, the layout of panel is null because of I need to insert image and text in position where i choose. I hope the width and height of text can auto change with the length of words. If it does't work,another way will be OK. Click the panel to create a text box, drag it to the size and position as we want, and then type our text. How to make it?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
sharkliu
  • 11
  • 3
  • 2
    Please put the code what u have tried so far – The Hungry Dictator Mar 26 '14 at 07:02
  • 2
    Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). BTW - like we need another D-n-D GUI designer, especially one that does not use layouts. – Andrew Thompson Mar 26 '14 at 09:26
  • @AndrewThompson I agree that in most cases proper layout managers must be used but there are applications e.g. default Windows Paint where you can add textboxes to any desired position. Looks like that's the case. – StanislavL Mar 26 '14 at 09:33
  • Maybe look at `JDesktopPane` and `JInternalFrame`? – trashgod Mar 26 '14 at 13:45

1 Answers1

0

Click the panel to create a text box,

Add a MouseListener to the panel. Create a JTextField with a default size by using something like:

JTextField textField = new JTextField(2);
textField.setSize( textField.getPreferredSize() );
textField.setLocation( mouseEvent.getPoint() );
panel.add( textField );
panel.repaint();

drag it to the size and position as we want,

Add the Component Mover and Component Resizer classes to the text field.

camickr
  • 321,443
  • 19
  • 166
  • 288