我怎样才能从追加不包含JTextArea中的方法的的JTextArea? [英] How can I append a JTextArea from a method that doesn't contain the JTextArea?

查看:469
本文介绍了我怎样才能从追加不包含JTextArea中的方法的的JTextArea?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一些测试code练习OOP,我想从writeToArea到JTextArea中定义和初始化初始化的方法追加的JTextArea。我已经尝试过直接调用输出变量,但是这将返回输出不能得到解决的错误。我想,这样每当我调用主类的writeToArea的方法,我就可以行添加到输出的JTextArea中的初始化的方法。

I'm making some test code to practice OOP, and I want to append a JTextArea from the "writeToArea" to the "initialize" method where the JTextArea is defined and initialized. I already tried to directly call the "output" variable, but this returns an "output cannot be resolved" error. I want so that whenever I call the "writeToArea" method in the main class, I'll be able to add lines to the "output" JTextArea in the "initialize" method.

下面是主类:

public class Pangea {

    public static void main(String[] args) {

        UI.initialize();
        UI.writeToArea();
    }
}

下面是初始化类:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class UI {

    static void initialize() {
        System.out.println("Initializing GUI.");
        JFrame frame = new JFrame();
        Font myFont = new Font("Courier", Font.BOLD, 14);
        JTextField input = new JTextField("");
        JTextArea output = new JTextArea("Initiated Succesfully.");
        output.setWrapStyleWord(true);
        output.setLineWrap(true);
        input.setFont(myFont);
        output.setFont(myFont);
        input.setForeground(Color.WHITE);
        output.setForeground(Color.WHITE);
        input.setBackground(Color.BLACK);
        input.setCaretColor(Color.WHITE);
        output.setBackground(Color.BLACK);
        output.setEditable(false);
        JScrollPane jp = new JScrollPane(output);
        frame.setTitle("PANGEA RPG [0.01 ALPHA][WIP]");
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(input, BorderLayout.SOUTH);
        frame.add(jp, BorderLayout.CENTER);
        frame.pack();
        frame.setSize(800, 500);
        frame.setVisible(true);
        System.out.println("GUI Initialized.");
    }

    static void writeToArea() {
        System.out.println("\"writeToArea\" running.");
        output.append("Hello!");
        System.out.println("\"writeToArea\" finished.");
    }
}

我试图做类似这样:从另一个类更新的JTextArea 但它没有工作。如果任何人有任何建议,我会非常感激。

I've tried to do something similar to this: Updating jtextarea from another class but it didn't work. If anyone has any suggestions I'd be very thankful.

推荐答案

在您的code的主要错误是缺乏面向对象的设计。使所有静态是设计不良。
同时Swing是基于事件的,所以,当一个事件发生,你应该文本追加到textarea的。见我为你写的例子。

The main error in your code is the lack of OOP design. Making all static is poor design. Also swing is event based, so you should append text to the textArea when an event happens. See the example i write for you.

public class UI {

   private JPanel panel;
   private JTextArea output;

    public UI(){
       initialize();
    }


    private void initialize() {
        panel = new JPanel();
        Font myFont = new Font("Courier", Font.BOLD, 14);
        final JTextField input = new JTextField(""); // must be declared final cause you use it in anonymous class, you can make it instance variable if you want to as textArea

        //add an actionListener then when you press enter this will write to textArea
        input.addActionListener(new ActionListener(){
              @Override             
              public void actionPerformed(ActionEvent evt){
                     writeToArea(input.getText());
              }

        });


        output = new JTextArea("Initiated Succesfully",50,100);// let the component determinate its preferred size.
        output.setWrapStyleWord(true);
        output.setLineWrap(true);
        input.setFont(myFont);
        output.setFont(myFont);
        input.setForeground(Color.WHITE);
        output.setForeground(Color.WHITE);
        input.setBackground(Color.BLACK);
        input.setCaretColor(Color.WHITE);
        output.setBackground(Color.BLACK);
        output.setEditable(false);
        JScrollPane jp = new JScrollPane(output);
        panel.setLayout(new BorderLayout());
        panel.add(input, BorderLayout.SOUTH);
        panel.add(jp, BorderLayout.CENTER);

    }

    private void writeToArea(String something) {
        System.out.println("\"writeToArea\" running.");
        output.append(something);
        System.out.println("\"writeToArea\" finished.");
    }


    public JPanel getPanel(){
        return panel;
    }
}

而在你的客户端code

And in your client code

    public class Pangea {

        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable(){
                @Override
                public void run(){
                   createAndShowGUI();
                }
            });

        }

     /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event dispatch thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        System.out.println("Initializing GUI.");
        JFrame frame = new JFrame();
        frame.setTitle("PANGEA RPG [0.01 ALPHA][WIP]");
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Add contents to the window.
        frame.add(new UI().getPanel());


        frame.pack();//sizes the frame
        frame.setVisible(true);
        System.out.println("GUI Initialized.");
    }
}

在这里,你有比这更好的如何使用文本教程地区

我删除您的的setSize ,并使用包()

该包装方法尺寸框架,使所有的内容都达到或
  高于preferred大小。收拾另一种方法是
  显式调用的setSize或的setBounds建立帧大小
  (其中还设置帧位置)。在一般情况下,使用包是
  preferable调用的setSize,因为包离开框架布局
  经理主管帧大小和布局经理善于
  适应平台的依赖性和影响等因素的影响
  组件尺寸。

The pack method sizes the frame so that all its contents are at or above their preferred sizes. An alternative to pack is to establish a frame size explicitly by calling setSize or setBounds (which also sets the frame location). In general, using pack is preferable to calling setSize, since pack leaves the frame layout manager in charge of the frame size, and layout managers are good at adjusting to platform dependencies and other factors that affect component size.

这篇关于我怎样才能从追加不包含JTextArea中的方法的的JTextArea?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