swing - How can we add JScrollPane on JTextArea in java? -
can tell me problem in following program? want fit jscrollpane
on jtextarea
when add jtextarea
not visible.
import javax.swing.*; import java.awt.*; import java.awt.event.*; class area extends jframe { private jtextarea ta; private jtextfield tf; jscrollpane jp; public area() { super("text area"); tf=new jtextfield(); tf.setbounds(100,350,300,30); add(tf); ta=new jtextarea(); ta.setbounds(100,100,300,200); jp= new jscrollpane(ta); add(jp); setlayout(null); setsize(500,500); setvisible(true); setdefaultcloseoperation(jframe.exit_on_close); } public static void main(string...s) { new area(); } }
i see several problems:
don't use
null
layout; use real layout.the default layout of
jframe
borderlayout
; default positioncenter
; 1 component can occupy position @ time; example below usesnorth
¢er
.use appropriate constructor parameters size text components initially.
the scrollbar appear automatically whenever scrollpane smaller enclosed component; resize frame see effect.
as shown here, frame's size made smaller effect.
see initial threads.
import java.awt.borderlayout; import java.awt.eventqueue; import javax.swing.jframe; import javax.swing.jscrollpane; import javax.swing.jtextarea; import javax.swing.jtextfield; /** @see https://stackoverflow.com/a/19215436/230513 */ public class area extends jframe { public area() { super("text area"); setdefaultcloseoperation(jframe.exit_on_close); jtextfield tf = new jtextfield(12); add(tf, borderlayout.north); jtextarea ta = new jtextarea(24, 12); jscrollpane jp = new jscrollpane(ta); add(jp, borderlayout.center); pack(); // arbitrary size make vertical scrollbar appear setsize(240, 240); setlocationbyplatform(true); setvisible(true); } public static void main(string[] args) { eventqueue.invokelater(new runnable() { @override public void run() { new area(); } }); } }
Comments
Post a Comment