我目前正在编写一个Java应用程序来显示数据库中的想法列表。每个Idea都有一个标题和一个数字,我希望在列中显示相似的想法。从下图中可以看出,我遇到了一些包装问题。
我想要的是一种以编程方式切断长想法名称的尾部以适应分配空间的方法。所以:
如果列宽仅为
########################
A really long idea titl
会变成
A really long i... #123
public class IdeaBar extends JPanel implements MouseListener {
private SortingColumn column;
private Idea idea;
private JPanel headPanel;
private JPanel bodyPanel;
private boolean isShrunk;
private boolean mouseToggle;
public IdeaBar(Idea id) {
this.idea = id;
this.setDoubleBuffered(true);
setLayout(new MigLayout("insets 0, hidemode 2", "[grow]", "[32px:32px:32px]0[]"));
// Head Panel
headPanel = new JPanel();
headPanel.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
headPanel.addMouseListener(this);
headPanel.setBorder(new BevelBorder(BevelBorder.RAISED, null, null,
null, null));
headPanel.setBackground(Color.GRAY);
headPanel.setBackground(idea.getStatus().getColor());
headPanel.setLayout(new BorderLayout(0, 0));
// Head Panel Labels
JLabel titleLabel = new JLabel("" + idea.getTitle());
titleLabel.setFont(new Font("Dialog", Font.PLAIN, 20));
titleLabel.setForeground(idea.getStatus().getTextColor());
headPanel.add(titleLabel, BorderLayout.WEST);
JLabel numLabel = new JLabel("New label");
numLabel.setHorizontalAlignment(SwingConstants.RIGHT);
numLabel.setForeground(idea.getStatus().getTextColor());
numLabel.setFont(new Font("Tahoma", Font.PLAIN, 20));
numLabel.setText("#" + idea.getNumber());
headPanel.add(numLabel, BorderLayout.EAST);
add(headPanel, "cell 0 0, grow, wrap");
// Body Panel
bodyPanel= new IdeaBarBodyPanel(idea);
this.add(bodyPanel, "grow");
this.shrink();
}
答案 0 :(得分:2)
尝试将titleLabel
添加到CENTER而不是WEST。因此,应调整其大小以填充左侧空间并自动将椭圆添加到文本中。请注意,这可能不适用于每个系统,请参阅此处:Java JLabel/JButton: on some systems I get "..." (an ellipsis) and on some systems I don't. how can I force to disable the ellipsis at all?
答案 1 :(得分:1)
一般代码。看看字符串是否比您可以显示的长。如果是,它会削减无法显示的部分并添加......
int colLength = 20;
public void addEntry(String entry)
{
if(entry.length() > colLength)
{
entry = entry.substring(0,colLength -3) + "...";
}
add(entry)
}
答案 2 :(得分:0)
嗯,最后,我决定画自己的组件。无论如何我想要小心控制背景,所以这对我来说是最好的解决方案。
支持你们两个人,因为你们的答案有助于我找到自己的方式!