Java 大规模多框架实例问题

标签 java swing class instance frame

叹气好吧,伙计们...这里将会有大量的代码,但无论如何我都会这样做。
所以基本上,我有一个定制的(实际上它只是 JFrame 的高度定制版本)并且遇到了重大问题。

我有背景。 (公平地说,这很好)然后我有一个弹出的终端框架并吐出东西。该终端框架基于另一个名为 CustomFrame 的类。我还有另一个类,称为通知,它也是一个框架类,如终端,也基于自定义框架。

一开始,背景加载良好。终端加载良好。调用方法来显示通知窗口。这就是问题所在。通知窗口不会显示。

我尝试过frame.setVisible();框架.setSize();框架.setLocation();我已经尝试过一切。

如果我根本不显示终端,它似乎会将其代码吐到通知上,几乎就像始终只能打开一个 CustomFrame 实例一样。

我希望你理解我的问题...所以这是代码!

Game.java

public class Game implements KeyListener {

int BACK_WIDTH = java.awt.Toolkit.getDefaultToolkit().getScreenSize().width;
int BACK_HEIGHT = java.awt.Toolkit.getDefaultToolkit().getScreenSize().height;

JFrame back_frame = new JFrame();
JPanel window = new JPanel();
JLabel title = new JLabel(Variables.TITLE);

Terminal login = new Terminal();

public static void main(String[] args) {
    new Game();
}

public Game() {
    try {

        back_frame.setSize(BACK_WIDTH, BACK_HEIGHT);
        back_frame.setLocation(0, 0);
        back_frame.getContentPane().setBackground(Color.BLACK);
        back_frame.setUndecorated(true);
        back_frame.setVisible(true);
        back_frame.add(window);
        window.setBackground(Color.BLACK);
        window.setLayout(null);

        window.add(title);
        title.setBounds((BACK_WIDTH / 2) - (550 / 2), (BACK_HEIGHT / 2) - (50 / 2), 550, 50);
        title.setForeground(Color.WHITE);

        back_frame.addKeyListener(this);
        login.addKeyListener(this);
        login.setLocationRelativeTo(null);
        login.setVariables(Types.LOGINTERMINAL);

        waitForStart();

    } catch (FontFormatException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

int index;
public void waitForStart() {
    Timer timer = new Timer(2000, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (index < 1 && index >= 0) {
                index++;
            } else {
                ((Timer)e.getSource()).stop();

                login.setVisible(true);
                login.slowPrint("Please login to continue...\n"
                          + "Type 'help' for more information.\n");
            }
       }
    });
    timer.start();
}

public void keyPressed(KeyEvent e) {
    int i = e.getKeyCode();

    if(i == KeyEvent.VK_ESCAPE) {
        System.exit(0);
    }
}

public void keyReleased(KeyEvent e) {}

public void keyTyped(KeyEvent e) {}

}

CustomFrame.java

public class CustomFrame implements MouseListener {

static JFrame frame = new JFrame();
public static Paint window = new Paint();

public void addKeyListener(KeyListener listener) {
    frame.addKeyListener(listener);
}

private Point initialClick;
private boolean inBounds = false;

public int getWidth() {
    return frame.getWidth();
}
public int getHeight() {
    return frame.getHeight();
}

public void add(JComponent component) {
    window.add(component);
}

public void setLocation(int x, int y) {
    frame.setLocation(x, y);
}

public void setLocationRelativeTo(Component c) {
    frame.setLocationRelativeTo(c);
}

private void setFrameType(Types type) {
    switch(type) {
        case TERMINAL:
            frame.setSize(600, 400);
            break;
        case LOGINTERMINAL:
            frame.setSize(600, 400);
            break;
        case NOTIFICATION:
            frame.setSize(300, 150);
            break;
        default:
            frame.setSize(600, 400);
            break;
    }
}

int index = 0;
public void slowPrint(final String text, final JTextArea field) {
    Timer timer = new Timer(40, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (index < text.length() && index >= 0) {
                String newChar = Character.toString(text.charAt(index));
                field.append(newChar);
                index++;
            } else {
                field.append("\n");

                index = 0;
                ((Timer)e.getSource()).stop();
            }
       }
    });
    timer.start();
}

