📄 frame1.java
字号:
package getmyip;import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.net.*;public class Frame1 extends JFrame { JPanel contentPane; FlowLayout flowLayout1 = new FlowLayout(); Button button1 = new Button(); Label label1 = new Label(); TextField textField1 = new TextField(); /**Construct the frame*/ public Frame1() { enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } /**Component initialization*/ private void jbInit() throws Exception { //setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]"))); contentPane = (JPanel) this.getContentPane(); button1.setLabel("获取"); button1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { button1_actionPerformed(e); } }); contentPane.setLayout(flowLayout1); this.setResizable(false); this.setSize(new Dimension(281, 168)); this.setTitle("得到本机IP地址"); label1.setFont(new java.awt.Font("Dialog", 0, 32)); label1.setText("本机的IP地址是:"); textField1.setColumns(15); textField1.setEditable(false); textField1.setFont(new java.awt.Font("Dialog", 1, 20)); contentPane.add(button1, null); contentPane.add(label1, null); contentPane.add(textField1, null); } /**Overridden so we can exit when window is closed*/ protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } void button1_actionPerformed(ActionEvent e) { try{ InetAddress localHost = InetAddress.getLocalHost(); String ipAdd = localHost.getHostAddress(); textField1.setText(ipAdd); }catch(Exception ex){ textField1.setText("Error"); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -