JLabel的名称中如何换行? 比如JLabel label = new JLabel( "This is a test.\n faintfaint" );我希望标签显示为两行,在\n处分行,请问是否可以达到这种效果?哪位大虾给点代码? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 JLabel label = new JLabel( "This is a test." + + System.getProperty("line.separator") + "faintfaint" ); 送分题啊,哈哈,准备给分吧。(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(); }} 我并不是这个意思,大家看看我的源代码就清楚了,其实我想用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(); }} xioyoo(xioyoo) 是对的.String str = "<html><body>第一行<br>第二行</body></html>";JLabel label = new JLabel(str);利用html语法,前后一定要有<html><body>标签,如要换行,可用<br>或<p>标签. 他的意思是传入一String消息体判断\n处的位置,然后决定换行这方面兄弟不打熟悉…… 同样可以解决!得到消息的长度,得到当前使用的字体的宽度,然后可以计算出消息的总长得到要显示消息的组件的WIDTH然后把消息总长度和组件的WIDTH比较如果消息长大于组件宽就换行(可以做除取模!)得到分成几行后,再在每行添加<br>,首尾添加<html> ... </html>在显示到组件上就OK了! 不能给点代码么?我希望最好能是在Jlable上改动就可以解决…… 我java水平不行,能写也就不需要问了…… 费这么大劲干嘛?? 用两个label就好了 很简单的,按照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放置消息体无需改变我试过了,可以完成,且两行之间间隔也合理些 用<html>标签后,jLabel标签中的内容一旦生成就不能灵活改变,有没有更好的办法? 怎样用代码GETwap网站 DOM4J 怎么从当前的NODE获得属性? 获得文件大小的最后修改时间 java项目缺少插件 想不明白,关于事件处理! 还有一个关于Dialog的奇怪问题。。。 求教高手:什么数据属于BCD.Decimal类型?? 这个java程序运行后结果怎么不正确啊? 100分相送: 请问考过SCJP的大虾,SCJP考试的题目是中文还是英文,证书的有效期是多长时间? 请教:java中的point 怎样在java中打印,快进来救救我
+ System.getProperty("line.separator") + "faintfaint" );
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();
}
}
源代码如下(在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();
}
}
JLabel label = new JLabel(str);利用html语法,前后一定要有<html><body>标签,如要换行,可用<br>或<p>标签.
判断\n处的位置,然后决定换行
这方面兄弟不打熟悉……
得到消息的长度,得到当前使用的字体的宽度,然后可以计算出消息的总长
得到要显示消息的组件的WIDTH
然后把消息总长度和组件的WIDTH比较
如果消息长大于组件宽就换行(可以做除取模!)
得到分成几行后,再在每行添加<br>,首尾添加<html> ... </html>
在显示到组件上就OK了!
我希望最好能是在Jlable上改动就可以解决……
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放置消息体
无需改变
我试过了,可以完成,且两行之间间隔也合理些