public void slowPrintAndClear(final String text, final JTextArea field, final boolean andQuit) {
    Timer timer = new Timer(40, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (index < text.length() && index >= 0) {
                String newChar = Character.toString(text.charAt(index));
                field.append(newChar);
                index++;
            } else {
                field.append("\n");

                if(andQuit == false) {
                    field.setText(null);
                } else {
                    System.exit(0);
                }

                index = 0;
                ((Timer)e.getSource()).stop();
            }
       }
    });
    timer.start();
}

public CustomFrame(Types type) {

    frame.setAlwaysOnTop(true);
    frame.addMouseListener(this);
    frame.setResizable(false);
    frame.setUndecorated(true);
    setFrameType(type);
    frame.add(window);
    window.setLayout(null);

    frame.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            initialClick = e.getPoint();
            frame.getComponentAt(initialClick);
        }
    });

    frame.addMouseMotionListener(new MouseMotionAdapter() {
            public void mouseDragged(MouseEvent e) {
                if(e.getX() >= 0 && e.getX()<= frame.getWidth() &&
                   e.getY() >= 0 && e.getY() <= 20) {
                    inBounds = true;
                }
                if(inBounds == true) {
                    int thisX = frame.getLocation().x;
                    int thisY = frame.getLocation().y;
                    int xMoved = (thisX + e.getX()) - (thisX + initialClick.x);
                    int yMoved = (thisY + e.getY()) - (thisY + initialClick.y);
                    int x = thisX + xMoved;
                    int y = thisY + yMoved;
                    frame.setLocation(x, y);
                }
            }
        });
}

public JFrame setVisible(boolean bool) {
    frame.setVisible(bool);
    return null;
}

public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}

public void mousePressed(MouseEvent e) {
    int x = e.getX();
    int y = e.getY();

    if(x >= CustomFrame.frame.getWidth() - 20 && x <= CustomFrame.frame.getWidth() - 6 &&
       y >= 3 && y <= 14) {
        frame.dispose();
    }

}

public void mouseReleased(MouseEvent e) {
    inBounds = false;
}

}

class Paint extends JPanel {
private static final long serialVersionUID = 1L;

private void doDrawing(Graphics g) {

    Graphics2D g2d = (Graphics2D) g;

    g2d.setColor(Color.BLACK);
    g2d.fillRect(0, 0, CustomFrame.frame.getWidth(), CustomFrame.frame.getHeight());

    Color LIGHT_BLUE = new Color(36, 171, 255);

    //g2d.setColor(Color.BLUE);
    GradientPaint topFill = new GradientPaint(0, 0, LIGHT_BLUE, CustomFrame.frame.getWidth(), 20, Color.BLUE);
    g2d.setPaint(topFill);

    g2d.fillRect(0, 0, CustomFrame.frame.getWidth(), 20);

    g2d.setColor(Color.WHITE);
    g2d.drawRect(0, 0, CustomFrame.frame.getWidth() - 1, CustomFrame.frame.getHeight() - 1);
    g2d.drawLine(0, 20, CustomFrame.frame.getWidth(), 20);

    g2d.fillRect(CustomFrame.frame.getWidth() - 20, 3, 14, 14);

}

public void paintComponent(Graphics g) {

    super.paintComponent(g);
    doDrawing(g);
}
}

终端.java

