图像
在我附加的图片(链接)中,应该在向前,向后和删除带有JTable标头的按钮下显示JTable:x/y/y/width/height
但是,不是。谁能帮帮我吗?我在这里做错了什么?
import java.awt.BorderLayout;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
public class Whiteboard extends JFrame {
public static void main(String[] args) {
new Whiteboard();
}
private static final long serialVersionUID = 1L;
private String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" };
public Whiteboard() {
JButton setColor = new JButton("Color");
JPanel colorPanel = new JPanel();
colorPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
colorPanel.setLayout(new BoxLayout(colorPanel, BoxLayout.X_AXIS));
colorPanel.add(setColor);
JButton addRect = new JButton("Rect");
JButton addOval = new JButton("Oval");
JButton addLine = new JButton("Line");
JButton addText = new JButton("Text");
JPanel addPanel = new JPanel();
addPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
addPanel.setLayout(new BoxLayout(addPanel, BoxLayout.X_AXIS));
addPanel.add(addRect);
addPanel.add(addOval);
addPanel.add(addLine);
addPanel.add(addText);
JTextField setText = new JTextField("");
JComboBox<String> changeFont = new JComboBox<String>(petStrings);
JPanel textPanel = new JPanel();
textPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
textPanel.setLayout(new BoxLayout(textPanel, BoxLayout.X_AXIS));
textPanel.add(setText);
textPanel.add(changeFont);
JButton sendForward = new JButton("Forward");
JButton sendBackward = new JButton("Backward");
JButton removeObj = new JButton("Remove");
JPanel orderPanel = new JPanel();
orderPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
orderPanel.setLayout(new BoxLayout(orderPanel, BoxLayout.X_AXIS));
orderPanel.add(sendForward);
orderPanel.add(sendBackward);
orderPanel.add(removeObj);
DefaultTableModel tableModel = new DefaultTableModel(new String[] { "X", "Y", "Width", "Height" }, 0);
JTable infoTable = new JTable(tableModel);
JScrollPane tablePanel = new JScrollPane();
infoTable.setPreferredScrollableViewportSize(infoTable.getPreferredSize());
infoTable.setFillsViewportHeight(true);
tablePanel.add(infoTable);
tableModel.addRow(new String[] { "TEST1", "TEST2", "TEST3", "TEST4" });
JPanel control = new JPanel();
control.setLayout(new BoxLayout(control, BoxLayout.Y_AXIS));
control.add(colorPanel);
control.add(addPanel);
control.add(textPanel);
control.add(orderPanel);
control.add(tablePanel);
Canvas canvas = new Canvas();
canvas.setLayout(new BoxLayout(canvas, BoxLayout.PAGE_AXIS));
JPanel window = new JPanel();
window.add(control);
window.add(canvas);
getContentPane().add(window);
pack();
setLocationRelativeTo(null);
setTitle("Whiteboard");
setLayout(new BorderLayout());
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
tablePanel.add(infoTable);
不要将组件直接添加到JScrollPane
。该组件应添加到滚动面板的JViewport
中。
您可以使用:
JScrollPane tablePanel = new JScrollPane(infoTable); // easiest way,
或
tablePanel.getViewport().setViewportView( infoTable );
另外,不要使用Canvas
类。那是一个AWT组件。您应该使用JPanel
。