duldi
duldi

Reputation: 180

Add a GUI to class

I want to know how to add a GUI to my program. I have started creating a java program in Blue J and the first class of the program is a class which has been extended by other classes.

Now I have to make a GUI too but from my understanding I can only implement an interface as the GUI extends the class Frame. The problem is I want to create a GUI of my class, it has instance variables too so is there a work around? Could I make my first class an interface without altering the extensions too much?

code:

public class Players /* Class name */
{

    private int attack; /* Instance variables* */
    private int defence;
    private int jump;

    public Players(int a, int d, int j) /* Constructor being defined */
    {
        int total = a + d + j;
        if((total) == 100)
        {
            attack = a;
            defence = d;
            jump = j;
        }
        else
        {
            System.out.println("Make stats add to 100");
        }
    }

    public Players()/* Default contructor if not user defined */
    {
        attack = 34;
        defence = 33;
        jump = 33;
    }

    public void addAttack(int a)
    {
        attack += a;
    }

    public void addDefence(int a)
    {
        defence += a;
    }

    public void addJump(int a)
    {
        jump += a;
    }

    public void getBasicStats()
    {
        System.out.println(attack + " " + defence + " " + jump);
    }
}

This is my first class and my superclass for most of the other classes

Upvotes: 1

Views: 1982

Answers (4)

A4L
A4L

Reputation: 17595

How will I declare aan instance variable inside the GUI class?

Like as shown bellow, you could start with something like this, note that your application should be able to hand out your data to other classes, for instance I changed getBasicStats() to return a String, this way you can use your application class anywhere you want, I guess this is why you were confused about where to place the GUI code...

public class PlayersGUI extends JFrame {

    private static final long serialVersionUID = 1L;

    private Players players; // instance variable of your application

    private PlayersGUI() {
        players = new Players();
        initGUI();
    }

    private void initGUI() {
        setTitle("This the GUI for Players application");
        setPreferredSize(new Dimension(640, 560));
        setLocation(new Point(360, 240));

        JPanel jPanel = new JPanel();
        JLabel stat = new JLabel(players.getBasicStats());

        JButton attack = new JButton("Attack!");
        attack.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                players.addAttack(1);
            }
        });

        JButton hugeAttack = new JButton("HUGE Attack!");
        hugeAttack.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                players.addAttack(10);
            }
        });

        JButton defend = new JButton("Defend");
        defend.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                players.addDefence(1);
            }
        });

        JButton showStats = new JButton("Show stats");
        showStats.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                stat.setText(players.getBasicStats());
            }
        });

        jPanel.add(stat);
        jPanel.add(attack);
        jPanel.add(hugeAttack);
        jPanel.add(defend);
        jPanel.add(showStats);

        add(jPanel);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

    public static void main(String... args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                PlayersGUI pgui = new PlayersGUI();
                pgui.pack();
                pgui.setVisible(true);
            }
        });
    }
}

Upvotes: 0

Andreas Strand
Andreas Strand

Reputation: 11

Another suggestion: Learn JavaFX and download SceneBuilder from Oracle: here At my university they have stopped teaching Swing and started to teach JavaFX, saying JavaFX has taken over the throne from Swing.

SceneBuilder is very easy to use, drag and drop concept. It creates a FXML file which is used to declare your programs GUI.

Upvotes: 1

Code-Apprentice
Code-Apprentice

Reputation: 83577

I suggest learning how to use Swing. You will have several different classes interacting together. In fact, it is considered good practice to keep separate the code which creates and manages the GUI from the code which performs the underlying logic and data manipulation.

Upvotes: 2

The Noobie Coder
The Noobie Coder

Reputation: 39

I would recommend using netbeans to start with. From there you can easily select pre created classes such as Jframes. Much easier to learn. You can create a GUI from there by dragging and dropping buttons and whatever you need.

Here is a youtube tut to create GUI's in netbeans.

https://www.youtube.com/watch?v=LFr06ZKIpSM

If you decide not to go with netbeans, you are gonna have to create swing containers withing your class to make the Interface.

Upvotes: 0

Related Questions