public class Terminal implements KeyListener {

static CustomFrame frame = new CustomFrame(Types.TERMINAL);

JTextArea log = new JTextArea();
JTextField field = new JTextField();

public void setVisible(boolean bool) {
    frame.setVisible(bool);
}

public void addKeyListener(KeyListener listener) {
    frame.addKeyListener(listener);
}

public void setLogText(String str) {
    log.setText(log.getText() + str + "\n");
}

public void setLocation(int x, int y) {
    frame.setLocation(x, y);
}

public void setLocationRelativeTo(Component c) {
    frame.setLocationRelativeTo(c);
}

int index = 0;
public void slowPrint(final String text) {
    frame.slowPrint(text, log);
}

public void slowPrintAndClear(final String text, boolean andQuit) {
    frame.slowPrintAndClear(text, log, andQuit);
}

public Terminal() {
    try {

        JScrollPane pane = new JScrollPane();
        JScrollBar scrollBar = pane.getVerticalScrollBar();

        scrollBar.setUI(new ScrollBarUI());
        pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        pane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        pane.setViewportView(log);

        frame.add(field);
        frame.add(pane);            

        log.setBackground(Color.BLACK);
        log.setForeground(Color.WHITE);
        log.setWrapStyleWord(true);
        log.setLineWrap(true);
        pane.setBounds(4, 20 + 4, frame.getWidth() - 8, frame.getHeight() - 50);
        pane.setBorder(null);
        log.setEditable(false);
        log.setCaretColor(Color.BLACK);

        field.setBackground(Color.BLACK);
        field.setForeground(Color.WHITE);
        field.setBounds(2, frame.getHeight() - 23, frame.getWidth() - 5, 20);
        field.setHighlighter(null);
        field.setCaretColor(Color.BLACK);
        field.addKeyListener(this);
        field.setText("  >  ");

    } catch (FontFormatException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private void dumpToLog() {
    log.setText(log.getText() + field.getText() + "\n");
    field.setText("  >  ");
}

public void setVariables(Types type) {
    switch(type) {
        case TERMINAL:
            this.type = Types.TERMINAL;
            break;
        case LOGINTERMINAL:
            this.type = Types.LOGINTERMINAL;
            break;
        default:
            this.type = Types.TERMINAL;
            break;
    }
}

Types type;
public void keyPressed(KeyEvent e) {
    int i = e.getKeyCode();

    String text1 = "  >  ";
    String text2 = field.getText().replaceFirst(text1, "");
    String text2_1 = text2.trim();
    String text = text1 + text2_1;

    if (type == Types.TERMINAL) {

    } else if (type == Types.LOGINTERMINAL) {
        if(i == KeyEvent.VK_ENTER && field.isFocusOwner()) {
            if(text.startsWith("  >  register") || text.startsWith("  >  REGISTER")) {
                if(!(text.length() == 13)) {
                    dumpToLog();
                    slowPrint("Registry not available at this current given time.\n");
                    //TODO: Create registry system.
                    new Notification("test");
                } else {
                    dumpToLog();
                    slowPrint("\nInformation:\n"
                            + "Registers a new account.\n\n"
                            + "Usage:\n"
                            + "register <username>\n");
                }
            } else {
                System.out.println("start |" + text + "| end");
                dumpToLog();
                slowPrint("Unknown command.\n");
            }
        }
    } else {
        // SETUP CODE FOR NOTIFICATION ERROR AGAIN
    }

    if(field.isFocusOwner() && i == KeyEvent.VK_LEFT || i == KeyEvent.VK_RIGHT) {
        e.consume();
    }

    if(!field.getText().startsWith("  >  ")) {
        field.setText("  >  ");
    }
}

public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}

}

Notification.java

public class Notification {

static CustomFrame frame = new CustomFrame(Types.NOTIFICATION);
JTextArea display = new JTextArea();

public Notification(String notification) {
    try {

        frame.setLocationRelativeTo(null);
        frame.add(display);

        display.setBackground(Color.BLACK);
        display.setForeground(Color.WHITE);
        display.setWrapStyleWord(true);
        display.setLineWrap(true);
        display.setBounds(4, 20 + 4, frame.getWidth() - 8, frame.getHeight() - 50);
        display.setBorder(null);
        display.setEditable(false);
        display.setCaretColor(Color.BLACK);

        frame.slowPrint(notification, display);
        frame.setVisible(true);
    } catch (FontFormatException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

类型.java

public enum Types {
    TERMINAL, LOGINTERMINAL,
    NOTIFICATION;
}

ScrollBarUI.java

public class ScrollBarUI extends MetalScrollBarUI {

private Image thumb, track;

private JButton blankButton() {
    JButton b = new JButton();
    b.setPreferredSize(new Dimension(0, 0));
    b.setMaximumSize(new Dimension(0, 0));
    b.setMinimumSize(new Dimension(0, 0));
    return b;
}

public ScrollBarUI() {
    thumb = FauxImage.create(32, 32, true);
    track = FauxImage.create(32, 32, false);
}

protected void paintThumb(Graphics g, JComponent component, Rectangle rectangle) {
    Graphics2D g2d = (Graphics2D) g;
    g.setColor(Color.BLUE);
    g2d.drawImage(thumb, rectangle.x, rectangle.y, rectangle.width, rectangle.height, null);
    g2d.setPaint(Color.WHITE);
    g2d.drawRect(rectangle.x, rectangle.y, rectangle.width - 1, rectangle.height-1);
}

protected void paintTrack(Graphics g, JComponent component, Rectangle rectangle) {
    ((Graphics2D) g).drawImage(track, rectangle.x, rectangle.y, rectangle.width, rectangle.height, null);
}

protected JButton createIncreaseButton(int orientation) {
    return blankButton();
}

protected JButton createDecreaseButton(int orientation) {
    return blankButton();
}

private static class FauxImage {
    static public Image create(int width, int height, boolean thumb) {
        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = bi.createGraphics();

        if (thumb == true) {
            Color LIGHT_BLUE = new Color(0, 140, 255);
            //g2d.setPaint(Color.BLUE);

            GradientPaint topFill = new GradientPaint(5, 25, Color.BLUE, 2, 2, LIGHT_BLUE);
            g2d.setPaint(topFill);
            g2d.fillRect(0, 0, width, height);

            g2d.dispose();
        } else {
            g2d.setPaint(Color.BLACK);
            g2d.fillRect(0, 0, width, height);

            g2d.dispose();
        }

        return bi;
    }
}

}

不过,严肃地说,如果有人能够帮助我完成如此大的帖子,我将永远感激不已。

干杯,谢谢...很多。

编辑:

确实有时间修复字体。非常抱歉,现在已经完成了。

编辑:

这里是通知框架被调用但最终没有显示的地方:

if(!(text.length() == 13)) {
    dumpToLog();
    slowPrint("Registry not available at this current given time.\n");
    //TODO: Create registry system.
    new Notification("test");
}

最佳答案

enter image description here

正如 @Andrew Thompson、@trashgod 指出的那样,使用多个框架是一种不好的做法。

如果您仍然需要解决问题,请执行以下操作:

问题在于您的游戏应用程序的 CustomFramestatic 实例,然后使用 setUndecorated(...) 等方法修改该框架实例。

在您的Terminal 类中,您有

static CustomFrame frame = new CustomFrame(Types.TERMINAL);

在你的Notification类中,你有

static CustomFrame frame = new CustomFrame(Types.NOTIFICATION);

但是您得到的是 frame 的相同实例

static JFrame frame = new JFrame(); (in your CustomFrame class)

这意味着什么:

加载游戏应用程序时,终端可见。当您注册用户时,您将显示一个通知,并修改帧大小,然后调用CustomFramesetVisible()方法。

这就是导致问题的原因。针对同一个静态实例调用 setUndecorated()setVisible()您无法修改可见的框架。意思是,您只能在框架可见之前对其进行修改。在这里,您的框架已经可见(对于终端),并且在显示通知时,您正在尝试更改大小和显示。这是错误的。

正如您所说我想要不同的 JFrame,就像在我的代码中一样,我使用 Types.java 作为枚举来选择不同类型的框架。由于使用不同的组件和大小,框架每次都完全不同,要实现这一点,您需要为每种类型的框架创建多个实例。

对代码的更改/修复:

Game1.java

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Game1 implements KeyListener
{

    int BACK_WIDTH = java.awt.Toolkit.getDefaultToolkit().getScreenSize().width;
    int BACK_HEIGHT = java.awt.Toolkit.getDefaultToolkit().getScreenSize().height;

    JFrame back_frame = new JFrame();
    JPanel window = new JPanel();
    JLabel title = new JLabel("Title");

    Terminal1 login = new Terminal1();

    public static void main(String[] args)
    {
        new Game1();
    }

    public Game1()
    {
        try
        {

            back_frame.setSize(BACK_WIDTH, BACK_HEIGHT);
            back_frame.setLocation(0, 0);
            back_frame.getContentPane().setBackground(Color.BLACK);
            back_frame.setUndecorated(true);
            back_frame.setVisible(true);
            back_frame.add(window);
            window.setBackground(Color.BLACK);
            window.setLayout(null);

            window.add(title);
            title.setBounds((BACK_WIDTH / 2) - (550 / 2), (BACK_HEIGHT / 2) - (50 / 2), 550, 50);
            title.setForeground(Color.WHITE);

            back_frame.addKeyListener(this);
            login.addKeyListener(this);
            login.setLocationRelativeTo(null);
            login.setVariables(Types.LOGINTERMINAL);

            waitForStart();

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    int index;

    public void waitForStart()
    {
        Timer timer = new Timer(2000, new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                if (index < 1 && index >= 0)
                {
                    index++;
                }
                else
                {
                    ((Timer) e.getSource()).stop();

                    login.setVisible(true);
                    login.slowPrint("Please login to continue...\n" + "Type 'help' for more information.\n");
                }
            }
        });
        timer.start();
    }

    public void keyPressed(KeyEvent e)
    {
        int i = e.getKeyCode();

        if (i == KeyEvent.VK_ESCAPE)
        {
            System.exit(0);
        }
    }

    public void keyReleased(KeyEvent e)
    {
    }

    public void keyTyped(KeyEvent e)
    {
    }

}

CustomFrame1.java

import java.awt.Color;
import java.awt.Component;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.Timer;

public class CustomFrame1 implements MouseListener
{

    JFrame frame = new JFrame();
    public static Paint window = null;

    public void addKeyListener(KeyListener listener)
    {
        frame.addKeyListener(listener);
    }

    private Point initialClick;
    private boolean inBounds = false;

    public int getWidth()
    {
        return frame.getWidth();
    }

    public int getHeight()
    {
        return frame.getHeight();
    }

    public void add(JComponent component)
    {
        window.add(component);
    }

    public void setLocation(int x, int y)
    {
        frame.setLocation(x, y);
    }

    public void setLocationRelativeTo(Component c)
    {
        frame.setLocationRelativeTo(c);
    }

    private void setFrameType(Types type)
    {
        switch (type)
        {
        case TERMINAL:
            frame.setSize(600, 400);
            break;
        case LOGINTERMINAL:
            frame.setSize(600, 400);
            break;
        case NOTIFICATION:
            frame.setSize(300, 150);
            break;
        default:
            frame.setSize(600, 400);
            break;
        }
    }

    int index = 0;

    public void slowPrint(final String text, final JTextArea field)
    {
        Timer timer = new Timer(40, new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                if (index < text.length() && index >= 0)
                {
                    String newChar = Character.toString(text.charAt(index));
                    field.append(newChar);
                    index++;
                }
                else
                {
                    field.append("\n");

                    index = 0;
                    ((Timer) e.getSource()).stop();
                }
            }
        });
        timer.start();
    }

    public void slowPrintAndClear(final String text, final JTextArea field, final boolean andQuit)
    {
        Timer timer = new Timer(40, new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                if (index < text.length() && index >= 0)
                {
                    String newChar = Character.toString(text.charAt(index));
                    field.append(newChar);
                    index++;
                }
                else
                {
                    field.append("\n");

                    if (andQuit == false)
                    {
                        field.setText(null);
                    }
                    else
                    {
                        System.exit(0);
                    }

                    index = 0;
                    ((Timer) e.getSource()).stop();
                }
            }
        });
        timer.start();
    }

