java - Display fullname of JCheckBox item -
i had design jframe
components. in jcheckbox
component had given name full name not displayed when run application. want display given name.
code
public class finddemo extends jframe { label l1; jcheckbox matchcase,matchwholewords; textfield tf; jbutton find_next,cancel; public finddemo() { l1 = new label("find what: "); matchcase=new jcheckbox(); matchcase.settext("match case "); matchwholewords=new jcheckbox("match whole words "); tf = new textfield(30); find_next = new jbutton("find next"); cancel = new jbutton("cancel"); setlayout(null); int label_w = 80; int label_h = 20; int tf_w = 120; l1.setbounds(10,10, label_w, label_h); add(l1); tf.setbounds(10+label_w, 10, tf_w, 20); add(tf); matchcase.setbounds(10, 10+label_h+10, label_w, label_h); add(matchcase); matchwholewords.setbounds(10, 10+label_h+35, label_w, label_h); add(matchwholewords); find_next.setbounds(250, 10, 100, 20); add(find_next); cancel.setbounds(250, 40, 100, 20); add(cancel); int w = 400; int h = 200; setsize(w,h); point center = graphicsenvironment.getlocalgraphicsenvironment().getcenterpoint(); setlocation(center.x-w/2, center.y-h/2); setvisible(true); } public static void main(string args[]){ new finddemo(); } }
- use swing components instead of awt components (
label
→jlabel
;textfield
→jtextfield
, ...) - don't use null layout manager (
setlayout(null)
) ,setbounds()
. examine using layout managers. - instead of
setsize(w, h)
usepack()
method.
full name not displayed
because use setbounds()
, null layout. matchcase.setbounds(10, 10 + label_h + 10, label_w, label_h)
set not enough space jcheckbox
because of shows '...'.
for example panel gridbaglayout
:
import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import java.awt.insets; import javax.swing.jbutton; import javax.swing.jcheckbox; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jtextfield; public class testframe extends jframe { jlabel l1; jcheckbox matchcase, matchwholewords; jtextfield tf; jbutton find_next, cancel; public testframe() { l1 = new jlabel("find what: "); matchcase = new jcheckbox(); matchcase.settext("match case "); matchwholewords = new jcheckbox("match whole words "); tf = new jtextfield(30); find_next = new jbutton("find next"); cancel = new jbutton("cancel"); setlayout(new gridbaglayout()); gridbagconstraints c = new gridbagconstraints(); c.insets = new insets(5, 5, 5, 5); c.gridx = 0; c.gridy = 0; c.anchor = gridbagconstraints.west; add(l1, c); c.gridx++; c.fill = gridbagconstraints.horizontal; c.weightx = 1; add(tf, c); c.fill = gridbagconstraints.none; c.weightx = 0; c.gridx++; add(find_next, c); c.gridx = 0; c.gridy = 1; c.gridwidth = 2; add(matchcase, c); c.gridx++; c.gridx++; c.gridwidth = 1; c.fill = gridbagconstraints.horizontal; add(cancel, c); c.gridy++; c.gridx = 0; c.gridwidth = 2; add(matchwholewords, c); setlocationrelativeto(null); pack(); setvisible(true); } public static void main(string[] args) { new testframe(); } }
Comments
Post a Comment