java - not able to display two JLabel at the same time -
because i'm spending days find solution, take risk ridiculous. i'm trying add , display 2 jlabel inside gridbagconstraints. seems second jlabel overwrite first one. thinking put jlabels inside container wasn't able that
this got :
and why want :
the following code runnable reproduce problem
package myproblem; import java.awt.basicstroke; import java.awt.borderlayout; import java.awt.color; import java.awt.dimension; import java.awt.eventqueue; import java.awt.font; import java.awt.graphics; import java.awt.graphics2d; import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import java.awt.geom.line2d; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.swingconstants; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; import javax.swing.border.border; import javax.swing.border.matteborder; public class myproblem { public int sizesquare = 50; public static void main(string[] args) { new myproblem(); } public myproblem() { eventqueue.invokelater(new runnable() { @override public void run() { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) { } jframe frame = new jframe("myproblem"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setlayout(new borderlayout()); frame.add(new testpb()); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } }); } public class testpb extends jpanel { public testpb() { setlayout(new gridbaglayout()); gridbagconstraints gbc = new gridbagconstraints(); border border = new matteborder(2, 2, 2, 2, color.black); (int row = 0; row < 3; row++) { (int col = 0; col < 3; col++) { gbc.gridx = col; gbc.gridy = row; fillsquare square = new fillsquare(sizesquare); square.setborder(border); square.setbackground(color.red); square.addlabel("45", "28"); add(square, gbc); } } } } public class fillsquare extends jpanel { public int sizesquare; public jlabel labelright; public jlabel labelleft; public jlabel label; public fillsquare(int sizesquare) { this.sizesquare = sizesquare; this.labelright = new jlabel("", swingconstants.right); this.labelleft = new jlabel("", swingconstants.left); this.label = new jlabel(); } @override public dimension getpreferredsize() { return new dimension(sizesquare, sizesquare); } public void addlabel(string number1, string number2) { add(addlabelleft(number1)); add(addlabelright(number2)); } public jlabel addlabelright(string number) { this.labelright.settext(number); labelright.setpreferredsize(new dimension(sizesquare * 80 / 100, sizesquare * 40 / 100)); labelright.setfont(new font("arial", 0, sizesquare * 40 / 100)); labelright.setalignmenty(swingconstants.top); return labelright; } public jlabel addlabelleft(string number) { this.labelleft.settext(number); labelleft.setpreferredsize(new dimension(25, 70)); labelleft.setfont(new font("arial", 0, sizesquare * 40 / 100)); labelleft.setalignmenty(swingconstants.bottom); return labelleft; } @override public void paintcomponent(graphics g) { super.paintcomponent(g); graphics2d g2d = (graphics2d) g; g2d.setstroke(new basicstroke(2)); line2d line = new line2d.double(0, 0, 100, 100); g2d.draw(line); } } }
any , advice appreciated
Comments
Post a Comment