Whitespaces on JLabel Arrays causes project to become unaligned

40 views Asked by At

Well. I started coding this big project using JLabel arrays to map out large arithmetic equations. I thought this would be the way to solve the problem of aligning all of the work that is shown by the calculator but as I finished up the first Division Problem class and replaced the 0's with " "'s I've come to realize that the am encountering the same alignment problems with whitespaces as before. I guess with the other classes the alignment was a little less off. Is there a simple way I can fix the alignment for this project to make everything line up to show the work to each problem? Thank you!

P.S. Sorry my code is so messy. I am running on caffeine and I plan on modulating everything when I figure out the algorithym. :P

GUI.CLASS:

public class GUI extends JFrame implements ActionListener{

private JTextField field1;
private JTextField field2;
private JButton add, subtract, multiply, divide;
private JLabel lanswer, label1, label2;
private String input1, input2, sanswer;
private int answer = 0;

JPanel contentPanel; 
Console answerPanel;

public GUI(){
    super("Calculator");

    field1 = new JTextField(null, 15);
    field2 = new JTextField(null, 15);

    add = new JButton("add");
    subtract = new JButton("subtract");
    multiply = new JButton("multiply");
    divide = new JButton("divide");

    label1 = new JLabel("Value 1:");
    label2 = new JLabel("Value 2:");

    add.addActionListener(this);
    subtract.addActionListener(this);
    multiply.addActionListener(this);
    divide.addActionListener(this);

    Dimension opSize = new Dimension(110, 20);
    Dimension inSize = new Dimension(200, 20);

    field1.setPreferredSize(inSize);
    field2.setPreferredSize(inSize);
    add.setPreferredSize(opSize);
    subtract.setPreferredSize(opSize);
    multiply.setPreferredSize(opSize);
    divide.setPreferredSize(opSize);

    contentPanel = new JPanel();
    contentPanel.setBackground(Color.PINK);
    contentPanel.setLayout(new FlowLayout());

    answerPanel = new Console();

    contentPanel.add(answerPanel);
    contentPanel.add(label1); contentPanel.add(field1); 
    contentPanel.add(label2); contentPanel.add(field2);
    contentPanel.add(add); contentPanel.add(subtract); contentPanel.add(multiply); contentPanel.add(divide);


    this.setContentPane(contentPanel);
    this.revalidate();
}

public void actionPerformed(ActionEvent e) {
    JButton src = (JButton)e.getSource();

    if(src.equals(add)){
        add();
    }
    else if(src.equals(multiply)){
        System.out.println("test");
        multiply();

    }
    else if (src.equals(divide)){
        System.out.println("test");
        divide();
    }

}

private void add(){
    input1 = field1.getText();
    input2 = field2.getText();

    AdditionProblem problem = new AdditionProblem(input1, input2);
    answerPanel.printAnswer(problem);

} 

private void subtract(){
}

private void multiply(){
    input1 = field1.getText();
    input2 = field2.getText();

    MultiplicationProblem problem = new MultiplicationProblem(input1, input2);
    answerPanel.printAnswer(problem);
}

private void divide(){

    input1 = field1.getText();
    input2 = field2.getText();

    DivisionProblem problem = new DivisionProblem(input1, input2);
    answerPanel.printAnswer(problem);

}
}

CONSOLE.CLASS:

