简体   繁体   中英

Making Java window without JFrame or AWT

First of all, I want you to know that I searched the web for that but couldn't find anything. I remember that I saw a thread on it once but couldn't find it again; it was a long time ago.

I want to know how to make my own window, without JFrame or AWT. Everywhere I am searching just shows me library and pre-made code but I can't learn how it works inside. I want to know how to build a window, without what Java already gave me.

The good thing and the bad thing in Java is that Java makes things easy for the developers, so, that's why I tried C, searched for assembly code and tried to find a way to do this, but all of the approaches used libraries.

That why, I want to know, it is even possible? Don't need all of the windows, just the base of it or even pixels on the screen, but something that will not include using more libraries or made classes that were built for that and everyone using just because this is the only thing they know.

Thanks to everyone that will help me and will answer my question(s) :)

In Java, we are reliant on bindings into the native OS. The reason we have abstract frameworks, like AWT, Swing, JavaFX, SWT is because the process by which this is done is not trivial, especially if you actually want to paint to these surfaces in some meaningful way and is compounded when you consider the different requirements of the available operating systems.

Depending on what you actually want to achieve, will depend on which way you will go, for example...

You could...

Use a JWindow or an undecorated JFrame . This provides you with a available painting surface and a connection into a ready made event queue (message loop).

These two windows present you with an "empty" rectangle with no decorations. When dealing with these types of windows, you can provide your own decorations directly or via a custom look and feel.

You could...

Use a JNI (native) binding directly into the operating system which would allow you to ask the OS to create a window of your own. This means that you become responsible for determining how you are going to paint to the window, how you are going to process messages (events) from the OS that are targeted to your window and all other considerations.

Take a look at JNA Win32WindowDemo.java or if you really want to get funky, you could also have a look at ...

as some additional bindings into native graphics library

you could just use message dialogue boxes. I know I made a program that ran entirely off of them.

This is a rock paper scissors program I made a while ago that runs just off of option panes.

import java.util.Random;

import javax.swing.JOptionPane;

public class AI 
{
    public static void main(String[] args)
    {
    Random generator = new Random();
    int scan = 0;
    String bool = "";
    int rps;
    String type = "";
    int option;
    int counter = 0;
    String stuff = "";
    int cpu = 0;
    int foo = 0;
    int total = 0;
    while (true)
        {
        while(true)
        {



                type = JOptionPane.showInputDialog("Choose what game you would like to play. \n best of 3 \n best of 5 \n best of 7 \n first to 10");
                stuff = type.toLowerCase();
                if (stuff.equals ("best of 3"))
                {
                    option = JOptionPane.showConfirmDialog(null, "Play a best of 3 game?", "Exit", JOptionPane.YES_NO_OPTION);
                    break;
                }
                else if (stuff.equals ("best of 5"))
                {
                    option = JOptionPane.showConfirmDialog(null, "Play a best of 5 game?", "Exit", JOptionPane.YES_NO_OPTION);
                    break;
                }
                else if (stuff.equals ("best of 7"))
                {
                    option = JOptionPane.showConfirmDialog(null, "Play a best of 7 game?", "Exit", JOptionPane.YES_NO_OPTION);
                    break;
                }
                else if (stuff.equals ("first to 10"))
                {
                    option = JOptionPane.showConfirmDialog(null, "Play a game to 10?", "Exit", JOptionPane.YES_NO_OPTION);
                    break;
                }
                else
                {
                    continue;
                }
            }
            if (stuff.equals("best of 3"))
                foo = 3;
            else if (stuff.equals("best of 5"))
                foo = 5;
            else if (stuff.equals("best of 7"))
                foo = 7;
            else if (stuff.equals("first to 10"))
                foo = 10;
            for (int i = 0; i <= foo; i++)
            {
            while(true)
            {
            scan = Integer.parseInt(JOptionPane.showInputDialog ("Enter a number between 1 and 3. \n 1: rock\n 2: paper\n 3: scissors"));
            if (scan == 1)
                {
                    JOptionPane.showMessageDialog(null,"You chose rock.");
                    break;
                }
            else if (scan == 2)
                {
                    JOptionPane.showMessageDialog(null,"You chose paper.");
                    break;
                }
            else if (scan == 3)
                {
                    JOptionPane.showMessageDialog(null,"You chose scissors.");
                    break;
                }
            else
                {
                    JOptionPane.showMessageDialog(null,"Choose a valid number");
                    continue;
                }
            }
        rps = generator.nextInt(3) + 1; 
        String str;
        if (rps == 1)
            {
            str = "The cpu chose rock!";
            }
        else if (rps == 2)
            {
            str = "The cpu chose paper!";
            }
        else 
            {
            str = "The cpu chose scissors!";
            }
            JOptionPane.showMessageDialog(null,str);



            if (rps == 1 && scan == 2)
                {
                JOptionPane.showMessageDialog(null,"you won!");
                counter++;
                total++;

                }
            else if (rps == 2 && scan == 3)
            {
                JOptionPane.showMessageDialog(null,"you won!");
                counter++;
                total++;

            }
            else if (rps == 3 && scan == 1)
            {
                JOptionPane.showMessageDialog(null,"you won!");
                counter++;
                total++;

            }
            else if (rps == scan)
            {
                JOptionPane.showMessageDialog(null,"Try again!");
                continue;
            }
            else 
            {
                JOptionPane.showMessageDialog(null,"you lose!");
                cpu++;
                total++;

            }
            if(stuff.equals("best of 3") && counter == 2 || cpu ==2)
            {
                break;
            }
            else if (stuff.equals("best of 5") && counter==3 || cpu == 3)
            {
                break;
            }
            else if (stuff.equals("best of 7") && counter == 4 || cpu == 4)
            {
                break;
            }
            else if (stuff.equals("first to 10") && counter == 10 || cpu ==10)
            {
                break;
            }

        }




            bool = JOptionPane.showInputDialog ("do you want play another game? \n Type in yes or no");
            if (bool.equals("yes"))
                continue;
            else if (bool.equals("no"))
            break;

    }
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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