[英]JTable not changing appearance
您好,早上好,
我正在請求有關樣式化JTable的幫助。 我對Java GUI非常陌生,因此想讓它正常工作真是令人費解。
我的目標是嘗試盡可能重新創建“危險”計分板。
現在,我正在嘗試完成3件事:
我的代碼在下面列出。 任何幫助,反饋或指導將不勝感激。
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package QuestionBoard;
import java.awt.*;
import javax.swing.table.*;
import javax.swing.*;
/**
*
* @author Kelly
*/
public class WiP_QuestionBoard extends javax.swing.JFrame {
/**
* Creates new form frameQuestionBoard
*/
public WiP_QuestionBoard() {
initComponents();
//Trying to center components
DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
centerRenderer.setHorizontalAlignment( SwingConstants.CENTER );
tblQuestions.setDefaultRenderer(Integer.class, centerRenderer);
//Trying to change header settings
JTableHeader Theader = tblQuestions.getTableHeader();
Theader.setBackground(Color.BLUE);
// Theader.setFontColor???(Color.YELLOW);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/**
* 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() {
panelQuestions = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
tblQuestions = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
tblQuestions.setBackground(new java.awt.Color(51, 102, 255));
tblQuestions.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 2));
tblQuestions.setFont(new java.awt.Font("Tahoma", 1, 13)); // NOI18N
tblQuestions.setForeground(new java.awt.Color(204, 153, 0));
tblQuestions.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{"200", "200", "200", "200", "200", "200"},
{"400", "400", "400", "400", "400", "400"},
{"600", "600", "600", "600", "600", "600"},
{"800", "800", "800", "800", "800", "800"},
{"1000", "1000", "1000", "1000", "1000", "1000"}
},
new String [] {
"Category 1", "Category 2", "Category 3", "Category 4", "Category 5", "Category 6"
}
));
tblQuestions.setDoubleBuffered(true);
tblQuestions.setGridColor(new java.awt.Color(0, 0, 0));
tblQuestions.setRowMargin(2);
tblQuestions.setSelectionBackground(new java.awt.Color(0, 51, 255));
tblQuestions.setSelectionForeground(new java.awt.Color(204, 204, 0));
tblQuestions.getTableHeader().setReorderingAllowed(false);
jScrollPane1.setViewportView(tblQuestions);
javax.swing.GroupLayout panelQuestionsLayout = new javax.swing.GroupLayout(panelQuestions);
panelQuestions.setLayout(panelQuestionsLayout);
panelQuestionsLayout.setHorizontalGroup(
panelQuestionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 675, Short.MAX_VALUE)
);
panelQuestionsLayout.setVerticalGroup(
panelQuestionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panelQuestionsLayout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 313, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(panelQuestions, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(panelQuestions, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(WiP_QuestionBoard.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(WiP_QuestionBoard.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(WiP_QuestionBoard.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(WiP_QuestionBoard.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new WiP_QuestionBoard().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JPanel panelQuestions;
private javax.swing.JTable tblQuestions;
// End of variables declaration
}
我是Java GUI的新手
我建議您不要使用IDE生成代碼。 您花時間學習IDE,而不是Swing。 使用IDE來幫助編譯,調試等。
1.嘗試將表格中的所有數據(包括標題)居中
tblQuestions.setDefaultRenderer(Integer.class, centerRenderer);
TableModel中的所有數據都是String數據,而不是Integer數據。 如果需要整數數據,則需要將整數對象添加到模型中。
但是,僅此還不夠。 現在,您需要通過重寫TableModel的getColumnClass(...)
方法以返回Integer.class來告訴表各列中的數據類型。
但是,如您的代碼所示,最簡單的解決方案是使用:
tblQuestions.setDefaultRenderer(Object.class, centerRenderer);
因為默認的getColumnClass(...)
實現只為所有列返回Object.class。
對於標題,您需要訪問默認渲染器以更改對齊方式:
DefaultTableCellRenderer renderer = (DefaultTableCellRenderer)tblQuestions.getTableHeader().getDefaultRenderer();
renderer.setHorizontalAlignment(SwingConstants.CENTER);
2.嘗試更改標題字體的顏色和背景
這是Nimbus LAF的問題。 默認渲染器似乎忽略了setBackground()方法。 不知道如何解決。
也許此發布會有所幫助: Nimbus-覆蓋TableHeader的顏色
3.嘗試擴展表格以適合其所在的JPanel的整個框架
表格的首選大小基於表格中每一行的高度。 該高度不會動態變化。 如果您希望每行的高度更大,請使用:
table.setRowHeight(32);
另外,按照我的最初評論,我不會使用IDE來生成布局代碼。 GroupLayout過於復雜。 我將簡單地利用框架的默認BorderLayout來使用以下代碼創建GUI:
JTable table = new JTable(...);
frame.add(new JScrollPane( table ), BorderLayout.PAGE_START);
frame.pack();
frame.setVisible();
使用簡單的布局代碼,表格將以其首選大小顯示在框架的頂部。 隨着框架寬度的更改,列的大小將重新調整。 如果更改框架高度,則什么也不會發生,因此您可能希望使框架不可調整大小,以確保表格始終填充框架。
這是滿足您要求的代碼,將構造函數更改為以下代碼(有一些注釋可以幫助您):
public WiP_QuestionBoard() {
initComponents();
//Makes the table header text centered.
((DefaultTableCellRenderer) tblQuestions.getTableHeader().getDefaultRenderer()).setHorizontalAlignment(SwingConstants.CENTER);
//This will center the text in table (header not included).
DefaultTableCellRenderer centerAlign = new DefaultTableCellRenderer();
//center columns.
centerAlign.setHorizontalAlignment(SwingConstants.CENTER);
for (int i = 0; i < tblQuestions.getColumnCount(); i++) {
tblQuestions.getColumnModel().getColumn(i).setCellRenderer(centerAlign);
}
//makes the table span the panel it is in.
tblQuestions.setPreferredSize(this.panelQuestions.getPreferredSize());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.