JPanels 中的 JLabels 大小问题
我目前正在编写一个 Java 应用程序来显示数据库中的想法列表。每个想法都有一个标题和一个数字,我希望将其与类似的想法一起显示在列中。正如您从下图中看到的,我遇到了一些包装问题。
我想要的是一种以编程方式切断长想法名称尾部的方法以适合分配的空间。所以:
如果列宽仅为
##########################
A real long idea titl
将成为
A very 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();
}
I am currently writing a Java application to display a list of ideas from a database. Each Idea has a title and a number which i would like to display in a column along with similar ideas. As you can see from the picture below, i am having a bit of a packaging issue.
What i would like is a way to programatically cut off the tail of a long idea name to fit in the allotted space. So:
if the column width is only
########################
A really long idea titl
would become
A really long i... #123
Code
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试将
titleLabel
添加到 CENTER 而不是 WEST。因此,应调整其大小以填充剩余空间并自动将省略号添加到文本中。请注意,这可能不适用于每个系统,请参见此处:Java JLabel/JButton:在某些系统上我得到“...”(省略号),而在某些系统上我没有。我怎样才能强制禁用省略号?Try and add the
titleLabel
to CENTER instead of WEST. Thus it should be resized to fill the space left and automatically add the ellipis to the text. Note that this might not work on every system, see here: 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?通用代码。查看字符串是否比您可以显示的长度长。如果是,它会删除无法显示的部分并添加...
General code. Sees if the string is longer than what you can display. If it is, it cuts of the part that cant be displayed and adds the ...
好吧,最后,我决定绘制自己的组件。无论如何,我想仔细控制背景,所以这对我来说是最好的解决方案。
给你们俩投票,因为你们的回答对帮助我找到方向很有帮助!
Well, in the end, i just decided to paint my own component. I want careful control of the background anyway, so this was the best solution for me.
Upvotes to both of you because your answers were instrumental in helping me find my way!