Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Struggling to display a button

I’ve previously made a button before but this is my first time trying to make it reusable for a project i’m working on. When i did it before i ran into no issues as it was all in one class but now with it being spread across a few classes I can’t do (this.add(button1));. As you can see I currently have add(button1) but it doesn’t add it to the screen what can i change to add it to the screen?

p.s i don’t mind about it not doing anything right now I just want it to be on the screen i can do the rest after that! Also this is my first time using stack overflow so sorry if I’ve formatted this message incorrectly.

I’ve tried a lot of things but not found any solutions – all of my attempts where hopeless ahah so not much i can add to what I’ve tried. Any help would be amazing thanks.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import javax.swing.*; 

 
 

public class Main { 

    static JFrame frame = new JFrame("Buttons"); 

    public static void main(String[] args) { 

        Mainmenu menu = new Mainmenu(); 

 
 

        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 

        frame.setSize(650,500); 

        frame.setVisible(true); 

 
 

        frame.add(menu); 

    } 

} 

import java.awt.*; 

import javax.swing.*; 

 
 

public class Mainmenu extends JPanel{ 

    private button button1; 

     

    Mainmenu () { 

        setLayout(null); 

        button1 = new button(50, 50, 50, 50, "game1", Color.red, Color.CYAN); 

        add(button1); 

    } 

} 

import javax.swing.JButton; 

import java.awt.Color; 

import java.awt.Font; 

 
 

public class button { 

 
 

    JButton tempButton = new JButton(); 

 
 

    public button (int x, int y, int width, int height, String buttonName, Color text, Color background) { 

        tempButton.setText(buttonName);  

        tempButton.setFont(new Font("Comic Sans",Font.BOLD,25)); 

        tempButton.setForeground(text); 

        tempButton.setBackground(background); 

        tempButton.setFocusable(false); 

 
 

        tempButton.setBounds(x, y, width, height); 

    } 

} 

 

 

>Solution :

Make a static method.

public static JButton button (int x, int y, int width, int height, String buttonName, Color text, Color background) { 
    JButton tempButton = new JButton();
    tempButton.setText(buttonName);  
    tempButton.setFont(new Font("Comic Sans",Font.BOLD,25)); 
    tempButton.setForeground(text); 
    tempButton.setBackground(background); 
    tempButton.setFocusable(false); 
    tempButton.setBounds(x, y, width, height); 
    return tempButton;
} 

That way you’ll return a formatted button.

The call to "setBounds" is really a bad practice. Don’t use null layout and dont call setBounds.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading