1

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> &#8260 <sub>s<sup> 2 </sup></sub>): 9.81</html>");
        densityOfPVCConstantLabel.setText("<html>Density of PVC (<sup>lb</sup> &#8260 <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.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • [for example](http://stackoverflow.com/questions/7448216/how-to-rendering-fraction-in-swing-jcomponents) – mKorbel Apr 09 '15 at 17:23
  • @mKorbel Thank you for responding. I have read that post before. I guess I had hoped that in the last 3 years someone would have come up with a simpler solution. For my particular application, I am not sure if I want to spend that kind of effort for a text field label, when it really has no effect on the function of the program, its just a pretty thing. Again, thank you for taking the time to respond. – Michael Morgan Apr 09 '15 at 17:40
  • maybe is there, to try ask the same question on CodeRanch, there is a rest of Swing Gurus ...., don't forget mentioned, add link to this question (is about crossposting), plus one against the clean_up – mKorbel Apr 09 '15 at 21:24

0 Answers0