比如JLabel label = new JLabel( "This is a test.\n faintfaint" );
我希望标签显示为两行,在\n处分行,请问是否可以达到这种效果?
哪位大虾给点代码?

解决方案 »

  1.   

    JLabel label = new JLabel( "This is a test." + 
    + System.getProperty("line.separator") + "faintfaint" );
      

  2.   

    送分题啊,哈哈,准备给分吧。(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();
    }
    }
      

  3.   

    我并不是这个意思,大家看看我的源代码就清楚了,其实我想用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();
      }
    }
      

  4.   

    xioyoo(xioyoo) 是对的.String str = "<html><body>第一行<br>第二行</body></html>";
    JLabel label = new JLabel(str);利用html语法,前后一定要有<html><body>标签,如要换行,可用<br>或<p>标签.
      

  5.   

    他的意思是传入一String消息体
    判断\n处的位置,然后决定换行
    这方面兄弟不打熟悉……
      

  6.   

    同样可以解决!
    得到消息的长度,得到当前使用的字体的宽度,然后可以计算出消息的总长
    得到要显示消息的组件的WIDTH
    然后把消息总长度和组件的WIDTH比较
    如果消息长大于组件宽就换行(可以做除取模!)
    得到分成几行后,再在每行添加<br>,首尾添加<html> ... </html>
    在显示到组件上就OK了!
      

  7.   

    不能给点代码么?
    我希望最好能是在Jlable上改动就可以解决……
      

  8.   

    我java水平不行,能写也就不需要问了……
      

  9.   

    费这么大劲干嘛?? 用两个label就好了
      

  10.   

    很简单的,按照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放置消息体
    无需改变
    我试过了,可以完成,且两行之间间隔也合理些
      

  11.   

    用<html>标签后,jLabel标签中的内容一旦生成就不能灵活改变,有没有更好的办法?