我很难调整JTable的大小,我尝试将左列设置为设置宽度,而右列填充表格的其余部分,左列宽度将改变,但它扩展它的容器(并因此切断一些数据)图片如下:
http://gyazo.com/6e928b55f699622470e05fe06f2d9a23(图片类型不支持??)
正如您所看到的,“我们”被切断了User
这个词。
这是我试图调整大小的内容,所有这些都没有效果。
setBounds(any, any, any, any) -- No effect
setMaximumSize(new Dimension(any, any)); --- No effect
然后我在小组中尝试了一些廉价黑客,它们是以下的孩子:
childPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
int index = Utils.getComponentIndex((childPanel.add(friendsList)));
childPanel.getComponent(index).setBounds(any, any, any, any);
也没有效果。
最困扰我的是setBounds()无效,因为我正在使用它(null布局,第一个jframe应用程序,我喜欢能够使用像素位置来处理所有事情)。
以下是代码:( 扩展JPanel )
public SocialPanel() {
this.setLayout(null);
this.setBounds(650, 0, 150, 400);
this.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
friendsList = new FriendsList();
ignoreList = new IgnoreList();
JLabel title = new JLabel("Social Pane");
title.setBounds(45, 3, 75, 25);
childPanel = new JPanel();
childPanel.setBounds(0, 30, 150, 325);
childPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
childPanel.add(friendsList);
JButton showFriends = new JButton("F");
showFriends.setBounds(15, 360, 50, 30);
JButton showIgnore = new JButton("I");
showIgnore.setBounds(80, 360, 50, 30);
this.add(title);
this.add(childPanel);
this.add(showFriends);
this.add(showIgnore);
}
然后当然,friendslist.java(扩展JTable )
public FriendsList() {
this.setGridColor(Color.gray);
this.setShowGrid(true);
this.setDefaultRenderer(Object.class, new SocialCellRenderer());
this.setMaximumSize(new Dimension(1, 1));
DefaultTableModel dtm = (DefaultTableModel)getModel();
dtm.addColumn("Username");
dtm.addColumn("Status");
getColumn("Username").setMinWidth(115);
getColumn("Username").setMaxWidth(115);
setRowHeight(20);
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if(SwingUtilities.isRightMouseButton(e)) {
JTable source = (JTable)e.getSource();
int row = source.rowAtPoint(e.getPoint());
int column = source.columnAtPoint(e.getPoint());
if(!source.isRowSelected(row))
source.changeSelection(row, column, false, false);
buildPopup(row, e.getX(), e.getY());
}
}
});
}
知道如何更改好友列表的大小? (扩展JTable)
注意:我已经从好友列表中删除了所有“大小调整”代码,因为我无法弄清楚并希望提供一个“干净”的解决方案,即使没有设置界限,它仍然pouplates大小相同。这就像尝试手动设置大小完全没有意义一样。
答案 0 :(得分:2)
这是如何使用布局管理器生成类似布局的基本示例。此示例使用BorderLayout
,FlowLayout
和GridBagLayout
它还显示了如何生成固定宽度列;)
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
public class TestLayout {
public static void main(String[] args) {
new TestLayout();
}
public TestLayout() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JPanel content = new JPanel() {
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
};
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(content);
frame.add(new FriendsPanel(), BorderLayout.EAST);
frame.add(new MessagePane(), BorderLayout.SOUTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class FriendsPanel extends JPanel {
public FriendsPanel() {
setLayout(new BorderLayout());
JLabel header = new JLabel("Social Pane");
header.setBorder(new CompoundBorder(new EtchedBorder(), new EmptyBorder(8, 8, 8, 8)));
add(header, BorderLayout.NORTH);
DefaultTableModel model = new DefaultTableModel();
model.addColumn("");
model.addColumn("");
model.addRow(new Object[]{"er3107", "Offline"});
model.addRow(new Object[]{"er4360", "Online"});
model.addRow(new Object[]{"er187", "Offline"});
model.addRow(new Object[]{"er1040", "Online"});
model.addRow(new Object[]{"er427", "Online"});
model.addRow(new Object[]{"er4140", "Online"});
model.addRow(new Object[]{"er835", "Offline"});
model.addRow(new Object[]{"er2045", "Online"});
model.addRow(new Object[]{"er4525", "Online"});
model.addRow(new Object[]{"er4864", "Offline"});
JTable table = new JTable(model);
table.setPreferredScrollableViewportSize(new Dimension(100, 200));
table.setFillsViewportHeight(true);
Font font = table.getFont();
FontMetrics fm = table.getFontMetrics(font);
TableColumnModel cm = table.getColumnModel();
TableColumn column = cm.getColumn(0);
int width = fm.stringWidth("M") * 8;
column.setWidth(width);
column.setMaxWidth(width);
column.setMinWidth(width);
column.setPreferredWidth(width);
column = cm.getColumn(1);
column.setPreferredWidth(width);
add(new JScrollPane(table));
JButton btnF = new JButton("F");
JButton btnI = new JButton("I");
JPanel buttons = new JPanel();
buttons.setBorder(new CompoundBorder(new EtchedBorder(), new EmptyBorder(8, 8, 8, 8)));
buttons.add(btnF);
buttons.add(btnI);
add(buttons, BorderLayout.SOUTH);
}
}
public class MessagePane extends JPanel {
public MessagePane() {
setLayout(new BorderLayout());
DefaultTableModel model = new DefaultTableModel();
model.addColumn("");
model.addColumn("");
model.addColumn("");
model.addRow(new Object[]{"rank", "user4916", "..."});
model.addRow(new Object[]{"rank", "user2916", "..."});
model.addRow(new Object[]{"rank", "user4471", "..."});
model.addRow(new Object[]{"rank", "user4161", "..."});
model.addRow(new Object[]{"rank", "user2048", "..."});
model.addRow(new Object[]{"rank", "user3212", "..."});
model.addRow(new Object[]{"Admin", "Chris", "Testing..."});
JTable table = new JTable(model);
table.setFillsViewportHeight(true);
Font font = table.getFont();
FontMetrics fm = table.getFontMetrics(font);
table.setPreferredScrollableViewportSize(new Dimension(200, fm.getHeight() * 9));
TableColumnModel cm = table.getColumnModel();
TableColumn column = cm.getColumn(0);
int width = fm.stringWidth("M") * 6;
column.setWidth(width);
column.setMaxWidth(width);
column.setMinWidth(width);
column.setPreferredWidth(width);
width = fm.stringWidth("M") * 10;
column = cm.getColumn(1);
column.setWidth(width);
column.setMaxWidth(width);
column.setMinWidth(width);
column.setPreferredWidth(width);
add(new JScrollPane(table));
JPanel buttons = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
buttons.add(new JTextField(5), gbc);
gbc.gridx++;
gbc.weightx = 0;
buttons.add(new JButton("Send Chat"), gbc);
add(buttons, BorderLayout.SOUTH);
}
}
}