简体   繁体   中英

How can i add JLabel and JTextArea in the same space in JAVA

Here is the UI that I'd like to make.

I'm new to UI design. Could anyone tell me which layout should i use? How to make JLabel and JTextArea stick together and make String in the JTextArea clickable? Are there better ways to realize this design. I have tried BorderLayout, but it seems that I can not add JLabel and JTextArea together into same spot.

Thanks alot.

Typically, a complex UI would be made up a series of compound components and layouts, each focusing on a particular area of responsibility. This makes life much easier, as you focus on a small area without been worried about how it might affect unrelated elements.

Having said that, I think what you really want is a TitledBorder , for example...

边界

JTextArea ta = new JTextArea(20, 20);
JScrollPane sp = new JScrollPane(ta);
JPanel wrapper = new JPanel(new BorderLayout());
wrapper.setBorder(new TitledBorder("Details"));
wrapper.add(sp);

This will greatly simplify your layout, as you simply need to layout the wrapper panel and not the label AND associated component.

The drawback here is you do lose the keyboard shortcut functionality that JLabel can provide, but you can use the same idea (of a wrapper component) to wrap a JLabel and associated component into a easy to use package.

Depending on the route you take, I'd see GridBagLayout and possible BorderLayout and FlowLayout coming into play.

See Laying Out Components Within a Container and How to Use Borders for more details

String in the JTextArea clickable

That's rather complex at best. If you mean like a hyperlink, then I'd recommending doing some research into a JEditorPane , it will make it (somewhat) easier

Something like this for example

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