我正在做一个java的程序,我知道如何输出本机的IP:System.out.println("For config:(your own IP^_^: " + InetAddress.getLocalHost().getHostAddress()); 现在我想在interface上显示,不知道该怎么做,求高人帮助

解决方案 »

  1.   

    只要把这个字符串的值,设到你控件上就可以了
    比如你有个JLabel label;
    只要label.setText(InetAddress.getLocalHost().getHostAddress());
      

  2.   

    jLabel3 = new JLabel();
    jLabel3.setText("YOUR IP"InetAddress.getLocalHost().getHostAddress());
    这样不行
      

  3.   

    jLabel3.setText("YOUR IP" + InetAddress.getLocalHost().getHostAddress());要这样
      

  4.   

    要写try catch,这个必须的,否则得话就在方法上写    throws IOException
      

  5.   

    不理解,为什么我单独输入的时候,可以输出,而放在jtextfield或者jlabel里的时候就显示不出来呢
      

  6.   

    我随便写了段代码试了下import java.io.IOException;
    import java.net.InetAddress;
    import javax.swing.JFrame;
    import javax.swing.JLabel;class Myframe extends JFrame {
        public Myframe() throws IOException{
           JLabel la1 = new JLabel("YOUR IP" + InetAddress.getLocalHost().getHostAddress());
        
     
            add(la1);
            setSize(100,100);
            setVisible(true);    }
    }public class csframe {    public static void main(String[] args) throws IOException {        Myframe fMyframe = new Myframe();
        }
    }
      

  7.   

    你单独输入没问题说明你原来那个方法上写了 throws Exception
      

  8.   

    private JPanel getJPanel() {
    if (jPanel == null) {
    jLabel = new JLabel();
    jLabel.setText("SOURCE IP");
    jLabel2 = new JLabel();
    jLabel2.setText("CAR WASH PORT");
    jLabel1 = new JLabel();
    jLabel1.setText("CAR WASH IP");
    jLabel3 = new JLabel();
    jLabel3.setText("YOUR IP"+InetAddress.getLocalHost().getHostAddress());
    GridLayout gridLayout = new GridLayout(1,8);
    gridLayout.setRows(16);
    gridLayout.setColumns(1);
    jPanel = new JPanel();
    jPanel.setLayout(gridLayout);
    jPanel.add(jLabel, null);
    jPanel.add(getJTextField1(), null);
    jPanel.add(jLabel1, null);
    jPanel.add(getJTextField2(), null);
    jPanel.add(jLabel2, null);
    jPanel.add(getJTextField3(), null);

    jPanel.add(jLabel3, null);
    jPanel.add(getJButton1(), null);
    }
    return jPanel;
    }这是部分代码,我在考虑怎么改
      

  9.   

    这样 try {
                jLabel3.setText("YOUR IP"+InetAddress.getLocalHost().getHostAddress());
            } catch (Exception e) {
                // TODO: handle exception
            }
      

  10.   

    放到位置的话主要看你layout的设置和你具体放控件的格式,你用的Gridlayout,那是网格形式摆放
    你开始已经设置了1行,8列,后来为什么又设了一下,只要构造方法里设一次就可以了。
    除非你需要动态修改,不然用不到
    gridLayout.setRows(16);
    gridLayout.setColumns(1);
    如果你要16*1的效果
    直接
    GridLayout gridLayout = new GridLayout(16,1);
    就可以