【发布时间】:2014-01-18 23:07:55
【问题描述】:
我在 NetBeans 中有以下面板(只有前三分之一是我的工作,其余部分是自动生成且不可修改的):
public class ScorePanel extends javax.swing.JPanel {
private DefaultTableModel dtm;
public ScorePanel() {
initComponents();
dtm = (DefaultTableModel) scoreTable.getModel();
for(int i = dtm.getRowCount() - 1; i >= 0; i--)
dtm.removeRow(i);
loadFile();
}
private void loadFile(){
FileInputStream ins = null;
try {
ins = new FileInputStream("./src/Resources/score.txt");
Scanner fScanner = new Scanner(ins, "UTF-8");
String[] str;
while(fScanner.hasNextLine()){
str = fScanner.nextLine().split(";");
dtm.addRow(new Object[]{str[0].trim(), Integer.parseInt(str[1].trim()), str[2]});
}
fScanner.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(ScorePanel.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
if(ins != null)
ins.close();
} catch (IOException ex) {
Logger.getLogger(ScorePanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
// The rest of the code was auto-generated by NetBeans and cannot be modified
private void initComponents() {
titleLabel = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
scoreTable = new javax.swing.JTable();
menuButton = new javax.swing.JButton();
nameField = new javax.swing.JTextField();
scoreLabel = new javax.swing.JLabel();
timeLabel = new javax.swing.JLabel();
addButton = new javax.swing.JButton();
saveButton = new javax.swing.JButton();
setMaximumSize(new java.awt.Dimension(640, 480));
setMinimumSize(new java.awt.Dimension(640, 480));
setPreferredSize(new java.awt.Dimension(640, 480));
setLayout(null);
titleLabel.setFont(new java.awt.Font("Snap ITC", 2, 36)); // NOI18N
titleLabel.setForeground(new java.awt.Color(51, 102, 0));
titleLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
titleLabel.setText("Zaku Attack!");
add(titleLabel);
titleLabel.setBounds(170, 56, 300, 47);
scoreTable.setAutoCreateColumnsFromModel(false);
scoreTable.setAutoCreateRowSorter(true);
scoreTable.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null},
{null, null, null},
{null, null, null},
{null, null, null}
},
new String [] {
"Name", "Score", "Time"
}
) {
Class[] types = new Class [] {
java.lang.String.class, java.lang.Integer.class, java.lang.String.class
};
boolean[] canEdit = new boolean [] {
false, false, false
};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
scoreTable.setRowSelectionAllowed(false);
scoreTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
jScrollPane1.setViewportView(scoreTable);
add(jScrollPane1);
jScrollPane1.setBounds(160, 191, 320, 190);
menuButton.setText("Back to Menu");
menuButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
menuButtonActionPerformed(evt);
}
});
add(menuButton);
menuButton.setBounds(250, 410, 137, 23);
add(nameField);
nameField.setBounds(160, 170, 130, 20);
add(scoreLabel);
scoreLabel.setBounds(290, 170, 60, 20);
add(timeLabel);
timeLabel.setBounds(350, 170, 60, 20);
addButton.setText("Add");
addButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addButtonActionPerformed(evt);
}
});
add(addButton);
addButton.setBounds(410, 170, 70, 23);
saveButton.setText("Save to File");
saveButton.setMaximumSize(menuButton.getMaximumSize());
saveButton.setMinimumSize(menuButton.getMinimumSize());
saveButton.setPreferredSize(menuButton.getPreferredSize());
saveButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
saveButtonActionPerformed(evt);
}
});
add(saveButton);
saveButton.setBounds(250, 390, 140, 23);
}// </editor-fold>
private void menuButtonActionPerformed(java.awt.event.ActionEvent evt) {
parent.switchPanel(MainWindow.PanelName.MENU);
}
private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {
if(!nameField.getText().isEmpty()){
dtm.addRow(new Object[]{nameField.getText(), scoreLabel.getText(), timeLabel.getText()});
nameField.setText("");
scoreLabel.setText("");
timeLabel.setText("");
addButton.setEnabled(false);
scoreTable.revalidate();
}
}
private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {
if(dtm.getRowCount() != 0){
File file = new File("./src/Resources/score.txt");
try {
if(!file.exists())
file.createNewFile();
FileWriter fw = new FileWriter(file);
int i;
for(i = 0; i < dtm.getRowCount(); i++)
fw.write(dtm.getValueAt(i, 0) + ";" +
dtm.getValueAt(i, 1)+ ";" +
dtm.getValueAt(i, 2));
fw.close();
JOptionPane.showMessageDialog(this,"Done saving file.","Completed",JOptionPane.INFORMATION_MESSAGE);
} catch (IOException ex1) {
JOptionPane.showMessageDialog(null,"Error while saving file.","Error",JOptionPane.ERROR_MESSAGE);
}
}
}
// Variables declaration - do not modify
private javax.swing.JButton addButton;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JButton menuButton;
private javax.swing.JTextField nameField;
private javax.swing.JButton saveButton;
private javax.swing.JLabel scoreLabel;
private javax.swing.JTable scoreTable;
private javax.swing.JLabel timeLabel;
private javax.swing.JLabel titleLabel;
// End of variables declaration
}
我的问题是面板的唯一表是不可见的,即使在调用 setVisible(true) 后也不会显示。表格的数据模型确实有表格的内容,并且所述内容可以正常读取和写入 - 它们只是不可见。
从我最近的类似问题来看,会不会是面板布局或外观造成的?
【问题讨论】:
-
如需尽快获得更好的帮助,请发帖 MCVE。请注意,当您将一个组件添加到另一个可见组件时,默认为 'visible'..
-
menuButton.setBounds(250, 410, 137, 23);Java GUI 可能必须在多个平台、不同的屏幕分辨率和使用不同的 PLAF 上工作。因此,它们不利于组件的精确放置。要为强大的 GUI 组织组件,请改用布局管理器或 combinations of them,以及 white space 的布局填充和边框。 -
这段代码已经是独立的,如果不遗漏关键细节,我不能让它更短。此外,代码的指示部分是由 NetBeans 自动生成的,我不允许修改它。
-
你不需要在组件上调用
setVisible(...)。您的程序中有错误,但我看不出您发布的代码对我们有何帮助。我同意您应该创建并发布一个 MCVE。在您了解 Swing 的基础知识之前,您还应该避免使用 Swing 代码生成器。不,您的代码不是“已经独立的”。在发布这样的废话之前,请阅读 Andrew 的链接。 -
“我不能修改它。” “尾巴在摇狗。”要么学习如何使用你可以支配的强大工具,要么不使用它们。完全可以在 Netbeans 中使用布局。