public class Console extends JPanel {

private int width = 230, height = 460;
private ArrayList<JPanel> rows;
private ArrayList<JLabel> columns;
private JPanel top;
private JPanel bottom;

private void init(){

    rows = new ArrayList<JPanel>();
    this.setPreferredSize(new Dimension(width, height));
    this.setBackground(Color.WHITE);
    this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

    top = new JPanel();
    top.setPreferredSize(new Dimension(230, 75));
    top.setBackground(Color.WHITE);

    bottom = new JPanel();
    bottom.setPreferredSize(new Dimension(230, 75));
    bottom.setBackground(Color.WHITE);

}

public Console(){

    init();

}

public void printAnswer(AdditionProblem problem){

    int rowIndex = 0;

    JLabel[] sumLabels = problem.getSumLabels();
    JLabel[] addend1Labels = problem.getAddend1Labels();
    JLabel[] addend2Labels = problem.getAddend2Labels();
    JLabel[] carriedLabels = problem.getCarriedLabels();

    reset();



    rows.add(new JPanel());

    for(int i = 0; i < carriedLabels.length; i++){
        rows.get(rowIndex).add(carriedLabels[i]);
    }

    rowIndex++;
    rows.add(new JPanel());

    for(int i = 0; i < addend1Labels.length; i++){
        rows.get(rowIndex).add(addend1Labels[i]);
    }

    rowIndex++;
    rows.add(new JPanel());

    for(int i = 0; i < addend2Labels.length; i++){
        rows.get(rowIndex).add(addend2Labels[i]);
    }

    rowIndex++;
    rows.add(new JPanel());
    String answerLine = "_";

    for(int i = 0; i < sumLabels.length; i++){
        answerLine+="__";
    }
    rows.get(rowIndex).add(new JLabel(answerLine));
    rowIndex++;
    rows.add(new JPanel());

    for(int i = 0; i < sumLabels.length; i++){
        rows.get(rowIndex).add(sumLabels[i]);
    }

    this.add(top);

    for(int i = 0; i < rows.size(); i++){
        rows.get(i).setBackground(Color.WHITE);
        rows.get(i).setAlignmentX(100);
        this.add(rows.get(i));
    }

    this.add(bottom);

    this.revalidate();
}

public void printAnswer(MultiplicationProblem problem){

    reset();


    int rowIndex = 0;

    ArrayList<JLabel[]> carriedLabels = new ArrayList<JLabel[]>();
    JLabel[] factorALabels = new JLabel[problem.getFactorA().length()];
    JLabel[] factorBLabels = new JLabel[problem.getFactorB().length()];
    ArrayList<JLabel[]> productLabels = new ArrayList<JLabel[]>();
    JLabel[] finalProductLabels  = new JLabel[problem.getFinalProduct().length()];
    //CONVERT
    for(int i = 0; i < problem.getCarried().size(); i++){
        JLabel[] tempDigits = new JLabel[problem.getCarried().get(i).length()];
        for(int j = 0; j < problem.getCarried().get(i).length(); j++){
            tempDigits[j] = new JLabel(""+problem.getCarried().get(i).charAt(j));
        }
        carriedLabels.add(tempDigits);
    }

    for(int j = 0; j < problem.getFactorA().length(); j++){
        factorALabels[j] = new JLabel(""+problem.getFactorA().charAt(j));
    }

    for(int j = 0; j < problem.getFactorB().length(); j++){
        factorBLabels[j] = new JLabel(""+problem.getFactorB().charAt(j));
    }

    for(int i = 0; i < problem.getProduct().size(); i++){
        JLabel[] tempDigits = new JLabel[problem.getProduct().get(i).length()];
        for(int j = 0; j < problem.getProduct().get(i).length(); j++){
            tempDigits[j] = new JLabel(""+problem.getProduct().get(i).charAt(j));
        }
        productLabels.add(tempDigits);
    }

    for(int j = 0; j < problem.getFinalProduct().length(); j++){
        finalProductLabels[j] = new JLabel(""+problem.getFinalProduct().charAt(j));
    }

    rows.add(new JPanel());

    //ADD TO ROWS
    for(int i = 0; i < carriedLabels.size(); i++){
        for(int j = 0; j < carriedLabels.get(i).length; j++){
            rows.get(rowIndex).add(carriedLabels.get(i)[j]);
        }
        rows.add(new JPanel());
        rowIndex++;
    }

    for(int j = 0; j < factorALabels.length; j++){
        rows.get(rowIndex).add(factorALabels[j]);
    }

    rows.add(new JPanel());
    rowIndex++;

    for(int j = 0; j < factorBLabels.length; j++){
        rows.get(rowIndex).add(factorBLabels[j]);
    }

    rows.add(new JPanel());
    rowIndex++;

    String answerLine = "_";

    for(int i = 0; i < factorBLabels.length; i++){
        answerLine+="__";
    }
    rows.get(rowIndex).add(new JLabel(answerLine));

    rows.add(new JPanel());
    rowIndex++;

    for(int i = 0; i < productLabels.size(); i++){
        for(int j = 0; j < productLabels.get(i).length; j++){
            rows.get(rowIndex).add(productLabels.get(i)[j]);
        }
        System.out.println(rows.size());
        rows.add(new JPanel());
        rowIndex++;
    }

    answerLine = "_";

    for(int i = 0; i < factorBLabels.length; i++){
        answerLine+="__";
    }
    rows.get(rowIndex).add(new JLabel(answerLine));

    rows.add(new JPanel());
    rowIndex++;

    for(int j = 0; j < finalProductLabels.length; j++){
        rows.get(rowIndex).add(finalProductLabels[j]);
    }



    //PRINT
    this.add(top);

    for(int i = 0; i < rows.size(); i++){
        rows.get(i).setBackground(Color.WHITE);
        this.add(rows.get(i));
    }

    this.add(bottom);

    this.revalidate();
}

public void printAnswer(DivisionProblem problem){

    reset();

    int rowIndex = 0;
    String equation = problem.getDivisor()+"/"+problem.getDividend();
    JLabel[] quotientLabels  = new JLabel[problem.getQuotient().length()];
    JLabel[] equationLabels = new JLabel[equation.length()];
    ArrayList<JLabel[]> workShownLabels = new ArrayList<JLabel[]>();

    //CONVERT

    for(int j = 0; j < problem.getQuotient().length(); j++){
        quotientLabels[j] = new JLabel(""+problem.getQuotient().charAt(j));
    }

    for(int j = 0; j < equation.length(); j++){
            equationLabels[j] = new JLabel(""+equation.charAt(j));
    }


    for(int i = 0; i < problem.getWorkShown().size(); i++){
        JLabel[] tempDigits = new JLabel[problem.getWorkShown().get(i).length()];
        for(int j = 0; j < problem.getWorkShown().get(i).length(); j++){
            tempDigits[j] = new JLabel(""+problem.getWorkShown().get(i).charAt(j));
        }
        workShownLabels.add(tempDigits);
    }

    //ADD ROWS
    rows.add(new JPanel());

    for(int j = 0; j < quotientLabels.length; j++){
        rows.get(rowIndex).add(quotientLabels[j]);
    }

    rows.add(new JPanel());
    rowIndex++;

    for(int j = 0; j < equationLabels.length; j++){
        rows.get(rowIndex).add(equationLabels[j]);
    }

    rows.add(new JPanel());
    rowIndex++;

    for(int i = 0; i < workShownLabels.size(); i++){
        for(int j = 0; j < workShownLabels.get(i).length; j++){
            rows.get(rowIndex).add(workShownLabels.get(i)[j]);
        }
        System.out.println(rows.size());
        rows.add(new JPanel());
        rowIndex++;
    }

    //PRINT
    this.add(top);

        for(int i = 0; i < rows.size(); i++){
            rows.get(i).setBackground(Color.WHITE);
            this.add(rows.get(i));
        }

        this.add(bottom);

        this.revalidate();

}

public void reset(){

    rows.clear();
    this.removeAll();

}

} 

DIVISIONPROBLEM.CLASS:

public class DivisionProblem {

private String dividend,  divisor, quotient, remainder;
private ArrayList<String> workShown;
private int spacing;


public DivisionProblem(String a, String b){

    dividend = a;
    divisor = b;
    quotient = "";
    remainder = "";
    workShown = new ArrayList<String>();
    spacing = 1 + divisor.length();

    solve();

    for(int i = 0; i < spacing; i++){
        quotient = "0" + quotient;
    }

    System.out.println(quotient);
    System.out.println(divisor + "/" + dividend);

    for(int i = 0; i < workShown.size(); i++){

        while(workShown.get(i).length()<quotient.length()){
            workShown.set(i, workShown.get(i) + "0");
        }

        System.out.println(workShown.get(i));

    }

}

public String getDividend(){
    return dividend;
}

public String getDivisor(){
    return divisor;
}

public String getQuotient(){
    return quotient;
}

public ArrayList<String> getWorkShown(){
    return workShown;
}

private void solve(){

    int dvsr = Integer.parseInt(divisor);
    int tempQuotient;
    int tempNum = Integer.parseInt(dividend.charAt(0)+"");
    int tempAnswer;
    int tempSpacing = spacing + 1;
    String tempString;

    for(int i = 0; i < dividend.length(); i++){
        tempQuotient = 0;

        while((dvsr * (tempQuotient+1)) <= tempNum){
            tempQuotient++;
        }

        tempString = (dvsr * tempQuotient)+"";

        while(tempString.length()<tempSpacing){
            tempString = "0"+tempString;
        }

        workShown.add(tempString);


        tempAnswer = tempNum-(dvsr * tempQuotient);



        if(i < dividend.length()-1){
            tempSpacing++;

            tempNum =  ((tempAnswer * 10) + Integer.parseInt(dividend.charAt(i+1)+""));

            tempString = tempNum+"";

            while(tempString.length()<tempSpacing){
                tempString = "0"+tempString;
            }

            workShown.add(tempString);
        }
        else {

            remainder = tempAnswer+"";

            tempString = tempAnswer+"";

            while(tempString.length()<tempSpacing){
                tempString = "0"+tempString;
            }

            workShown.add(tempString);
        }
        quotient += "" + tempQuotient;

    }

}

}
1

There are 1 answers

0
Tim B On

If you want to control the full details then do not use JLabels at all. Create your own control (the easiest way is to subclass JPanel) and implement your own custom renderer.

You can then place and layout the text exactly where you want inside that renderer.

An alternative would be to use a HTML renderer (you will need one more advanced than the standard ones built into Java) and create your layouts using markup.