JLabel的名称中如何换行?

milufeiyang 2002-05-08 05:46:29
比如JLabel label = new JLabel( "This is a test.\n faintfaint" );
我希望标签显示为两行,在\n处分行,请问是否可以达到这种效果?
哪位大虾给点代码?
...全文
3019 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
qianheliu 2010-06-02
  • 打赏
  • 举报
回复
用<html>标签后,jLabel标签中的内容一旦生成就不能灵活改变,有没有更好的办法?
alphazhao 2002-05-09
  • 打赏
  • 举报
回复
很简单的,按照xioyoo(xioyoo)所说,其实你只要那一句就行
JLabel label=new JLabel("<html><body><p>this is a test</p><br><p>faint faint</p><body></html>");
现在我假如你消息体最多显示两行,即进入消息体参数为String msg里面只含有一个"\n",那么进行如下处理:
String strMsg = msg;
int i = strMsg.indexOf("\n");//判断消息体是否含有\n换行
if(i!=-1) {//i=-1表示不含"\n",即消息体无须换行
String msg1 = strMsg.substring(0,i);//第一行
String msg2 = strMsg.substring(i+1);//第二行
strMsg = "<html><body>"+msg1+"<br>"+msg2+"<body></html>";//换行处理
}
……
其它的可以不改动,即
JLabel label = new JLabel(strMsg);//用label放置消息体
无需改变
我试过了,可以完成,且两行之间间隔也合理些
seasun2k 2002-05-09
  • 打赏
  • 举报
回复
费这么大劲干嘛?? 用两个label就好了
milufeiyang 2002-05-09
  • 打赏
  • 举报
回复
我java水平不行,能写也就不需要问了……
肖尧19 2002-05-08
  • 打赏
  • 举报
回复
我觉得思路说清楚了,你怎么那么懒啊,我不写代码。
milufeiyang 2002-05-08
  • 打赏
  • 举报
回复
不能给点代码么?
我希望最好能是在Jlable上改动就可以解决……
肖尧19 2002-05-08
  • 打赏
  • 举报
回复
同样可以解决!
得到消息的长度,得到当前使用的字体的宽度,然后可以计算出消息的总长
得到要显示消息的组件的WIDTH
然后把消息总长度和组件的WIDTH比较
如果消息长大于组件宽就换行(可以做除取模!)
得到分成几行后,再在每行添加<br>,首尾添加<html> ... </html>
在显示到组件上就OK了!
alphazhao 2002-05-08
  • 打赏
  • 举报
回复
他的意思是传入一String消息体
判断\n处的位置,然后决定换行
这方面兄弟不打熟悉……
山卜居士 2002-05-08
  • 打赏
  • 举报
回复
xioyoo(xioyoo) 是对的.

String str = "<html><body>第一行<br>第二行</body></html>";
JLabel label = new JLabel(str);

利用html语法,前后一定要有<html><body>标签,如要换行,可用<br>或<p>标签.
milufeiyang 2002-05-08
  • 打赏
  • 举报
回复
我并不是这个意思,大家看看我的源代码就清楚了,其实我想用Jlable来做个消息框,但是消息太长时,我想换行,不知道该如果处理的说
源代码如下(在strMsg的,号后换行):
import javax.swing.*;
import java.awt.*;
class applit extends Thread {
private String strTitle;//消息显示标题
private String strMsg;//显示消息体
public applit(String title, String msgBody) {
strTitle = title;
strMsg = msgBody;
}
public void run() {
try {//自定义消息框格式
Font f = new Font("Serif", Font.PLAIN, 16);//定义消息体字体格式
JLabel label = new JLabel(strMsg);//用label放置消息体
label.setFont(f);
JOptionPane pane = new JOptionPane(label, JOptionPane.PLAIN_MESSAGE);
JDialog dialog = pane.createDialog(null, strTitle);
dialog.setResizable(false);
dialog.show();
System.exit(0);//退出当前线程
}
catch(Exception e) {
System.out.println("消息框信息显示出错,问题在" + e);
}
}
}
public class MainC {
public static boolean faint = false;
public static void main(String[] args) {
String strMsg = "消息提示行1,\n消息提示行2";
String strTitle = "Message come!";
applit mp = new applit(strTitle,strMsg);
mp.start();
}
}
肖尧19 2002-05-08
  • 打赏
  • 举报
回复
送分题啊,哈哈,准备给分吧。(JLable可以写HTML,而你的\n不行哦)

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class LabelTest
{
public static void main(String[] args)
{
JFrame frame=new JFrame("frame");
Container con=frame.getContentPane();
con.setLayout(new FlowLayout());
frame.setBounds(100,100,300,200);
JLabel label=new JLabel("<html><body><p>this is a test</p><br><p>faint faint</p><body></html>");
con.add(label);
frame.show();
}
}
gdsean 2002-05-08
  • 打赏
  • 举报
