用JPanels列表制作表格(应用百分比大小列)

时间:2018-08-16 14:47:33

标签: java swing jpanel

在这篇文章Making Table With List Of JPanels中,我可以制作一个带有面板列表的表格。该表具有许多功能,但我需要一个新功能,它正在应用百分比列。这意味着我喜欢所有列的宽度不等于且由浮点数组确定。
这是我的课程表:

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;

public class PanelTable extends JPanel {
    private String data[][];
    private String columns[];
    private int numberOfRows;
    private int numberOfColumns;
    private JTextArea[] header;
    private JTextArea[][] cell;

    public PanelTable(String data[][], String columns[]) {
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

        this.data = data;
        this.columns = columns;


        numberOfRows = data.length;
        numberOfColumns = columns.length;

        makeHeader();
        makeCells();
    }

    public void makeCells() {
        cell = new JTextArea[numberOfRows][numberOfColumns];
        for (int rowIndex = 0; rowIndex < numberOfRows; rowIndex++) {
            add(getRow(numberOfColumns, rowIndex));
        }
    }

    public void makeHeader() {
        header = new JTextArea[numberOfColumns];


        for (int columnIndex = 0; columnIndex < numberOfColumns; columnIndex++) {
            JTextArea ta = new JTextArea();
            ta.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
            ta.setBackground(new Color(57, 77, 112));
            ta.setForeground(Color.WHITE);
            ta.setText(columns[columnIndex]);
            ta.setFont(new Font("Calibri Light", Font.BOLD, 25));
            ta.setLineWrap(true);
            ta.setWrapStyleWord(true);
            ta.setEditable(false);
            header[columnIndex] = ta;
        }

        JPanel rowHeader = new JPanel();
        rowHeader.setLayout(new BoxLayout(rowHeader, BoxLayout.X_AXIS));

        for (int i = 0; i < numberOfColumns; i++) {
            rowHeader.add(header[i]);
        }
        add(rowHeader);
    }


    public Component getRow(int numberOfColumns, int rowIndex) {

        JPanel row = new JPanel();
        //use GridLayout if you want equally spaced columns
        row.setLayout(new BoxLayout(row, BoxLayout.X_AXIS));
        for (int columnIndex = 0; columnIndex < numberOfColumns; columnIndex++) {
            row.add(getCell(rowIndex, columnIndex));
        }
        return row;
    }

    public Component getCell(int rowIndex, int columnIndex) {
        if (cell[rowIndex][columnIndex] == null) {
            JTextArea ta = new JTextArea();
            ta.setLineWrap(true);
            ta.setWrapStyleWord(true);
            ta.setEditable(false);
            ta.setSize(50, ta.getHeight());

            ta.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
            ta.setBorder(BorderFactory.createLineBorder(Color.BLACK));
            ta.setText(data[rowIndex][columnIndex]);
            ta.setFont(new Font("Bmitra", Font.BOLD, 15));

            cell[rowIndex][columnIndex] = ta;
        }

        return cell[rowIndex][columnIndex];
    }

}

这是测试的gui示例:

import javax.swing.*;
import java.awt.*;

public class GUI extends JFrame {
    private PanelTable table;
    private JScrollPane scrollPane;

    public GUI(){
        super("Bank Table");
        JPanel contentPanel = new JPanel();
        setContentPane(contentPanel);
        contentPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        setLayout(new BorderLayout());
        setMinimumSize(new Dimension(1000,700));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        String[][] data= new String[][]{{"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/01"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/02"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/03"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/04"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/05"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/06"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/07"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/08"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/09"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/10"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/11"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/12"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/13"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/14"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/15"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/16"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/17"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/18"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/19"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/20"},
                {"0212670003009", "ص 318", "77081634","380dddddddddddddddddddddddddddddddddddddddddddddddddddddddd00000","0","331142","2102","وصول چک","08:56:46","1397/05/21"},
                {"0212670003009", "ص 318", "77081634","380dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd00000","0","331142","2102","وصول چک","08:56:46","1397/05/22"},
                {"0212670003009", "ص 318", "77081634","3800ddddddddddddddddddddddddddddddddddddddddd0000","0","331142","2102","وصول چک","08:56:46","1397/05/23"},
                {"0212670003009", "ص 318", "77081634","38000ddddddddddddddddddddddddddddddddddddddddddddddddddd000","0","331142","2102","وصول چک","08:56:46","1397/05/24"},
                {"0212670003009", "ص 318", "77081634","38000ddddddddddddddddddddddddddddddddddddddd000","0","331142","2102","وصول چک","08:56:46","1397/05/25"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/26"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/15"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/16"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/17"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/18"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/19"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/20"},
                {"0212670003009", "ص 318", "77081634","380dddddddddddddddddddddddddddddddddddddddddddddddddddddddd00000","0","331142","2102","وصول چک","08:56:46","1397/05/21"},
                {"0212670003009", "ص 318", "77081634","380dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd00000","0","331142","2102","وصول چک","08:56:46","1397/05/22"},
                {"0212670003009", "ص 318", "77081634","3800ddddddddddddddddddddddddddddddddddddddddd0000","0","331142","2102","وصول چک","08:56:46","1397/05/23"},
                {"0212670003009", "ص 318", "77081634","38000ddddddddddddddddddddddddddddddddddddddddddddddddddd000","0","331142","2102","وصول چک","08:56:46","1397/05/24"},
                {"0212670003009", "ص 318", "77081634","38000ddddddddddddddddddddddddddddddddddddddd000","0","331142","2102","وصول چک","08:56:46","1397/05/25"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/26"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/15"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/16"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/17"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/18"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/19"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/20"},
                {"0212670003009", "ص 318", "77081634","380dddddddddddddddddddddddddddddddddddddddddddddddddddddddd00000","0","331142","2102","وصول چک","08:56:46","1397/05/21"},
                {"0212670003009", "ص 318", "77081634","380dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd00000","0","331142","2102","وصول چک","08:56:46","1397/05/22"},
                {"0212670003009", "ص 318", "77081634","3800ddddddddddddddddddddddddddddddddddddddddd0000","0","331142","2102","وصول چک","08:56:46","1397/05/23"},
                {"0212670003009", "ص 318", "77081634","38000ddddddddddddddddddddddddddddddddddddddddddddddddddd000","0","331142","2102","وصول چک","08:56:46","1397/05/24"},
                {"0212670003009", "ص 318", "77081634","38000ddddddddddddddddddddddddddddddddddddddd000","0","331142","2102","وصول چک","08:56:46","1397/05/25"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/26"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/15"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/16"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/17"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/18"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/19"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/20"},
                {"0212670003009", "ص 318", "77081634","380dddddddddddddddddddddddddddddddddddddddddddddddddddddddd00000","0","331142","2102","وصول چک","08:56:46","1397/05/21"},
                {"0212670003009", "ص 318", "77081634","380dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd00000","0","331142","2102","وصول چک","08:56:46","1397/05/22"},
                {"0212670003009", "ص 318", "77081634","3800ddddddddddddddddddddddddddddddddddddddddd0000","0","331142","2102","وصول چک","08:56:46","1397/05/23"},
                {"0212670003009", "ص 318", "77081634","38000ddddddddddddddddddddddddddddddddddddddddddddddddddd000","0","331142","2102","وصول چک","08:56:46","1397/05/24"},
                {"0212670003009", "ص 318", "77081634","38000ddddddddddddddddddddddddddddddddddddddd000","0","331142","2102","وصول چک","08:56:46","1397/05/25"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/26"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/27"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/28"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/29"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/30"}};


        String[] columns= new String[]{"شماره حساب", "اطلاعات اضافه", "مانده","واریز","برداشت","فیش/ حواله","کد شعبه","شرح","ساعت","تاریخ"};
        System.out.println("Hey");
        PanelTable table = new PanelTable(data,columns);
        System.out.println("!!!");

        scrollPane = new JScrollPane(table);
        scrollPane.getVerticalScrollBar().setUnitIncrement(50);

        add(scrollPane,BorderLayout.CENTER);
//        JTextArea textArea = new JTextArea();
//        textArea.setLineWrap(true);
//        textArea.setWrapStyleWord(true);
//        textArea.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
//        textArea.setText("Alireza");
//        add(textArea,BorderLayout.NORTH);

        setVisible(true);
    }

    public static void main(String args[]){
        GUI gui =new GUI();
    }


}

2 个答案:

答案 0 :(得分:1)

执行以下操作: 首先创建一个宽度大小的数组。例如:

 final int[] columnsWidth = { 300, 100, 50, 300, 50 };

然后在要设置文本区域所有属性的方法makeHeader和getCell上,还设置首选大小。请参见下面的代码。这应该给您基于大小数组的宽度。

ta.setPreferredSize(new Dimension(columnsWidth[columnIndex], ta.getHeight())); 

让我知道它是否对您有用:)

下面是完整的代码: 对于GUI:

import javax.swing.*;
import java.awt.*;

public class GUI extends JFrame {
    private PanelTable2 table;
    private JScrollPane scrollPane;

    public GUI(){
        super("Bank Table");
        JPanel contentPanel = new JPanel();
        setContentPane(contentPanel);
        contentPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        setLayout(new BorderLayout());
        setMinimumSize(new Dimension(1000,700));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final String[] columns = { "Big Column", "Serodys", "Ramírez", "Entrada", "MercaSur" };


        final String[][] data = {{"2323", "Hundreds","2323", "Hundreds","2323"},{"2323", "Hundreds","2323", "Hundreds","2323"}}; 
        // final PanelTable22 panel = new PanelTable22(data, columns);

        System.out.println("Hey");
        PanelTable2 table = new PanelTable2(data,columns);
        System.out.println("!!!");

        scrollPane = new JScrollPane(table);
        scrollPane.getVerticalScrollBar().setUnitIncrement(50);

        add(scrollPane,BorderLayout.CENTER);

        setVisible(true);
    }

    public static void main(String args[]){
        GUI gui =new GUI();
    }


}

对于表格模型:

import java.awt.Color;
import java.awt.Component;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class PanelTable2 extends JPanel {
    private String data[][];
    private String columns[];
    private int numberOfRows;
    private int numberOfColumns;
    private JTextArea[] header;
    private JTextArea[][] cell;
    final int[] columnsWidth = { 300, 100, 50, 300, 50 };


    public PanelTable2(String data[][], String columns[]) {
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

        this.data = data;
        this.columns = columns;


        numberOfRows = data.length;
        numberOfColumns = columns.length;

        makeHeader();
        makeCells();
    }

    public void makeCells() {
        cell = new JTextArea[numberOfRows][numberOfColumns];
        for (int rowIndex = 0; rowIndex < numberOfRows; rowIndex++) {
            add(getRow(numberOfColumns, rowIndex));
        }
    }

    public void makeHeader() {
        header = new JTextArea[numberOfColumns];


        for (int columnIndex = 0; columnIndex < numberOfColumns; columnIndex++) {
            JTextArea ta = new JTextArea();
            ta.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); 
            ta.setPreferredSize(new Dimension(columnsWidth[columnIndex], ta.getHeight())); 
            ta.setBackground(new Color(57, 77, 112));
            ta.setForeground(Color.WHITE);
            ta.setText(columns[columnIndex]);
            ta.setFont(new Font("Calibri Light", Font.BOLD, 25));
            ta.setLineWrap(true);

            ta.setWrapStyleWord(true);
            ta.setEditable(false);
            header[columnIndex] = ta;
        }

        JPanel rowHeader = new JPanel();
        rowHeader.setLayout(new BoxLayout(rowHeader, BoxLayout.X_AXIS));

        for (int i = 0; i < numberOfColumns; i++) {
            rowHeader.add(header[i]);
        }
        add(rowHeader);
    }


    public Component getRow(int numberOfColumns, int rowIndex) {

        JPanel row = new JPanel();
        //use GridLayout if you want equally spaced columns
        row.setLayout(new BoxLayout(row, BoxLayout.X_AXIS));
        for (int columnIndex = 0; columnIndex < numberOfColumns; columnIndex++) {
            row.add(getCell(rowIndex, columnIndex));
        }
        return row;
    }

    public Component getCell(int rowIndex, int columnIndex) {
        if (cell[rowIndex][columnIndex] == null) {
            JTextArea ta = new JTextArea();
            ta.setLineWrap(true);
            ta.setWrapStyleWord(true);
            ta.setEditable(false);

            ta.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
            ta.setBorder(BorderFactory.createLineBorder(Color.BLACK));
            ta.setText(data[rowIndex][columnIndex]);
            ta.setFont(new Font("Bmitra", Font.BOLD, 15));
            ta.setPreferredSize(new Dimension(columnsWidth[columnIndex], ta.getHeight())); 

            cell[rowIndex][columnIndex] = ta;
        }

        return cell[rowIndex][columnIndex];
    }

}

答案 1 :(得分:0)

关键是这些行:

//use GridLayout if you want equally spaced columns
row.setLayout(new BoxLayout(row, BoxLayout.X_AXIS));

要将约束应用于列,可以使用GridBagLayout代替BoxLayout

这是使用GridBagLayout的非常基本的技巧:

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.io.IOException;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class PanelTable2 extends JPanel {

    public PanelTable2() {

        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        setPreferredSize(new Dimension(400, 150));
        add(getRow());
    }

    Component getRow() {

        int[] percent =  {10,40,50};

        JPanel row = new JPanel();
        GridBagLayout layout = new GridBagLayout();
        row.setLayout(layout);

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.anchor = GridBagConstraints.CENTER;

        for (int i= 0; i < percent.length; i++) {
            gbc.gridy = 0 ; gbc.gridx = i;
            gbc.weightx = percent[i];
            Component component =  getCell();
            row.add(component, gbc);
        }
        return row;
    }

    Component getCell() {

        JTextArea ta = new JTextArea(4,11);
        ta.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        ta.setFont(new Font("Bmitra", Font.BOLD, 15));
        return ta;
    }

    public static void main(String[] args) throws IOException{

        JFrame window = new JFrame();
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.add(new PanelTable2());
        window.pack();
        window.setVisible(true);
    }
}

请注意,mcve不应代表您的应用程序。它应该是问题的简单表示。
GridBagLayout绝不是唯一的选择。您还可以探索其他选择,例如SpringLayout