Hi,
I have been working on the following Java GUI class. All it does is display a JScrollPane, and within that pane displays arrays of numbers, each of which has an associated label. The main class is as follows:-
import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Color; import java.awt.Graphics; import javax.swing.BoxLayout; import javax.swing.border.EmptyBorder; import javax.swing.border.Border; import javax.swing.BorderFactory; /** * Write a description of class MatrixDisplayPanel here. * * @author Jeremy Watts * @version 23-01-2019 */ public class MatrixDisplayPanel extends JPanel { // instance variables int rowNumber; int columnNumber; /** * Constructor for objects of class MatrixDisplayFrame */ public MatrixDisplayPanel(String[][] jLabelString, String matrixLabel) { // initialise instance variables this.rowNumber = jLabelString.length; this.columnNumber = jLabelString[jLabelString.length - 1].length; create(jLabelString, matrixLabel); } /** * An example of a method - replace this comment with your own * * @param y a sample parameter for a method * @return the sum of x and y */ private void create(String[][] jLabelString, String matrixLabel) { JPanel gridPanel; JLabel[][] jLabel; int rowIndex; int columnIndex; gridPanel = new JPanel(new GridLayout(this.rowNumber, this.columnNumber, 15, 15)); jLabel = new JLabel[this.rowNumber][this.columnNumber]; Border border = BorderFactory.createLineBorder(Color.GRAY, 1); for(rowIndex = 0; rowIndex <= this.rowNumber - 1; rowIndex++) { for(columnIndex = 0; columnIndex <= this.columnNumber - 1; columnIndex++) { jLabel[rowIndex][columnIndex] = new JLabel(); jLabel[rowIndex][columnIndex].setText(jLabelString[rowIndex][columnIndex]); jLabel[rowIndex][columnIndex].setFont(new java.awt.Font("Tahoma", 0, 15)); jLabel[rowIndex][columnIndex].setBorder(border); gridPanel.add(jLabel[rowIndex][columnIndex]); } } gridPanel.setOpaque(true); gridPanel.setBackground(Color.GREEN); JPanel mainPanel; mainPanel = new JPanel(); mainPanel.setBorder(new EmptyBorder(20, 20, 20, 20)); mainPanel.add(gridPanel); mainPanel.setOpaque(true); mainPanel.setBackground(Color.RED); JPanel labelPanel = new JPanel(); JLabel label = new JLabel(matrixLabel + " = "); label.setFont(new java.awt.Font("Tahoma", 0, 18)); labelPanel.setOpaque(true); labelPanel.setBackground(new Color(232, 232, 232)); labelPanel.add(label); labelPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); this.add(labelPanel); this.add(mainPanel); this.setOpaque(true); this.setBackground(Color.BLUE); } }
The following class, called 'ResultsPanel' , then takes instances of the above, and puts them onto a JPanel using BoxLayout as the Layout Manager. It is as follows:-
import java.awt.Component; import javax.swing.JPanel; import javax.swing.BoxLayout; import javax.swing.Box; import java.awt.Dimension; /** * Is the JPanel that holds all currently declared matrices * * @author Jeremy Watts * @version 24-01-2019 */ public class ResultsPanel extends JPanel { // instance variables private int bufferHeight = 50; private int numberOfResults; /** * Constructor for objects of class MatricesPanel */ public ResultsPanel() { this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); this.numberOfResults = 0; } /** * An example of a method - replace this comment with your own * * @param y a sample parameter for a method * @return the sum of x and y */ public void addMatrixDisplayPanel(MatrixDisplayPanel matrixDisplayPanel) { matrixDisplayPanel.setAlignmentX(Component.LEFT_ALIGNMENT); this.add(matrixDisplayPanel); this.numberOfResults++; } }
Finally, an instantiator class is used:-
import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.Box; import java.awt.Dimension; /** * Write a description of class Run2 here. * * @author (your name) * @version (a version number or a date) */ public class Run2 { public static void main(String[] args) { String[][] jLabelString = new String[4][4]; jLabelString[0][0] = " 0.0 + 9.67i"; jLabelString[0][1] = "2.6598663"; jLabelString[0][2] = "100234"; jLabelString[0][3] = "1.09864653"; jLabelString[1][0] = "-2/4 + 8/9i"; jLabelString[1][1] = "2095.6789457"; jLabelString[1][2] = "-2845"; jLabelString[1][3] = "8.906343"; jLabelString[2][0] = "-0.00989"; jLabelString[2][1]= "9.9067"; jLabelString[2][2] = "1.56"; jLabelString[2][3] = "7.894557446"; jLabelString[3][0] = "-0.00989"; jLabelString[3][1]= "9.9067"; jLabelString[3][2] = "1.56"; jLabelString[3][3] = "1.56"; MatrixDisplayPanel mdp1 = new MatrixDisplayPanel(jLabelString, "A"); String[][] jLabelString2 = new String[5][5]; jLabelString2[0][0] = " 0.0 + 9.67i"; jLabelString2[0][1] = "2.6598663"; jLabelString2[0][2] = "100234"; jLabelString2[0][3] = "1.09864653"; jLabelString2[0][4] = "1.09864653"; jLabelString2[1][0] = "-2/4 + 8/9i"; jLabelString2[1][1] = "2095.6789457"; jLabelString2[1][2] = "-2845"; jLabelString2[1][3] = "8.906343"; jLabelString2[1][4] = "8.906343"; jLabelString2[2][0] = "-0.00989"; jLabelString2[2][1]= "9.9067"; jLabelString2[2][2] = "1.56"; jLabelString2[2][3] = "7.894557446"; jLabelString2[2][4] = "7.894557446"; jLabelString2[3][0] = "-0.00989"; jLabelString2[3][1]= "9.9067"; jLabelString2[3][2] = "1.56"; jLabelString2[3][3] = "1.56"; jLabelString2[3][4] = "1.56"; jLabelString2[4][0] = "-0.00989"; jLabelString2[4][1]= "9.9067"; jLabelString2[4][2] = "1.56"; jLabelString2[4][3] = "1.56"; jLabelString2[4][4] = "1.56"; MatrixDisplayPanel mdp2 = new MatrixDisplayPanel(jLabelString2, "B"); /*JScrollPane jsp = new JScrollPane(new MatrixDisplayPanel(jLabelString, "A")); mdf.add(jsp);*/ String[][] jLabelString3 = new String[6][6]; jLabelString3[0][0] = " 0.0 + 9.67i"; jLabelString3[0][1] = "2.6598663"; jLabelString3[0][2] = "100234"; jLabelString3[0][3] = "1.09864653"; jLabelString3[0][4] = "1.09864653"; jLabelString3[0][5] = "1.09864653"; jLabelString3[1][0] = "-2/4 + 8/9i"; jLabelString3[1][1] = "2095.6789457"; jLabelString3[1][2] = "-2845"; jLabelString3[1][3] = "8.906343"; jLabelString3[1][4] = "8.906343"; jLabelString3[1][5] = "8.906343"; jLabelString3[2][0] = "-0.00989"; jLabelString3[2][1]= "9.9067"; jLabelString3[2][2] = "1.56"; jLabelString3[2][3] = "7.894557446"; jLabelString3[2][4] = "7.894557446"; jLabelString3[2][5] = "7.894557446"; jLabelString3[3][0] = "-0.00989"; jLabelString3[3][1]= "9.9067"; jLabelString3[3][2] = "1.56"; jLabelString3[3][3] = "1.56"; jLabelString3[3][4] = "1.56"; jLabelString3[3][5] = "1.56"; jLabelString3[4][0] = "-0.00989"; jLabelString3[4][1]= "9.9067"; jLabelString3[4][2] = "1.56"; jLabelString3[4][3] = "1.56"; jLabelString3[4][4] = "1.56"; jLabelString3[4][5] = "1.56"; jLabelString3[5][0] = "-0.00989"; jLabelString3[5][1]= "9.9067"; jLabelString3[5][2] = "1.56"; jLabelString3[5][3] = "1.56"; jLabelString3[5][4] = "1.56"; jLabelString3[5][5] = "1.56"; MatrixDisplayPanel mdp3 = new MatrixDisplayPanel(jLabelString3, "C"); ResultsPanel res = new ResultsPanel(); res.addMatrixDisplayPanel(mdp1); res.addMatrixDisplayPanel(mdp2); res.addMatrixDisplayPanel(mdp3); JFrame mdf = new JFrame(); JScrollPane jsp = new JScrollPane(res); mdf.add(jsp); mdf.pack(); mdf.setVisible(true); } }
If you run the code, then you can see 3 arrays of numbers, each with the labels 'A','B' and 'C'. Basically, what I am trying to do is to get the 'A' and 'B' arrays to line up with the bottom one. I simply want it always to be the case that the labels for any array is far flush to the left and always in line with eachother. (P.S. I have coloured the various panels used to make it easier to discern what's going on)
My intention is to get the 'labels' all linedScreenshot from 2019-01-25 12-14-47.jpg up flush to the left, all in line. I thought that setting the alignment to LEFT_ALIGNMENT would have this effect, but apparently not.
Jeremy