簡體   English   中英

如何從main函數int java將文本追加到JTextArea?

[英]How to append text to JTextArea from main function int java?

我想從main函數向JTextArea附加一些文本,但它不起作用。

我從init()main()附加文本,但是只有來自init()文本出現在JTextArea

public class Test extends JApplet{

    private static JPanel panel = new JPanel();
    private static JTextArea textArea = new JTextArea();

    public void init() {   

        panel.setLayout(null); 
        panel.setPreferredSize(new Dimension(400,300)); 
        this.add(panel);

        textArea.setBounds(20, 150, 350, 100);
        panel.add(textArea);

        setTextArea("BBBB");
    }

    public static void setTextArea(String text){
        textArea.append(text);
    }
    public static void main(String args[]) {        
        setTextArea("AAAAA");
    }   

}

我正在用“BBBB”獲得textarea。

UPDATE

我還有一個功能。 我從init()調用它,文本正在追加,一切都很好。 但是如果我把一行setTextArea("some text"); 后行clientSocket = new Socket(address, port); ,文字不會附加。

 private static void connetToServer() throws IOException, InterruptedException {
        try {
            //address = args.length > 0 ? args[0] : "localhost";
            //port = args.length > 1 ? Integer.parseInt(args[1]) : 4444;
            //System.out.println(address + ' ' + port);
            setTextArea("some text");
            clientSocket = new Socket(address, port);
            output = new PrintStream(clientSocket.getOutputStream());
            input = new DataInputStream(clientSocket.getInputStream());
            inputLine = new DataInputStream(new BufferedInputStream(System.in));
        } 
        catch( IOException e){
            setTextArea("Can't connet to server");
            System.exit(0);
        }
     }

您將“BBBB”附加到文本區域,因為init方法用作appletsservlets的入口點。

您的類extends JApplet ,它是java.applet.Applet的子類,這意味着它將使用init而不是main (它用作應用程序的入口點)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM