0

我有这段代码可以在eclipse上正常工作。下面提到了我的查询

class clienttime_gui
{
    public static void main(String args[]) throws Exception
    {   
        JFrame frame=new JFrame("Add IP");
        JTextField textbox;
        JButton button;
        JLabel label;
        frame.setLayout(null);
        textbox = new JTextField();
        textbox.setBounds(100,20,150,20); 
        label = new JLabel("Add IP");
        label.setBounds(50, 20, 100, 20);
        button=new JButton("Submit");
        button.setBounds(250,20,100,20);
        frame.add(textbox);
        frame.add(label);
        frame.add(button);
        final String x=textbox.getText();
       frame.setSize(400,100);
        frame.setVisible(true);  
        button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 1) {
                    try{

                    InetAddress locIP = InetAddress.getByName(x);
                    //InetAddress locIP = InetAddress.getByName("14.139.60.104");
                    Socket soc= new Socket(locIP,7681);
                    BufferedReader in=new BufferedReader(new InputStreamReader(soc.getInputStream()));
                    String y= null;
                    while((y = in.readLine()) != null){
                    System.out.println(y);
                    }
                    }catch(Exception e1){}
                }
            }
        });       
   }
}

这是我的用户界面 在此处输入图像描述

现在我在 Eclipse 控制台中得到输出,但我想把它放到框架中,我应该怎么做?

4

4 回答 4

1

添加一个开头没有文本的 JLabel 并在每次提交新 ip 时对其进行编辑。

于 2013-11-12T11:03:30.507 回答
1

声明 aJLabel并调用setText,而不是System.out.println

 label.setText(y);

class clienttime_gui
{
static JLabel output=new JLabel();// Declare JLabel here.

public static void main(String args[]) throws Exception
{   
    JFrame frame=new JFrame("Add IP");
    JTextField textbox;
    JButton button;
    JLabel label;

    frame.setLayout(null);
    textbox = new JTextField();
    textbox.setBounds(100,20,150,20); 
    label = new JLabel("Add IP");
    label.setBounds(50, 20, 100, 20);
    button=new JButton("Submit");
    button.setBounds(250,20,100,20);
    frame.add(textbox);
    frame.add(label);
    frame.add(output);
    frame.add(button);
    final String x=textbox.getText();
   frame.setSize(400,100);
    frame.setVisible(true);  
    button.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 1) {
                try{

                InetAddress locIP = InetAddress.getByName(x);
                //InetAddress locIP = 
                         InetAddress.getByName("14.139.60.104");
                Socket soc= new Socket(locIP,7681);
                BufferedReader in=new BufferedReader(
                   new InputStreamReader(soc.getInputStream()));
                String y= null;
                while((y = in.readLine()) != null){
               //         System.out.println(y);
                  output.setText(y);
                }
                }catch(Exception e1){}
            }
        }
    });       
 }
 }
于 2013-11-12T11:03:36.890 回答
1

您可以使用setText("youString").. 尝试学习swing基础知识。在这里你可以找到一个教程。

于 2013-11-12T11:06:36.777 回答
1

试试这个

class clienttime_gui {

    public static void main(String args[]) throws Exception {
        JFrame frame = new JFrame("Add IP");
        JTextField textbox;
        JButton button;
        JLabel label;
        final List list = new List();
        frame.setLayout(null);
        textbox = new JTextField();
        textbox.setBounds(100, 20, 150, 20);
        label = new JLabel("Add IP");
        label.setBounds(50, 20, 100, 20);
        button = new JButton("Submit");
        button.setBounds(250, 20, 100, 20);
        list.setBounds(50, 50, 350, 200);
        frame.add(textbox);
        frame.add(label);
        frame.add(button);
        final String x = textbox.getText();
        frame.setSize(400, 100);
        frame.setVisible(true);
        button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 1) {
                    try {

                        InetAddress locIP = InetAddress.getByName(x);
                        //InetAddress locIP = InetAddress.getByName("14.139.60.104");
                        Socket soc = new Socket(locIP, 7681);
                        BufferedReader in = new BufferedReader(new InputStreamReader(soc.getInputStream()));
                        String y = null;
                        while ((y = in.readLine()) != null) {
                            list.add(y);
                        }
                    } catch (Exception e1) {
                    }
                }
            }
        });
    }
}
于 2013-11-12T11:12:04.267 回答