Java 表格格式化
我正在使用 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.
Thank you for your collaboration.
Best regards.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在许多情况下,一个表将使用多个呈现器来呈现数据。一种用于字符串、数字、日期等。默认渲染器使用表格的字体来渲染文本。要更改所有渲染器的字体,您可以使用:
您也可以对表标题执行此操作。
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:
You can do this for the table header as well.
JTable 中单元格的显示由 TableCell渲染器。 默认 实现从 JLabel 扩展而来,有控制字体的方法。例如...
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...