java - JScrollPane isn't visible -
the scroll pane isn't visible here. know problem setlayout(null)
. can write instead of this?
labelsend.addmouselistener(new mouseadapter(){ public void mouseclicked(mouseevent e3){ framelogin.setvisible(false); jframe framesend = new jframe(); framesend.setsize(500,500); jpanel panelsend = new jpanel(); panelsend.setlayout(null); jlabel labelsendname = new jlabel("name: "); labelsendname.setbounds(20,20,50,10); panelsend.add(labelsendname); jtextfield textsendname = new jtextfield(); textsendname.setbounds(60, 15, 400, 18); panelsend.add(textsendname); jlabel labelmessagesend = new jlabel("message: "); labelmessagesend.setbounds(1,50,80,14); panelsend.add(labelmessagesend); jtextarea textmessagesend = new jtextarea(5,10); textmessagesend.setbounds(60,50,400,300); jscrollpane scrollsend = new jscrollpane(textmessagesend); scrollsend.setverticalscrollbarpolicy(scrollpaneconstants.vertical_scrollbar_always); textmessagesend.setwrapstyleword(true); textmessagesend.setlinewrap(true); panelsend.add(scrollsend); framesend.add(panelsend); framesend.setvisible(true); framesend.add(panelsend); framesend.setvisible(true); } });
what can write instead of this?
layouts. one, i'd use gridbaglayout
constraints give text area remainder of available space, honor initial size of text field (i.e. don't expand it).
import java.awt.*; import javax.swing.*; import javax.swing.border.emptyborder; public class sendframe2 { private jcomponent ui = null; jlabel labelsendname = new jlabel("name: ", jlabel.trailing); jtextfield textsendname = new jtextfield(20); //suggest size in columns jlabel labelmessagesend = new jlabel("message: ", jlabel.trailing); jtextarea textmessagesend = new jtextarea(15, 45); sendframe2() { initui(); } public void initui() { if (ui != null) { return; } gridbagconstraints gbc = new gridbagconstraints( 0, 0, 1, 1, 0, 0, gridbagconstraints.baseline_trailing, gridbagconstraints.both, new insets(5,5,5,5), 4, 2); ui = new jpanel(new gridbaglayout()); ui.setborder(new emptyborder(10, 10, 10, 10)); ui.add(labelsendname, gbc); gbc.gridy = 1; ui.add(labelmessagesend, gbc); gbc.gridx = 1; gbc.gridy = 0; gbc.fill = gridbagconstraints.vertical; gbc.anchor = gridbagconstraints.baseline_leading; ui.add(textsendname, gbc); gbc.fill = gridbagconstraints.both; gbc.gridy = 1; gbc.weightx = 1; gbc.weighty = 1; jscrollpane jsp = new jscrollpane( textmessagesend, jscrollpane.vertical_scrollbar_always, jscrollpane.horizontal_scrollbar_never); ui.add(jsp, gbc); } public jcomponent getui() { return ui; } public static void main(string[] args) { runnable r = new runnable() { @override public void run() { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (exception usedefault) { } sendframe2 o = new sendframe2(); jframe f = new jframe(o.getclass().getsimplename()); f.setdefaultcloseoperation(jframe.dispose_on_close); f.setlocationbyplatform(true); f.setcontentpane(o.getui()); f.pack(); f.setminimumsize(f.getsize()); f.setvisible(true); } }; swingutilities.invokelater(r); } }
Comments
Post a Comment