嗨,这个问题可能看似重复,但事实并非如此,因为我已经尝试过像这样的其他stackoverflow问题的建议。有人可以帮我调试我的代码,这是我的代码:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.MatteBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
// This is the Panel that contains represents the view of the
// Music Store
public class PosPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
// These are the components
private JLabel billNumber;
private JButton submitButton;
private JButton changeViewButton;
private JButton addItemButton;
private JTable table;
public JButton getsubmitButton() {
return submitButton;
}
public JLabel getBillNumber() {
return billNumber;
}
public JButton getAddItemButton() {
return addItemButton;
}
public JButton getChangeViewButton() {
return changeViewButton;
}
public void setChangeViewButton(JButton changeViewButton) {
this.changeViewButton = changeViewButton;
}
public JTable getTable() {
return table;
}
public PosPanel(){
super();
// Use a GridBagLayout (lotsa fun)
GridBagLayout layout = new GridBagLayout();
GridBagConstraints layoutConstraints = new GridBagConstraints();
setLayout(layout);
billNumber = new JLabel("Invoice #1");
layoutConstraints.gridx = 0;
layoutConstraints.gridy = 0;
layoutConstraints.gridwidth = 1;
layoutConstraints.gridheight = 1;
layoutConstraints.fill = GridBagConstraints.BOTH;
layoutConstraints.insets = new Insets(10, 10, 10, 10);
layoutConstraints.anchor = GridBagConstraints.EAST;
layoutConstraints.weightx = 1.0;
layoutConstraints.weighty = 0.05;
layout.setConstraints(billNumber, layoutConstraints);
add(billNumber);
changeViewButton = new JButton("Exchange/Return");
layoutConstraints.gridx = 1;
layoutConstraints.gridy = 0;
layoutConstraints.gridwidth = 1;
layoutConstraints.gridheight = 1;
layoutConstraints.fill = GridBagConstraints.BOTH;
layoutConstraints.insets = new Insets(0, 0, 0, 0);
layoutConstraints.anchor = GridBagConstraints.EAST;
layoutConstraints.weightx = 1.0;
layoutConstraints.weighty = 0.05;
layout.setConstraints(changeViewButton, layoutConstraints);
add(changeViewButton);
addItemButton = new JButton("Add Item");
layoutConstraints.gridx = 2;
layoutConstraints.gridy = 0;
layoutConstraints.gridwidth = 1;
layoutConstraints.gridheight = 1;
layoutConstraints.fill = GridBagConstraints.BOTH;
layoutConstraints.insets = new Insets(0, 0, 0, 0);
layoutConstraints.anchor = GridBagConstraints.EAST;
layoutConstraints.weightx = 1.0;
layoutConstraints.weighty = 0.05;
layout.setConstraints(addItemButton, layoutConstraints);
add(addItemButton);
submitButton = new JButton("Submit");
layoutConstraints.gridx = 0;
layoutConstraints.gridy = 8;
layoutConstraints.gridwidth = 3;
layoutConstraints.gridheight = 1;
layoutConstraints.fill = GridBagConstraints.BOTH;
layoutConstraints.insets = new Insets(0, 0, 0, 0);
layoutConstraints.anchor = GridBagConstraints.EAST;
layoutConstraints.weightx = 1.0;
layoutConstraints.weighty = 0.05;
layout.setConstraints(submitButton, layoutConstraints);
add(submitButton);
table = new JTable(new MyTableModel());
int[] alignments = new int[] { JLabel.LEFT, JLabel.CENTER, JLabel.RIGHT };
table.getTableHeader().getColumnModel().getColumn(0)
.setHeaderRenderer(new HeaderRenderer(table, alignments[0]));
table.getTableHeader().getColumnModel().getColumn(2)
.setHeaderRenderer(new HeaderRenderer(table, alignments[1]));
table.getTableHeader().setFont(new Font("Times New Roman", Font.BOLD, 28));
table.setFont(new Font("Times New Roman", Font.PLAIN, 20));
table.getColumnModel().getColumn(0).setMinWidth(350);
table.getColumnModel().getColumn(0).setMaxWidth(350);
table.getColumnModel().getColumn(2).setMinWidth(135);
table.getColumnModel().getColumn(2).setMaxWidth(135);
table.getColumnModel().getColumn(3).setMinWidth(125);
table.getColumnModel().getColumn(3).setMaxWidth(125);
table.getColumnModel().getColumn(4).setMinWidth(155);
table.getColumnModel().getColumn(4).setMaxWidth(155);
table.setRowHeight(30);
DefaultTableCellRenderer rightRenderer = new DefaultTableCellRenderer();
rightRenderer.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
table.getColumn("Quantity").setCellRenderer( rightRenderer );
table.getColumn("Price").setCellRenderer( rightRenderer );
table.getColumn("Item Total").setCellRenderer( rightRenderer );
table.setShowGrid(true);
Color color = Color.black;
MatteBorder border = new MatteBorder(1, 1, 0, 0, color);
table.setBorder(border);
table.setGridColor(color);
table.setFillsViewportHeight(true);
layoutConstraints.gridx = 0;
layoutConstraints.gridy = 3;
layoutConstraints.gridwidth = 3;
layoutConstraints.gridheight = 1;
layoutConstraints.fill = GridBagConstraints.BOTH;
layoutConstraints.insets = new Insets(0, 0, 0, 0);
layoutConstraints.anchor = GridBagConstraints.EAST;
layoutConstraints.weightx = 1.0;
layoutConstraints.weighty = 1.0;
layout.setConstraints(table, layoutConstraints);
layoutConstraints.gridx = 0;
layoutConstraints.gridy = 2;
layoutConstraints.gridwidth = 4;
layoutConstraints.gridheight = 1;
layoutConstraints.fill = GridBagConstraints.BOTH;
layoutConstraints.insets = new Insets(0, 0, 0, 0);
layoutConstraints.anchor = GridBagConstraints.EAST;
layoutConstraints.weightx = 0;
layoutConstraints.weighty = 0;
layout.setConstraints(table.getTableHeader(), layoutConstraints);
JScrollPane pane = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
layoutConstraints.gridx = 0;
layoutConstraints.gridy = 3;
layoutConstraints.gridwidth = 3;
layoutConstraints.gridheight = 1;
layoutConstraints.fill = GridBagConstraints.BOTH;
layoutConstraints.insets = new Insets(0, 0, 0, 0);
layoutConstraints.anchor = GridBagConstraints.EAST;
layoutConstraints.weightx = 1.0;
layoutConstraints.weighty = 1.0;
layout.setConstraints(pane, layoutConstraints);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
add(table);
add(table.getTableHeader());
add(pane);
}
public void tableReset() {
table.setModel(new MyTableModel());
int[] alignments = new int[] { JLabel.LEFT, JLabel.CENTER, JLabel.RIGHT };
table.getTableHeader().getColumnModel().getColumn(0)
.setHeaderRenderer(new HeaderRenderer(table, alignments[0]));
table.getTableHeader().getColumnModel().getColumn(2)
.setHeaderRenderer(new HeaderRenderer(table, alignments[1]));
table.getTableHeader().setFont(new Font("Times New Roman", Font.BOLD, 28));
table.setFont(new Font("Times New Roman", Font.PLAIN, 20));
table.getColumnModel().getColumn(0).setMinWidth(350);
table.getColumnModel().getColumn(0).setMaxWidth(350);
table.getColumnModel().getColumn(2).setMinWidth(135);
table.getColumnModel().getColumn(2).setMaxWidth(135);
table.getColumnModel().getColumn(3).setMinWidth(125);
table.getColumnModel().getColumn(3).setMaxWidth(125);
table.getColumnModel().getColumn(4).setMinWidth(155);
table.getColumnModel().getColumn(4).setMaxWidth(155);
table.setRowHeight(30);
DefaultTableCellRenderer rightRenderer = new DefaultTableCellRenderer();
rightRenderer.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
table.getColumn("Quantity").setCellRenderer( rightRenderer );
table.getColumn("Price").setCellRenderer( rightRenderer );
table.getColumn("Item Total").setCellRenderer( rightRenderer );
}
private static class HeaderRenderer implements TableCellRenderer {
DefaultTableCellRenderer renderer;
int horAlignment;
public HeaderRenderer(JTable table, int horizontalAlignment) {
horAlignment = horizontalAlignment;
renderer = (DefaultTableCellRenderer)table.getTableHeader()
.getDefaultRenderer();
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int col) {
Component c = renderer.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row, col);
JLabel label = (JLabel)c;
label.setHorizontalAlignment(horAlignment);
return label;
}
}
}
[更新]根据其他问题提出的建议代码
Vertical scrollbar in jTable swing does not appear
Horizontal Scrollbar is not working with JTable in Java Swing
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.io.*;
public class TableCreate extends JFrame
{
JPanel mainPanel;
TableCreate() throws IOException
{
mainPanel=new JPanel(new BorderLayout());
String InputFile1 = "TableCreate.java";
BufferedReader breader1 = new BufferedReader(new FileReader(InputFile1));
String line1 = "";
line1 = breader1.readLine();
DefaultTableModel model1 = new DefaultTableModel();
model1.addColumn("line");
while((line1=breader1.readLine()) != null)
{
System.out.println(line1);
model1.addRow(new Object[]{line1});
}
breader1.close();
JTable tab=new JTable(model1);
tab.setPreferredScrollableViewportSize(new Dimension(300, 200));
tab.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
TableColumnAdjuster tca = new TableColumnAdjuster(tab);
tca.adjustColumns();
JScrollPane js = new JScrollPane(tab);
add(js);
}
public static void main(String[] args) throws IOException
{
TableCreate tc=new TableCreate();
tc.pack();
tc.setVisible(true);
tc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
答案 0 :(得分:0)
您的代码中存在一些明显的问题。最大的是:
在最后几行中,您将显式地将表添加到基础容器中。您应该更改add(table); // remove this line
add(table.getTableHeader()); // remove this line
add(pane); // keep this line
课程中的以下行:
JTable
通常应将JScrollPane
添加到JTable
的观看端口以显示其标头。如果您将JScrollPane
分别添加到容器中,则标题将不可见。
然后应将JTable
添加到容器中。不要再将{{1}}添加到容器中。它会工作。如果这不能帮助您在此处发布代码以进行调试。
祝你好运。