This isn't an overly critical question and it will probably get marked as a duplicate since what I am about to ask has sort of been asked before( but not exactly using the same characters I am trying to do, and they were asked around 2011 - 2012, so maybe there are different answers now).
OK, So I have have a physics program that I am writing that requires numerical input, values of constants, etc.... I am using JLabels for all of my JTextField labels. Now I understand using html tagging for super and subscript when trying to create fractions. But most physics units are not numerical fractions (i.e. meters per second squared). Below is what I have done to try to create this, and it is moderately successful (does the job but pretty ugly).
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MathFormattingTest extends JFrame {
private static final long serialVersionUID = 1L;
JPanel panel;
JLabel gravitationalAccelerationConstantLabel;
JLabel densityOfPVCConstantLabel;
private void initComponents() {
panel = new JPanel();
gravitationalAccelerationConstantLabel = new JLabel();
densityOfPVCConstantLabel = new JLabel();
setDefaultCloseOperation(EXIT_ON_CLOSE);
BorderLayout layout = new BorderLayout(25, 25);
panel.setLayout(layout);
panel.setBackground(Color.WHITE);
panel.setOpaque(true);
panel.setBorder(BorderFactory.createEmptyBorder(25, 25, 25, 25));
gravitationalAccelerationConstantLabel.setText("<html>Gravitational Acceleration (<sup>m</sup> ⁄ <sub>s<sup> 2 </sup></sub>): 9.81</html>");
densityOfPVCConstantLabel.setText("<html>Density of PVC (<sup>lb</sup> ⁄ <sub>in</sub>): 0.04225</html>");
panel.add(gravitationalAccelerationConstantLabel, BorderLayout.NORTH);
panel.add(densityOfPVCConstantLabel, BorderLayout.SOUTH);
add(panel);
pack();
setLocationRelativeTo(null);
}
public MathFormattingTest() {
initComponents();
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new MathFormattingTest().setVisible(true);
}
});
}
}
If you compile and run this program you will see the output isn't the cleanest thing in the world. Now one thing that was suggested was to draw this as a paint component then convert the image to an icon and post the icon. That seems like a lot of effort for something that I would think is relatively simple. Maybe I am crazy but I would think that something like this would be relatively commonplace in java programming and there would be a "prettier" way of doing it. Any suggestions you guys can give are more than welcome, this place never lets me down and should be the go-to for anyone trying to learn programming.