    public CustomFrame1(Types type)
    {

        window = new Paint(frame);
        frame.setAlwaysOnTop(true);
        frame.addMouseListener(this);
        frame.setResizable(false);
        frame.setUndecorated(true);
        setFrameType(type);
        frame.add(window);
        window.setLayout(null);

        frame.addMouseListener(new MouseAdapter()
        {
            public void mousePressed(MouseEvent e)
            {
                initialClick = e.getPoint();
                frame.getComponentAt(initialClick);
            }
        });

        frame.addMouseMotionListener(new MouseMotionAdapter()
        {
            public void mouseDragged(MouseEvent e)
            {
                if (e.getX() >= 0 && e.getX() <= frame.getWidth() && e.getY() >= 0 && e.getY() <= 20)
                {
                    inBounds = true;
                }
                if (inBounds == true)
                {
                    int thisX = frame.getLocation().x;
                    int thisY = frame.getLocation().y;
                    int xMoved = (thisX + e.getX()) - (thisX + initialClick.x);
                    int yMoved = (thisY + e.getY()) - (thisY + initialClick.y);
                    int x = thisX + xMoved;
                    int y = thisY + yMoved;
                    frame.setLocation(x, y);
                }
            }
        });
    }

    public void dispose()
    {
        frame.dispose();
    }

