0

我是经验丰富的程序员,但直到最近才使用 Java。我有一个通过从互联网(huzzah)复制代码来工作的类,当我在使用 main() 函数时将下面的代码作为自己的线程运行时,Jframe 在自己的线程中运行。

但我有一个问题。我想将此 Jframe 用作另一个线程的交互部分(作为实现 Runnable 类的私有成员)。而且我希望他们能够互动。Jframe 方法 actionPerformed 需要调用父线程对象的函数(我输入命令,它会做一些事情)并且父需要调用 .append() 方法将结果报告给 Jframe。我可以在我的 Jframe 中包含对父级的引用,这样我就可以调用它的方法。

所以我的问题是关于为线程同步正确设计这个。我不确定线程​​同步和事件处理程序如何交互,因此我的线程可以正确使用非线程安全的 Jframe。

我在哪里以及如何正确初始化 Jframe?(具体来说,我应该以某种方式使用 EventQueue 吗?)还是我错过了一些明显的东西并且以完全错误的方式这样做?

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.plaf.ActionMapUIResource;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

public class CmdPrompt3 extends JFrame {
    private static final String progname = "CmdPrompt <Monitor Game Client>";

    private JTextField input;
    private JTextArea history;
    private JPanel mainpanel;
    private JMenuBar greenMenuBar;
    private String lastCmd;
    private boolean unusedCmd;
    //public boolean defaultConstruct = true;

    public CmdPrompt3(int x, int y, String name) { //offset coordinates
        super("CmdPrompt3 <" + name + ">");
        lastCmd = "";
        unusedCmd = false;
        //defaultConstruct = false;

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create the menu bar.  Make it have a green background.
        greenMenuBar = new JMenuBar();
        greenMenuBar.setOpaque(true);
        greenMenuBar.setBackground(new Color(154, 165, 127));
        greenMenuBar.setPreferredSize(new Dimension(200, 20));
        this.setJMenuBar(greenMenuBar);

        mainpanel = new JPanel(new BorderLayout());
        mainpanel.setBorder(new EmptyBorder(2, 3, 2, 3));

        // adjust numbers for a bigger default area
        JTextArea history = new JTextArea(25, 40); //25 lines, 40 columns
        Font font = new Font(
                Font.MONOSPACED,
                Font.PLAIN,
                16); //font size 16
        //history.getFont().getSize()+4);
        mainpanel.setFont(font);
        history.setFont(font);
        mainpanel.add(new JScrollPane(history,
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS));
        this.add(mainpanel);

        input = new JTextField(80);
        input.setFont(font);
        this.add(input, BorderLayout.SOUTH);

        ActionMap actionMap = new ActionMapUIResource();
        actionMap.put("enter", new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String cmd = input.getText();

                //String sofar = history.getText ();
                history.append("\tinput box>>\t" + cmd + "\n");

                //DO IMPORTANT STUFF HERE
                //Set flags for retrieving commands
                lastCmd = cmd;
                unusedCmd = true;

                //clear input line
                input.setText("");
            }
        });
        InputMap keyMap = new ComponentInputMap(input);
        keyMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "enter");

        SwingUtilities.replaceUIActionMap(input, actionMap);
        SwingUtilities.replaceUIInputMap(input, JComponent.WHEN_IN_FOCUSED_WINDOW, keyMap);

        setLocation(x, y);
        this.pack();
        setVisible(true);
    }

    //owner thread writes history to window
    //TODO this doesn't work when Client calls the function
    //Apparently,
    public void append(String S) {
        history.append(S);
        return;
    }

    //owner thread gets last issued command, if any
    public String getUnusedCmd() {
        if (unusedCmd) {
            unusedCmd = false;
        } else {
            lastCmd = "";
        }
        return lastCmd;
    }

    public static void main(final String args[]) {
        Runnable runner = new Runnable() {
            public void run() {
                new CmdPrompt3(100, 100, "Basic Class Test");
            }
        };
        EventQueue.invokeLater(runner);
    }
}
4

0 回答 0