如何在java的界面上显示我的本机IP 我正在做一个java的程序,我知道如何输出本机的IP:System.out.println("For config:(your own IP^_^: " + InetAddress.getLocalHost().getHostAddress()); 现在我想在interface上显示,不知道该怎么做,求高人帮助 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 只要把这个字符串的值,设到你控件上就可以了比如你有个JLabel label;只要label.setText(InetAddress.getLocalHost().getHostAddress()); jLabel3 = new JLabel(); jLabel3.setText("YOUR IP"InetAddress.getLocalHost().getHostAddress());这样不行 jLabel3.setText("YOUR IP" + InetAddress.getLocalHost().getHostAddress());要这样 要写try catch,这个必须的,否则得话就在方法上写 throws IOException 不理解,为什么我单独输入的时候,可以输出,而放在jtextfield或者jlabel里的时候就显示不出来呢 我随便写了段代码试了下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(); }} 你单独输入没问题说明你原来那个方法上写了 throws Exception 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; }这是部分代码,我在考虑怎么改 这样 try { jLabel3.setText("YOUR IP"+InetAddress.getLocalHost().getHostAddress()); } catch (Exception e) { // TODO: handle exception } 放到位置的话主要看你layout的设置和你具体放控件的格式,你用的Gridlayout,那是网格形式摆放你开始已经设置了1行,8列,后来为什么又设了一下,只要构造方法里设一次就可以了。除非你需要动态修改,不然用不到gridLayout.setRows(16);gridLayout.setColumns(1);如果你要16*1的效果直接GridLayout gridLayout = new GridLayout(16,1);就可以 conn = DriverManager.getConnection 新手求助!! window xp下的tomcat免安装版,下载zip还是32-bit Windows zip struts2.0中tiles的问题 blog系统 在线等待:一个关于在线聊天的问题,急! 转换????????????? 关于hibernate的一点疑问 有个问题真是郁闷!--关于java类的编译问题 求助 [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序 怎么办? java开发多年,不会SSH,求快速学习SSH办法、教程啥的!!多谢!! SSH问题,急
比如你有个JLabel label;
只要label.setText(InetAddress.getLocalHost().getHostAddress());
jLabel3.setText("YOUR IP"InetAddress.getLocalHost().getHostAddress());
这样不行
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();
}
}
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;
}这是部分代码,我在考虑怎么改
jLabel3.setText("YOUR IP"+InetAddress.getLocalHost().getHostAddress());
} catch (Exception e) {
// TODO: handle exception
}
你开始已经设置了1行,8列,后来为什么又设了一下,只要构造方法里设一次就可以了。
除非你需要动态修改,不然用不到
gridLayout.setRows(16);
gridLayout.setColumns(1);
如果你要16*1的效果
直接
GridLayout gridLayout = new GridLayout(16,1);
就可以