Java 表格格式化

发布于 2024-10-16 23:20:45 字数 6173 浏览 3 评论 0原文

我正在使用 Netbeans 使用 Java 进行编程。

我有一张列出患者的表格。我读取了 XML 文件中的信息。 这里有代码:

package digiscope;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;

/**
 *
 * @author Daniel
 */
public class TabelaPaciente extends javax.swing.JPanel {


 String numero_processo;
    int localizador = 0;

    /** Creates new form TabelaPaciente */

    public TabelaPaciente() {
        initComponents();
        load_table();
    }

    public void setNumeroProc(String numero) {
        numero_processo = numero;
    }

    public String getNumeroProc() {
        return numero_processo;
    }

    public int getLocalizador() {
        return localizador;
    }

    String[][] concat(String[][] A, String[][] B) {
        String[][] C = new String[A.length + B.length][A.length + B.length];
        System.arraycopy(A, 0, C, 0, A.length);
        System.arraycopy(B, 0, C, A.length, B.length);

        return C;
    }

    private void load_table() {

        File dir;
        String[] filenames;

        //dir = new File(System.getProperty("user.dir")+ "/XML");
        dir = new File(System.getProperty("user.dir") + "/Processos");

        filenames = dir.list();

        String[][] table_final = new String[][]{};

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

            String n_proc = filenames[i];//filenames[i].substring(0,filenames[i].length()-4);

            //System.out.println(filenames[i]);

            Ler_XML xml = new Ler_XML(n_proc);
            String nome_utente = xml.getNome();

            String[][] line = new String[][]{
                {n_proc, nome_utente, null, "ir para formulario"}
            };

            table_final = concat(table_final, line);
        }

        tabela_pacientes.setModel(new javax.swing.table.DefaultTableModel(
                table_final,
                new String[]{
                    "Process Nº", "Name", "Date", "Form state"
                }));
    }



    void executa_accao(int column, int row) {

        if (column == 3) {
            Object numero_proc = tabela_pacientes.getValueAt(row, 0);

            localizador = 1;

            numero_processo = numero_proc.toString();

            JanelaPrincipal cont = (JanelaPrincipal) this.getParent() //    Internal Pane
                    .getParent() // ???
                    .getParent() // ???
                    .getParent() // ???
                    .getParent();   //  Frame

            cont.loadPanel(new Formulario(1));

            Preencher_FormPaciente new_xml = new Preencher_FormPaciente(numero_processo, 1);

        }

    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tabela_pacientes = new javax.swing.JTable();
        btnMenu1 = new javax.swing.JButton();

        setPreferredSize(new java.awt.Dimension(1000, 550));

        tabela_pacientes.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent event) {
                int row = tabela_pacientes.rowAtPoint(event.getPoint());
                int column = tabela_pacientes.columnAtPoint(event.getPoint());
                if (row >= 0 && column >= 0) {
                    executa_accao(column, row);
                }
            }
        });
        jScrollPane1.setViewportView(tabela_pacientes);

        btnMenu1.setBackground(new java.awt.Color(255, 153, 0));
        btnMenu1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        btnMenu1.setText("MENU");
        btnMenu1.setPreferredSize(new java.awt.Dimension(105, 75));
        btnMenu1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMenu1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnMenu1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(885, Short.MAX_VALUE))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 1000, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnMenu1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 446, Short.MAX_VALUE))
        );
    }// </editor-fold>

    private void btnMenu1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            JanelaPrincipal cont = (JanelaPrincipal) this.getParent() //    Internal Pane
                    .getParent() // ???
                    .getParent() // ???
                    .getParent() // ???
                    .getParent();   //  Frame
            cont.loadPanel(new Menu());
    }                                        

    // Variables declaration - do not modify
    private javax.swing.JButton btnMenu1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tabela_pacientes;
    // End of variables declaration
}

我想知道是否可以增加字母大小? 您可以在此处查看表格的预览。 在此处输入图像描述

感谢您的合作。

此致。

I’im programing in Java with Netbeans.

I have a table who list patients. I read the information in XML file.
Here you have the code:

package digiscope;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;

/**
 *
 * @author Daniel
 */
public class TabelaPaciente extends javax.swing.JPanel {


 String numero_processo;
    int localizador = 0;

    /** Creates new form TabelaPaciente */

    public TabelaPaciente() {
        initComponents();
        load_table();
    }

    public void setNumeroProc(String numero) {
        numero_processo = numero;
    }

    public String getNumeroProc() {
        return numero_processo;
    }

    public int getLocalizador() {
        return localizador;
    }

    String[][] concat(String[][] A, String[][] B) {
        String[][] C = new String[A.length + B.length][A.length + B.length];
        System.arraycopy(A, 0, C, 0, A.length);
        System.arraycopy(B, 0, C, A.length, B.length);

        return C;
    }

    private void load_table() {

        File dir;
        String[] filenames;

        //dir = new File(System.getProperty("user.dir")+ "/XML");
        dir = new File(System.getProperty("user.dir") + "/Processos");

        filenames = dir.list();

        String[][] table_final = new String[][]{};

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

            String n_proc = filenames[i];//filenames[i].substring(0,filenames[i].length()-4);

            //System.out.println(filenames[i]);

            Ler_XML xml = new Ler_XML(n_proc);
            String nome_utente = xml.getNome();

            String[][] line = new String[][]{
                {n_proc, nome_utente, null, "ir para formulario"}
            };

            table_final = concat(table_final, line);
        }

        tabela_pacientes.setModel(new javax.swing.table.DefaultTableModel(
                table_final,
                new String[]{
                    "Process Nº", "Name", "Date", "Form state"
                }));
    }



    void executa_accao(int column, int row) {

        if (column == 3) {
            Object numero_proc = tabela_pacientes.getValueAt(row, 0);

            localizador = 1;

            numero_processo = numero_proc.toString();

            JanelaPrincipal cont = (JanelaPrincipal) this.getParent() //    Internal Pane
                    .getParent() // ???
                    .getParent() // ???
                    .getParent() // ???
                    .getParent();   //  Frame

            cont.loadPanel(new Formulario(1));

            Preencher_FormPaciente new_xml = new Preencher_FormPaciente(numero_processo, 1);

        }

    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tabela_pacientes = new javax.swing.JTable();
        btnMenu1 = new javax.swing.JButton();

        setPreferredSize(new java.awt.Dimension(1000, 550));

        tabela_pacientes.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent event) {
                int row = tabela_pacientes.rowAtPoint(event.getPoint());
                int column = tabela_pacientes.columnAtPoint(event.getPoint());
                if (row >= 0 && column >= 0) {
                    executa_accao(column, row);
                }
            }
        });
        jScrollPane1.setViewportView(tabela_pacientes);

        btnMenu1.setBackground(new java.awt.Color(255, 153, 0));
        btnMenu1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        btnMenu1.setText("MENU");
        btnMenu1.setPreferredSize(new java.awt.Dimension(105, 75));
        btnMenu1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMenu1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnMenu1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(885, Short.MAX_VALUE))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 1000, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnMenu1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 446, Short.MAX_VALUE))
        );
    }// </editor-fold>

    private void btnMenu1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            JanelaPrincipal cont = (JanelaPrincipal) this.getParent() //    Internal Pane
                    .getParent() // ???
                    .getParent() // ???
                    .getParent() // ???
                    .getParent();   //  Frame
            cont.loadPanel(new Menu());
    }                                        

    // Variables declaration - do not modify
    private javax.swing.JButton btnMenu1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tabela_pacientes;
    // End of variables declaration
}

I would like to know if it’s possible to increase the letter size?
You can see here a preview of the table.
enter image description here

Thank you for your collaboration.

Best regards.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半衾梦 2024-10-23 23:20:45

在许多情况下,一个表将使用多个呈现器来呈现数据。一种用于字符串、数字、日期等。默认渲染器使用表格的字体来渲染文本。要更改所有渲染器的字体,您可以使用:

Font font = table.getFont();
table.setFont( font.deriveFont(24.0f) );
table.setRowHeight(24);

您也可以对表标题执行此操作。

In many cases a table will use multiple renderers to render the data. One for Strings, Numbers, Dates etc. The default renderers use the Font of the table to render the text. To change the font for all renderers you can use:

Font font = table.getFont();
table.setFont( font.deriveFont(24.0f) );
table.setRowHeight(24);

You can do this for the table header as well.

阳光①夏 2024-10-23 23:20:45

JTable 中单元格的显示由 TableCell渲染器默认 实现从 JLabel 扩展而来,有控制字体的方法。例如...

DefaultTableCellRenderer newRenderer = new DefaultTableCellRenderer();
Font oldFont = newRenderer.getFont();
Font bigFont = new Font(oldFont.getName(),oldFont.getStyle(),24);
newRenderer.setFont(bigFont);
table.setDefaultRenderer(Object.class,newRenderer);

The display of a cell in a JTable is controlled by the TableCellRenderer. The default implementation extends from JLabel which has methods to control font. For example...

DefaultTableCellRenderer newRenderer = new DefaultTableCellRenderer();
Font oldFont = newRenderer.getFont();
Font bigFont = new Font(oldFont.getName(),oldFont.getStyle(),24);
newRenderer.setFont(bigFont);
table.setDefaultRenderer(Object.class,newRenderer);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文