回复
JLabel label = new JLabel( "This is a test." +
+ System.getProperty("line.separator") + "faintfaint" );
高仿QQ聊天 登录界面 import java.awt.BorderLayout; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import javax.swing.BorderFactory; import javax.swing.Box; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JScrollPane; import javax.swing.JTextField; import javax.swing.JTextPane; import javax.swing.UIManager; import javax.swing.text.BadLocationException; import javax.swing.text.SimpleAttributeSet; import javax.swing.text.StyleConstants; import javax.swing.text.StyledDocument; public class Test extends JFrame { private JScrollPane scrollPane = null; // 滚动 private JTextPane text = null; private Box box = null; // 放输入组件的容器 private JButton b_insert = null, b_remove = null, b_icon = null; // 插入按钮;清除按钮;插入图片按钮 private JTextField addText = null; // 文字输入框 // 字体名称;字号大小;文字样式;文字颜色;文字背景颜色 private JComboBox fontName = null, fontSize = null, fontStyle = null, fontColor = null, fontBackColor = null; private StyledDocument doc = null; // 非常重要插入文字样式就靠它了 public Test() { super("JTextPane Test"); try { // 使用Windows的界面风格 UIManager.setLookAndFeel ("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); } catch (Exception e) { e.printStackTrace(); } text = new JTextPane(); text.setEditable(false); // 不可录入 doc = text.getStyledDocument(); // 获得JTextPane的Document scrollPane = new JScrollPane(text); addText = new JTextField(18); String[] str_name = { "宋体", "黑体", "Dialog", "Gulim" }; String[] str_Size = { "12", "14", "18", "22", "30", "40" }; String[] str_Style = { "常规", "斜体", "粗体", "粗斜体" }; String[] str_Color = { "黑色", "红色", "蓝色", "黄色", "绿色" }; String[] str_BackColor = { "无色", "灰色", "淡红", "淡蓝", "淡黄", "淡绿" }; fontName = new JComboBox(str_name); // 字体名称 fontSize = new JComboBox(str_Size); // 字号 fontStyle = new JComboBox(str_Style); // 样式 fontColor = new JComboBox(str_Color); // 颜色 fontBackColor = new JComboBox(str_BackColor); // 背景颜色 b_insert = new JButton("插入"); // 插入 b_remove = new JButton("清空"); // 清除 b_icon = new JButton("图片"); // 插入图片 b_insert.addActionListener(new ActionListener() { // 插入文字的事件 public void actionPerformed(ActionEvent e) { insert(getFontAttrib()); addText.setText(""); } }); b_remove.addActionListener(new ActionListener() { // 清除事件 public void actionPerformed(ActionEvent e) { text.setText(""); } }); b_icon.addActionListener(new ActionListener() { // 插入图片事件 public void actionPerformed(ActionEvent arg0) { JFileChooser f = new JFileChooser(); // 查找文件 f.showOpenDialog(null); insertIcon(f.getSelectedFile()); // 插入图片 } }); box = Box.createVerticalBox(); // 竖结构 Box box_1 = Box.createHorizontalBox(); // 横结构 Box box_2 = Box.createHorizontalBox(); // 横结构 box.add(box_1); box.add(Box.createVerticalStrut(8)); // 两行的间距 box.add(box_2); box.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8)); // 8个的边距 // 开始将所需组件加入容器 box_1.add(new JLabel("字体:")); // 加入标签 box_1.add(fontName); // 加入组件 box_1.add(Box.createHorizontalStrut(8)); // 间距 box_1.add(new JLabel("样式:")); box_1.add(fontStyle); box_1.add(Box.createHorizontalStrut(8)); box_1.add(new JLabel("字号:")); box_1.add(fontSize); box_1.add(Box.createHorizontalStrut(8)); box_1.add(new JLabel("颜色:")); box_1.add(fontColor); box_1.add(Box.createHorizontalStrut(8)); box_1.add(new JLabel("背景:")); box_1.add(fontBackColor); box_1.add(Box.createHorizontalStrut(8)); box_1.add(b_icon); box_2.add(addText); box_2.add(Box.createHorizontalStrut(8)); box_2.add(b_insert); box_2.add(Box.createHorizontalStrut(8)); box_2.add(b_remove); this.getRootPane().setDefaultButton(b_insert); // 默认回车按钮 this.getContentPane().add(scrollPane); this.getContentPane().add(box, BorderLayout.SOUTH); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(600, 400); this.setLocationRelativeTo(null); this.setVisible(true); addText.requestFocus(); } /** * 插入图片 * * @param icon */ private void insertIcon(File file) { text.setCaretPosition(doc.getLength()); // 设置插入位置 text.insertIcon(new ImageIcon(file.getPath())); // 插入图片 insert(new FontAttrib()); // 这样做可以换行 } /** * 将文本插入JTextPane * * @param attrib */ private void insert(FontAttrib attrib) { try { // 插入文本 doc.insertString(doc.getLength(), attrib.getText() + "\n", attrib.getAttrSet()); } catch (BadLocationException e) { e.printStackTrace(); } } /** * 获取所需要的文字设置 * * @return FontAttrib */ private FontAttrib getFontAttrib() { FontAttrib att = new FontAttrib(); att.setText(addText.getText()); att.setName((String) fontName.getSelectedItem()); att.setSize(Integer.parseInt((String) fontSize.getSelectedItem())); String temp_style = (String) fontStyle.getSelectedItem(); if (temp_style.equals("常规")) { att.setStyle(FontAttrib.GENERAL); } else if (temp_style.equals("粗体")) { att.setStyle(FontAttrib.BOLD); } else if (temp_style.equals("斜体")) { att.setStyle(FontAttrib.ITALIC); } else if (temp_style.equals("粗斜体")) { att.setStyle(FontAttrib.BOLD_ITALIC); } String temp_color = (String) fontColor.getSelectedItem(); if (temp_color.equals("黑色")) { att.setColor(new Color(0, 0, 0)); } else if (temp_color.equals("红色")) { att.setColor(new Color(255, 0, 0)); } else if (temp_color.equals("蓝色")) { att.setColor(new Color(0, 0, 255)); } else if (temp_color.equals("黄色")) { att.setColor(new Color(255, 255, 0)); } else if (temp_color.equals("绿色")) { att.setColor(new Color(0, 255, 0)); } String temp_backColor = (String) fontBackColor.getSelectedItem(); if (!temp_backColor.equals("无色")) { if (temp_backColor.equals("灰色")) { att.setBackColor(new Color(200, 200, 200)); } else if (temp_backColor.equals("淡红")) { att.setBackColor(new Color(255, 200, 200)); } else if (temp_backColor.equals("淡蓝")) { att.setBackColor(new Color(200, 200, 255)); } else if (temp_backColor.equals("淡黄")) { att.setBackColor(new Color(255, 255, 200)); } else if (temp_backColor.equals("淡绿")) { att.setBackColor(new Color(200, 255, 200)); } } return att; } public static void main(String args[]) { new Test(); } /** * 字体的属性类 */ private class FontAttrib { public static final int GENERAL = 0; // 常规 public static final int BOLD = 1; // 粗体 public static final int ITALIC = 2; // 斜体 public static final int BOLD_ITALIC = 3; // 粗斜体 private SimpleAttributeSet attrSet = null; // 属性集 private String text = null, name = null; // 要输入的文本和字体名称 private int style = 0, size = 0; // 样式和字号 private Color color = null, backColor = null; // 文字颜色和背景颜色 /** * 一个空的构造(可当做换行使用) */ public FontAttrib() { } /** * 返回属性集 * * @return */ public SimpleAttributeSet getAttrSet() { attrSet = new SimpleAttributeSet(); if (name != null) StyleConstants.setFontFamily(attrSet, name); if (style == FontAttrib.GENERAL) { StyleConstants.setBold(attrSet, false); StyleConstants.setItalic(attrSet, false); } else if (style == FontAttrib.BOLD) { StyleConstants.setBold(attrSet, true); StyleConstants.setItalic(attrSet, false); } else if (style == FontAttrib.ITALIC) { StyleConstants.setBold(attrSet, false); StyleConstants.setItalic(attrSet, true); } else if (style == FontAttrib.BOLD_ITALIC) { StyleConstants.setBold(attrSet, true); StyleConstants.setItalic(attrSet, true); } StyleConstants.setFontSize(attrSet, size); if (color != null) StyleConstants.setForeground(attrSet, color); if (backColor != null) StyleConstants.setBackground(attrSet, backColor); return attrSet; } /** * 设置属性集 * * @param attrSet */ public void setAttrSet(SimpleAttributeSet attrSet) { this.attrSet = attrSet; } /* 后面的注释就不写了,一看就明白 */ public String getText() { return text; } public void setText(String text) { this.text = text; } public Color getColor() { return color; } public void setColor(Color color) { this.color = color; } public Color getBackColor() { return backColor; } public void setBackColor(Color backColor) { this.backColor = backColor; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getSize() { return size; } public void setSize(int size) { this.size = size; } public int getStyle() { return style; } public void setStyle(int style) { this.style = style; } } }

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