简体   繁体   English

Java大规模多帧实例问题

[英]Java Massive Multiple Frame Instances Issue

sigh OK guys... There is going to be a painstaking amount of code here, but i'm going to do it anyway. 感叹好男人......这里将成为这里的代码量艰苦,但我还是要去做到这一点。
So basically, I have a custom made (well it's actually just a HEAVILY customized version of a JFrame) and am having major issues. 因此,基本上,我有一个定制的产品(实际上,它实际上只是JFrame的HEAVILY定制版本),并且存在重大问题。

I have a background. 我有背景。 (Fair enough, that's fine) THEN I have a Terminal frame that pops up and spits stuff out. (足够,没关系)然后我有一个弹出的终端框架,将东西吐出。 This Terminal frame is based off another class named CustomFrame. 该终端框架基于另一个名为CustomFrame的类。 I also have ANOTHER class called Notification, which is ALSO a frame class like Terminal ALSO based off Custom Frame. 我还有一个名为Notification的类,它也是一个框架类,例如基于自定义框架的Terminal ALSO。

In the beginning, background loads fine. 一开始,背景加载良好。 Terminal loads fine. 终端负载很好。 Calls method to show Notification window. 调用方法以显示“通知”窗口。 And thats where the problem rises. 那就是问题所在。 The notification window won't show. 通知窗口不会显示。

I have tried frame.setVisible(); 我已经尝试过frame.setVisible(); frame.setSize(); frame.setSize(); frame.setLocation(); frame.setLocation(); I have tried, EVERYTHING. 我尝试了一切。

And if I don't show Terminal at all, it seems to spit it's code onto Notification instead, almost like there can only be ONE instance of the CustomFrame open AT ALL TIMES. 而且,如果我根本不显示Terminal,它似乎会将其代码吐到Notification上,几乎就像在所有时间都只能打开一个CustomFrame实例。

I hope you understand my problems... So here is the code! 希望您能理解我的问题...代码如下!

Game.java 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 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);
}
}

Terminal.java Terminal.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 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();
    }
}

}

Types.java Types.java

public enum Types {
    TERMINAL, LOGINTERMINAL,
    NOTIFICATION;
}

ScrollBarUI.java 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;
    }
}

}

On a serious note though, if anyone is able to help me with such a sizeable post, I will SERIOUSLY be eternally grateful. 诚然,如果有人能在如此庞大的职位上为我提供帮助,我将永远感激不已。

Cheers and thankyou... ALOT. 干杯,谢谢你...很多。

Edit: 编辑:

Did have the time to fix up fonts. 确实有时间修复字体。 Extremely sorry, now it has been done. 非常抱歉,现在已完成。

Edit: 编辑:

Here is where the Notification frame is called and doesn't end up showing: 这是调用通知框架的地方,但最终没有显示出来:

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

在此处输入图片说明

As @Andrew Thompson, @trashgod pointed, it is a bad practice to use multiple frames. 正如@trashgod指出的@Andrew Thompson所说,使用多个框架是一种不好的做法。

If you still need to fix your problem, here goes: 如果您仍然需要解决问题,请按以下步骤进行:

The issue is with your static instance of the CustomFrame for your Game application and then modifying that frame instance using methods like setUndecorated(...) . 问题是你static的实例CustomFrame为您的游戏应用程序,然后修改使用类似的方法是框架实例setUndecorated(...)

In your Terminal class, you have Terminal课程中,您有

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

and in your Notification class, you have Notification类中

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

but you are getting the same instance of the frame 但是你得到frame的相同实例

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

So what this means : 所以这意味着:

When the Game application loads, the Terminal is visible. 当游戏应用程序加载时,终端可见。 And when you register a user, you are displying a Notification , with modified frame size and then by calling the setVisible() method of the CustomFrame . 而当你注册的用户,则是一个displying Notification ,具有修饰的帧大小,然后通过调用setVisible()的方法CustomFrame

Which is causing the issue. 造成问题的原因。 The setUndecorated() and setVisible() is invoked for the same static instance. 对于同一静态实例,将调用setUndecorated()setVisible() YOU CANNOT MODIFY A FRAME WHICH IS VISIBLE . 您无法修改可见的框架 Meaning, YOU CAN ONLY MODIFY A FRAME BEFORE IT IS VISIBLE . 意思是, 您只能在可见之前修改框架 Here your frame is already visible (for Terminal) and when displaying the Notification you are trying to change the size and display. 在这里,您的frame已经可见(对于终端),并且在显示Notification您尝试更改大小和显示。 WHICH IS WRONG. 错了。

As you said I want DIFFERENT JFrames, as in my code, I use Types.java as an enum to pick my different TYPES of frames. 正如您所说的那样, 我需要不同的JFrames,就像在代码中一样,我使用Types.java作为枚举来选择不同类型的帧。 The frame is completely different each time due to the use of different components and sizing , to achieve this, you need multiple instances for each type of frame. 每次由于使用不同的组件和调整大小而使框架完全不同 ,要实现此目的,每种类型的框架都需要多个实例。

Changes/Fixes to your code : 更改/修复您的代码:

Game1.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 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 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 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();
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM