使线程运行Jframe

时间:2016-03-16 15:35:56

标签: java multithreading swing jframe non-thread-safe

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

但我有一个问题。我想使用这个Jframe作为另一个线程的交互部分(作为实现Runnable类的私有成员)。我希望他们互动。 Jframe方法actionPerformed需要调用父线程对象的函数(我键入命令,它执行某些操作)并且父需要调用.append()方法将结果报告给Jframe。我可以在我的Jframe中包含对父类的引用,所以我可以调用它的方法。

所以我的问题是关于为线程同步正确设计它。 我不确定线程​​同步和事件处理程序如何交互,因此我的线程可以正确使用not threadsafe 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);
    }
}

0 个答案:

没有答案