Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.awt.Dimension;

import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Main {
    private final static int ROW_HEIGHT = 25;

    /**
     *
     * @param strs an array of strings, where each string will be made into a JLabel
     * @return a panel containing a column of JLabels
     */
    public static JPanel getJLabelColumn(String[] strs) {
        JLabel[] labels = new JLabel[strs.length];

        for (int i = 0; i < strs.length; i++) {
            labels[i] = new JLabel(strs[i]);
        }
        return getComponentColumn(labels);
    }

    public static JPanel getComponentColumn(JComponent[] components) {
        JPanel columnPanel = new JPanel();
        columnPanel.setLayout(new BoxLayout(columnPanel, BoxLayout.PAGE_AXIS));

        for (int i = 0; i < components.length; i++) {
            components[i].setMinimumSize(new Dimension(components[i].getPreferredSize().width, ROW_HEIGHT));
            components[i].setPreferredSize(new Dimension(components[i].getPreferredSize().width, ROW_HEIGHT));
            components[i].setMaximumSize(new Dimension(components[i].getPreferredSize().width, ROW_HEIGHT));
            components[i].setAlignmentX(JComponent.LEFT_ALIGNMENT);
            columnPanel.add(components[i]);
        }
        return columnPanel;
    }
}