    public JFrame setVisible(boolean bool)
    {
        frame.setVisible(bool);
        return null;
    }

    public void mouseClicked(MouseEvent e)
    {
    }

    public void mouseEntered(MouseEvent e)
    {
    }

    public void mouseExited(MouseEvent e)
    {
    }

    public void mousePressed(MouseEvent e)
    {
        int x = e.getX();
        int y = e.getY();

        if (x >= frame.getWidth() - 20 && x <= frame.getWidth() - 6 && y >= 3 && y <= 14)
        {
            frame.dispose();
        }

    }

    public void mouseReleased(MouseEvent e)
    {
        inBounds = false;
    }

}

class Paint extends JPanel
{
    private static final long serialVersionUID = 1L;

    private JFrame frame;

    public Paint(JFrame frame)
    {
        this.frame = frame;
    }

    private void doDrawing(Graphics g)
    {

        Graphics2D g2d = (Graphics2D) g;

        g2d.setColor(Color.BLACK);
        g2d.fillRect(0, 0, frame.getWidth(), frame.getHeight());

        Color LIGHT_BLUE = new Color(36, 171, 255);

        // g2d.setColor(Color.BLUE);
        GradientPaint topFill = new GradientPaint(0, 0, LIGHT_BLUE, frame.getWidth(), 20, Color.BLUE);
        g2d.setPaint(topFill);

        g2d.fillRect(0, 0, frame.getWidth(), 20);

        g2d.setColor(Color.WHITE);
        g2d.drawRect(0, 0, frame.getWidth() - 1, frame.getHeight() - 1);
        g2d.drawLine(0, 20, frame.getWidth(), 20);

        g2d.fillRect(frame.getWidth() - 20, 3, 14, 14);

    }

