i want make gui contains 3 textfields , 1 result displayed using jlabel align textfields vertically. however, wanted add border label border fixed size , have same size textfields. it's ok if label overflow border. looks : gui manually add spaces in order hold space, still couldn't make label align same width textfields, knows please fix problem.
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class testscorepanel extends jpanel{ private jpanel p5, label_panel,text_panel; private jbutton calculate_average; private jlabel length1_label, length2_label, length3_label,output_label,result_label; private jtextfield length1, length2, length3; public testscorepanel(){ length1_label = new jlabel("enter length #1"); length2_label = new jlabel("enter length #2"); length3_label = new jlabel("enter length #3"); output_label = new jlabel("average"); result_label = new jlabel(" ");//spaces used hold space length1 = new jtextfield(5); length2 = new jtextfield(5); length3 = new jtextfield(5); calculate_average = new jbutton("calculate average"); calculate_average.addactionlistener(new scorelistener()); p5 = new jpanel(); label_panel = new jpanel(); // left side labels text_panel = new jpanel(); // right side labels label_panel.setlayout(new boxlayout(label_panel, boxlayout.y_axis));//set vertical layout panel text_panel.setlayout(new boxlayout(text_panel, boxlayout.y_axis)); setborder(borderfactory.createemptyborder(10,10,10,10));//set result_label.setborder(borderfactory.createlineborder(color.black)); //setminimumsize donesn's seem work way wanted. result_label.setminimumsize(new dimension(400,20)); p5.setborder(borderfactory.createtitledborder("enter 3 lengths")); p5.add(label_panel); p5.add(box.createrigidarea(new dimension (50,0))); p5.add(text_panel); add(p5); label_panel.add(length1_label); text_panel.add(length1); label_panel.add(box.createrigidarea(new dimension (0, 10))); text_panel.add(box.createrigidarea(new dimension (0, 10))); label_panel.add(length2_label); text_panel.add(length2); label_panel.add(box.createrigidarea(new dimension (0, 10))); text_panel.add(box.createrigidarea(new dimension (0, 10))); label_panel.add(length3_label); text_panel.add(length3); label_panel.add(box.createrigidarea(new dimension (0, 10))); text_panel.add(box.createrigidarea(new dimension (0, 10))); label_panel.add(output_label); text_panel.add(result_label); setlayout(new boxlayout(this, boxlayout.y_axis)); add(box.createrigidarea(new dimension (0, 10))); add(calculate_average); setpreferredsize(new dimension(320,220)); } private class scorelistener implements actionlistener{ public void actionperformed(actionevent event){ int len1, len2, len3, average; len1 = integer.parseint(length1.gettext()); len2 = integer.parseint(length2.gettext()); len3 = integer.parseint(length3.gettext()); average= (len1+ len2+ len3)/3; if (event.getsource() == calculate_average) result_label.settext(integer.tostring(average)); } } }
consider using different layout manager gives more control on how components laid out, example, gridbaglayout
nb: padding around text fields courtesy of macos, in case, i'd consider using non-editable text field instead
setlayout(new gridbaglayout()); gridbagconstraints gbc = new gridbagconstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.anchor = gridbagconstraints.west; add(new jlabel("enter length #1"), gbc); gbc.gridy++; add(new jlabel("enter length #2"), gbc); gbc.gridy++; add(new jlabel("enter length #3"), gbc); gbc.gridy++; add(new jlabel("enter length #4"), gbc); gbc.gridy++; add(new jlabel("average"), gbc); gbc.gridx++; gbc.gridy = 0; gbc.fill = gridbagconstraints.horizontal; add(new jtextfield(10), gbc); gbc.gridy++; add(new jtextfield(10), gbc); gbc.gridy++; add(new jtextfield(10), gbc); gbc.gridy++; add(new jtextfield(10), gbc); gbc.gridy++; jlabel average = new jlabel(" "); average.setborder(borderfactory.createlineborder(color.black)); add(average, gbc);
Comments
Post a Comment