Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中的GUI_Java_Swing_Jframe_Client - Fatal编程技术网

java中的GUI

java中的GUI,java,swing,jframe,client,Java,Swing,Jframe,Client,我有一段在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.setLay

我有一段在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控制台中获取输出,但我想将其放入框架中,我应该怎么做?

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

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

声明一个
JLabel
并调用
setText
,而不是
System.out.println

 label.setText(y);


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

 label.setText(y);

您可以使用
setText(“youString”)
。。尝试学习
swing
基础知识

您可以使用
setText(“youString”)
。。尝试学习
swing
基础知识

试试这个

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) {
                    }
                }
            }
        });
    }
}
试试这个

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) {
                    }
                }
            }
        });
    }
}

我知道,但我不能将这行System.out.println(y)传递给字符串来执行此操作。label.setText(string.valueOf(y)),请将服务器/客户端放在单独的线程中,否则gui线程将被阻塞。我知道,但我不能将这行System.out.println(y)传递给字符串来执行此操作。label.setText(string.valueOf(y))请将您的服务器/客户端放在一个单独的线程中,否则您的gui线程将被阻塞。