    public void paintComponent(Graphics g)
    {

        super.paintComponent(g);
        doDrawing(g);
    }
}

Terminal1.java

import java.awt.Color;
import java.awt.Component;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;

public class Terminal1 implements KeyListener
{

    static CustomFrame1 frame = new CustomFrame1(Types.TERMINAL);

    JTextArea log = new JTextArea();
    JTextField field = new JTextField();

    public void setVisible(boolean bool)
    {
        frame.setVisible(bool);
    }

    public void addKeyListener(KeyListener listener)
    {
        frame.addKeyListener(listener);
    }

    public void setLogText(String str)
    {
        log.setText(log.getText() + str + "\n");
    }

    public void setLocation(int x, int y)
    {
        frame.setLocation(x, y);
    }

    public void setLocationRelativeTo(Component c)
    {
        frame.setLocationRelativeTo(c);
    }

    int index = 0;

    public void slowPrint(final String text)
    {
        frame.slowPrint(text, log);
    }

    public void slowPrintAndClear(final String text, boolean andQuit)
    {
        frame.slowPrintAndClear(text, log, andQuit);
    }

    public Terminal1()
    {
        try
        {

            JScrollPane pane = new JScrollPane();
            JScrollBar scrollBar = pane.getVerticalScrollBar();

            scrollBar.setUI(new ScrollBarUI());
            pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
            pane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
            pane.setViewportView(log);

            frame.add(field);
            frame.add(pane);

            log.setBackground(Color.BLACK);
            log.setForeground(Color.WHITE);
            log.setWrapStyleWord(true);
            log.setLineWrap(true);
            pane.setBounds(4, 20 + 4, frame.getWidth() - 8, frame.getHeight() - 50);
            pane.setBorder(null);
            log.setEditable(false);
            log.setCaretColor(Color.BLACK);

            field.setBackground(Color.BLACK);
            field.setForeground(Color.WHITE);
            field.setBounds(2, frame.getHeight() - 23, frame.getWidth() - 5, 20);
            field.setHighlighter(null);
            field.setCaretColor(Color.BLACK);
            field.addKeyListener(this);
            field.setText("  >  ");

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    private void dumpToLog()
    {
        log.setText(log.getText() + field.getText() + "\n");
        field.setText("  >  ");
    }

    public void setVariables(Types type)
    {
        switch (type)
        {
        case TERMINAL:
            this.type = Types.TERMINAL;
            break;
        case LOGINTERMINAL:
            this.type = Types.LOGINTERMINAL;
            break;
        default:
            this.type = Types.TERMINAL;
            break;
        }
    }

    Types type;

    public void keyPressed(KeyEvent e)
    {
        int i = e.getKeyCode();

        String text1 = "  >  ";
        String text2 = field.getText().replaceFirst(text1, "");
        String text2_1 = text2.trim();
        String text = text1 + text2_1;

        if (type == Types.TERMINAL)
        {

        }
        else if (type == Types.LOGINTERMINAL)
        {
            if (i == KeyEvent.VK_ENTER && field.isFocusOwner())
            {
                if (text.startsWith("  >  register") || text.startsWith("  >  REGISTER"))
                {
                    if (!(text.length() == 13))
                    {
                        dumpToLog();
                        slowPrint("Registry not available at this current given time.\n");
                        // TODO: Create registry system.
                        new Notification1("test");
                    }
                    else
                    {
                        dumpToLog();
                        slowPrint("\nInformation:\n" + "Registers a new account.\n\n" + "Usage:\n" + "register <username>\n");
                    }
                }
                else
                {
                    System.out.println("start |" + text + "| end");
                    dumpToLog();
                    slowPrint("Unknown command.\n");
                }
            }
        }
        else
        {
            // SETUP CODE FOR NOTIFICATION ERROR AGAIN
        }

        if (field.isFocusOwner() && i == KeyEvent.VK_LEFT || i == KeyEvent.VK_RIGHT)
        {
            e.consume();
        }

        if (!field.getText().startsWith("  >  "))
        {
            field.setText("  >  ");
        }
    }

    public void keyReleased(KeyEvent e)
    {
    }

    public void keyTyped(KeyEvent e)
    {
    }

}

Notification1.java

import java.awt.Color;

import javax.swing.JTextArea;

public class Notification1
{

    static CustomFrame1 frame = new CustomFrame1(Types.NOTIFICATION);
    JTextArea display = new JTextArea();

    public Notification1(String notification)
    {
        try
        {

            frame.setLocationRelativeTo(null);
            frame.add(display);

            display.setBackground(Color.BLACK);
            display.setForeground(Color.WHITE);
            display.setWrapStyleWord(true);
            display.setLineWrap(true);
            display.setBounds(4, 20 + 4, frame.getWidth() - 8, frame.getHeight() - 50);
            display.setBorder(null);
            display.setEditable(false);
            display.setCaretColor(Color.BLACK);

            frame.slowPrint(notification, display);
            frame.setVisible(true);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

}

关于Java 大规模多框架实例问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26905349/

相关文章:

类中的 JavaScript/jQuery 变量作用域 (this)

java - dialogBu​​ilder.setMessage 为空

java - 如何从不同机器上的 JSF 应用程序访问 EJB?

java - 属性文件中的常量

java - 在 Swing Java 中向 JList 添加元素

java - 为什么我的照片不再显示了

c++ - Variadic 模板类,从其参数列表中获取特定类型的索引

java - 为什么不能将整数转换为字符串?

Java隐藏JFrame1和JFrame2,当JFrame0停用时?

c++ - 为什么 delete 仅在 main 中起作用,而在将要删除的一个或多个对象作为输入的函数中不起作用?