Last active
October 23, 2016 17:16
-
-
Save bracco23/c47975ede0d857ac3b134f197c4371a2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.Dimension; | |
import java.awt.Font; | |
import java.awt.FontMetrics; | |
import java.awt.Insets; | |
import java.util.regex.Pattern; | |
import javax.swing.Icon; | |
import javax.swing.JLabel; | |
/** | |
* | |
* @author bracco23 | |
*/ | |
public class JAdaptiveLabel extends JLabel{ | |
boolean inizialized = false; | |
public JAdaptiveLabel(String text, Icon icon, int horizontalAlignment) { | |
super(text, icon, horizontalAlignment); | |
inizialized = true; | |
} | |
public JAdaptiveLabel(String text, int horizontalAlignment) { | |
super(text, horizontalAlignment); | |
inizialized = true; | |
} | |
public JAdaptiveLabel(String text) { | |
super(text); | |
inizialized = true; | |
} | |
public JAdaptiveLabel(Icon image, int horizontalAlignment) { | |
super(image, horizontalAlignment); | |
inizialized = true; | |
} | |
public JAdaptiveLabel(Icon image) { | |
super(image); | |
inizialized = true; | |
} | |
public JAdaptiveLabel() { | |
super(); | |
inizialized = true; | |
} | |
@Override | |
public void setText(String text) { | |
super.setText(text); | |
if (inizialized) { | |
adaptFontSize(); | |
} | |
} | |
public void adaptFontSize() { | |
float xFactor, yFactor; | |
Font fontNome = this.getFont(); | |
Insets ins = this.getInsets(); | |
FontMetrics metricsNome = this.getFontMetrics(fontNome); | |
Dimension lblNomeSize = this.getSize(); | |
xFactor = ((lblNomeSize.width - ins.left - ins.right) * 0.9f ) / metricsNome.stringWidth(this.getText()); | |
yFactor = ((lblNomeSize.height - ins.top - ins.bottom) * 0.9f ) / metricsNome.getHeight(); | |
xFactor = (xFactor != 0 && Math.abs(xFactor - 1)>0.1) ? xFactor : 1; | |
yFactor = (yFactor != 0 && Math.abs(xFactor - 1)>0.1) ? yFactor : 1;/**/ | |
float size = Math.min(xFactor, yFactor) * fontNome.getSize(); | |
Font newFont = fontNome.deriveFont(size); | |
if(Pattern.matches("<html>.*</html>", getText())){ | |
setText(getText().replaceFirst("size=\"[0-9]*\"", "size=\""+size+'"')); | |
} | |
this.setFont(newFont); | |
invalidate(); | |
} | |
@Override | |
public int hashCode() { | |
int hash = 5; | |
hash = 29 * hash + (this.inizialized ? 1 : 0); | |
return hash; | |
} | |
@Override | |
public boolean equals(Object obj) { | |
if (this == obj) { | |
return true; | |
} | |
if (obj == null) { | |
return false; | |
} | |
if (getClass() != obj.getClass()) { | |
return false; | |
} | |
final JAdaptiveLabel other = (JAdaptiveLabel) obj; | |
if (this.inizialized != other.inizialized) { | |
return false; | |
} | |
return true; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import javax.swing.JButton; | |
import javax.swing.JFileChooser; | |
import javax.swing.JFrame; | |
import javax.swing.JLabel; | |
import javax.swing.JPanel; | |
import net.miginfocom.layout.LayoutUtil; | |
import net.miginfocom.swing.MigLayout; | |
/** | |
* | |
* @author Emiddio | |
*/ | |
public class Prove { | |
static JPanel pane = null; | |
static int i = 0, j = 0; | |
static boolean CUSTOM = false; | |
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String[] args) throws Exception { | |
JFrame f = new JFrame(); | |
LayoutUtil.setGlobalDebugMillis(1000); | |
f.setLayout(new MigLayout("fill", "[100%]", "[10%|90%]")); | |
JButton b = new JButton("Add"); | |
b.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
if (pane != null) { | |
f.remove(pane); | |
} | |
pane = new JPanel(); | |
pane.setLayout(new MigLayout("fill")); | |
JPanel pane2 = new JPanel(); | |
pane2.setLayout(new MigLayout("fill", "[33%|33%|33%]", "[10%|90%]")); | |
JLabel l1 = getLabel("a", CUSTOM); | |
pane2.add(l1, "cell 0 0, grow"); | |
JLabel l2 = getLabel("b", CUSTOM); | |
pane2.add(l2, "cell 1 0, grow"); | |
JLabel l3 = getLabel("c", CUSTOM); | |
pane2.add(l3, "cell 2 0, grow"); | |
JPanel pane3 = new JPanel(); | |
pane3.setLayout(new MigLayout("fill")); | |
for (int itemp = i; itemp < i + 5; itemp++) { | |
for (int jtemp = j; jtemp < j + 6; jtemp++) { | |
if (itemp != i || jtemp != j) { | |
pane3.add(getLabel(Integer.toString(itemp + jtemp), CUSTOM), "grow, cell " + itemp + " " + jtemp); | |
} | |
} | |
} | |
i++; | |
j++; | |
pane2.add(pane3, "grow, cell 0 1 3 1"); | |
pane.add(pane2, "grow, cell 0 0"); | |
f.add(pane, "cell 0 1, grow"); | |
f.validate(); | |
} | |
}); | |
f.add(b, "cell 0 0, grow"); | |
f.setSize(300, 400); | |
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
f.setVisible(true); | |
} | |
public static JLabel getLabel(String s, boolean custom) { | |
JLabel l; | |
if(custom){ | |
l = new JAdaptiveLabel(s); | |
}else{ | |
l = new JLabel(s); | |
} | |
l.setHorizontalAlignment(JLabel.CENTER); | |
return l; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment