0

I have been practicing my code with Java Swing and have gotten decent on controlling where to place some items, such as labels and or buttons, but I was wondering if you can do the same with classes? I have just a simple class with enough code to put a button in it and that's it, that I am trying to create an instance of the class and then control for to put on the left and right side but when I do, all it does is create two separate windows with the button in the middle and that's it. Am I doing something wrong, or can you not do classes the same way?

The code:

import java.awt.Color;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class Fun extends JFrame
{
    private final int WIDTH = 500;
    private final int HEIGHT = 400;

    public Fun()
    {
        setTitle("Fun Management");
        setSize(WIDTH, HEIGHT);
        
        BuildPanel west = new BuildPanel(); /// BuildPanel is the name of the class that has just a button in it. 
        BuildPanel east = new BuildPanel(); /// 
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        add(west, BorderLayout.WEST); /// I am doing the same thing with the instances as I would with buttons or labesl
        add(east, BorderLayout.EAST);
        
        setVisible(true);
    }
    
    public static void main(String[] args)
    {
        new Fun();
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    That `add()` method takes a type `Component` so if `BuildPanel` inherits from Component, you can do this. However, normally one keeps the GUI separate from the "program logic" classes, so be careful extending GUI classes like this. For keeping stuff separate see "MVC" https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller – markspace Aug 09 '21 at 05:23
  • Would it be possible to share the `BuildPanel` code? – akortex Aug 09 '21 at 06:36

1 Answers1

3

I took your code and created the following GUI.

Fun Management

Oracle has a rad tutorial, Creating a GUI With Swing, that will show you how to create Swing GUIs. Skip the Netbeans section.

Always start your Swing application with a call to the SwingUtilities invokeLater method. This method ensures that your Swing components are created and executed on the Event Dispatch Thread.

Use Swing components. Don't extend a Swing component unless you want to override one or more of the component methods.

The JFrame methods must be called in a specific order. This is the order I recommend for most Swing applications. Use the JFrame pack method and let the components size the JFrame.

I created a BuildPanel class to build a JPanel. There are good reasons to do this, but be careful. You have to manage each instance of the class you create. As an example, what if you want the text of the two buttons to be different? What if you want to assign two different ActionListener classes, one to each button?

Here's the complete runnable code. I made the BuildPanel class an inner class so I can post the code as one block.

import java.awt.BorderLayout;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TwoPanelExample implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new TwoPanelExample());
    }

    @Override
    public void run() {
        JFrame frame = new JFrame("Fun Management");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        BuildPanel west = new BuildPanel(); 
        BuildPanel east = new BuildPanel(); 
        
        frame.add(west.getPanel(), BorderLayout.WEST);
        frame.add(east.getPanel(), BorderLayout.EAST);
        
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }
    
    public class BuildPanel {
        
        private final JPanel panel;
        
        public BuildPanel() {
            this.panel = createMainPanel();
        }
        
        private JPanel createMainPanel() {
            JPanel panel = new JPanel();
            panel.setBorder(BorderFactory.createEmptyBorder(5, 30, 5, 30));
            
            JButton button = new JButton("Click Me");
            panel.add(button);
            
            return panel;
        }

        public JPanel getPanel() {
            return panel;
        }
        
        
    }

}
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111