gpt4 book ai didi

java - 使用 JPanels 列表制作表格(使用百分比大小列)

转载 作者:行者123 更新时间:2023-11-30 10:12:27 25 4
gpt4 key购买 nike

在这篇文章中 Making Table With List Of JPanels我可以制作一个包含面板列表的表格。这张表有很多功能,但我需要一个新功能,它正在应用百分比列。这意味着我喜欢所有列的宽度不等于并且用 float 数组确定。
这是我的类(class)表:

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[]{"ستون ۲", "ستون ۱", "ستون ۱۰","ستون ۹","ستون ۸","ستون ۷","ستون ۶","ستون ۵","ستون ۴","ستون ۳"};
PanelTable table = new PanelTable(data,columns);

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

add(scrollPane,BorderLayout.CENTER);

setVisible(true);
}

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


}

最佳答案

这样做:首先创建一个宽度大小的数组。例如:

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

然后在您的方法 makeHeader 和 getCell 中设置文本区域的所有属性,同时设置首选大小。请参阅下面的代码。这应该为您提供基于大小数组的宽度。

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

让我知道它是否适合你:)

完整代码如下:对于图形用户界面:

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];
}

}

关于java - 使用 JPanels 列表制作表格(使用百分比大小列),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51879830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com