2

So, I am trying to create buttons 8 on left and right side each. And I am really new to GUI. So, I am not sure how to change the colour and shape, to make those buttons a circle and color them in red and blue...This what I have so far...

import javax.swing.*;

import java.awt.*;

public class Arrangement {

// main must be static
public static void main(String[] args) {
    Arrangement arrangement = new Arrangement();
    arrangement.handle();
}

public void handle() {
    JFrame f= new JFrame();
    f.setVisible(true);
    f.setSize(600,400);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel p = new JPanel (new GridBagLayout());//in the constructor u specify the layout :)
    JPanel a = new JPanel (new GridBagLayout());


    JButton b1 = new JButton("Button 1");
    JButton b2 = new JButton("Button 2");
    JButton b3= new JButton ("Button 3");
    JButton b4= new JButton ("Button 4");
    JButton b5= new JButton ("Button 5");
    JButton b6= new JButton ("Button 6");
    JButton b7 = new JButton("Button 7");
    JButton b8 = new JButton("Button 8");
    JButton b9 = new JButton ("Button 9");
    JButton b10 = new JButton ("Button 10");
    JButton b11 = new JButton ("Button 11");
    JButton b12 =new JButton ("Button 12");
    JButton b13 = new JButton("Button 13");
    JButton b14= new JButton("Button 14");
    JButton b15= new JButton ("Button 15");
    JButton b16 = new JButton ("Button 16");

    GridBagConstraints c= new GridBagConstraints();
    GridBagConstraints d= new GridBagConstraints();

    c.insets = new Insets(5,5,5,5);//spacing

    c.gridx=0;
    c.gridy=1;
    p.add(b1,c);

    c.gridx=0;
    c.gridy=2;      
    p.add(b2,c);

    c.gridx=0;
    c.gridy=4;      
    p.add(b3,c);

    c.gridx=0;
    c.gridy=6;      
    p.add(b4,c);

    c.gridx=0;
    c.gridy=8;      
    p.add(b5,c);

    c.gridx=0;
    c.gridy=10;     
    p.add(b6,c);

    c.gridx=0;
    c.gridy=11;     
    p.add(b7,c);

    c.gridx=0;
    c.gridy=12;     
    p.add(b8,c);

    d.insets = new Insets(5,5,5,5);

    d.gridx=0;
    d.gridy=1;
    a.add(b9,d);

    d.gridx=0;
    d.gridy=2;
    a.add(b10,d);

    d.gridx=0;
    d.gridy=3;
    a.add(b11,d);

    d.gridx=0;
    d.gridy=4;
    a.add(b12,d);

    d.gridx=0;
    d.gridy=6;
    a.add(b13,d);

    d.gridx=0;
    d.gridy=8;
    a.add(b14,d);

    d.gridx=0;
    d.gridy=10;
    a.add(b15,d);

    d.gridx=0;
    d.gridy=12;
    a.add(b16,d);


    f.add(p, BorderLayout.WEST);
    f.add(a, BorderLayout.EAST);

}

} Now the problem here is I cant use "this. ", with static and if I remove static I get an error saying I need to include static for my code to work... Can someone help me debug this :'( and direct me as to how can I get my buttons to be desired shape and colour...! Any help would be greatly appreciated <3

Samya
  • 25
  • 6
  • You can't reference non-static variables from static contexts. What you can do is create an instance of Arrangement and substitute this for your instance reference – Absalon Castañon Feb 15 '16 at 14:34
  • @AbsalonCastañon is right. I give a few more details on this issue below. – Austin Feb 15 '16 at 14:41
  • Not really an answer but I suggest that you ***don't*** do awt.*; and swing.*; They both are huge libraries and may slow down the program when you don't need everything there. – Jack Feb 15 '16 at 16:04
  • Thanks for the suggestion @Jack :) Duly noted! – Samya Feb 15 '16 at 17:33

3 Answers3

2

This was too long for a comment, but it relates to your use of the static keyword:

The reason you can't use the this keyword and must have static is because your code is executed in the static main method. Instead, move all the code from main to a new method in the Arrangement class, say it's called handle(). Then create an instance of Arrangement at the beginning of your main class and call handle(). For instance:

public class Arrangement {

    // main must be static
    public static void main(String[] args) {
        Arrangement arrangement = new Arrangement();
        arrangement.handle();
    }

    public void handle() {
        /* Put the rest of your code here and 
         * you'll be able to use the 'this' keyword */
    }

}

Additional questions that might be helpful:

EDIT A similar task was described in this question. The user wanted to have clickable circles and squares display on-screen. Rather than using a JButton he simply drew shapes on-screen.

Community
  • 1
  • 1
Austin
  • 8,018
  • 2
  • 31
  • 37
  • When I do that....it gives me an error saying "The method handle() is undefined for the type Arrangement"... – Samya Feb 15 '16 at 14:50
  • Make sure you save the file and check your spelling. – Austin Feb 15 '16 at 14:55
  • Nevermind.Yeah, I got it. Except for the image is still unable to display on the frame. But thanks for your help :) Appreciate it! – Samya Feb 15 '16 at 14:59
  • What error do you get? Also, I added a new link to my post – Austin Feb 15 '16 at 15:01
  • There is no error. Its just that if I create an instance of Arrangement, and follow on to use "this" keyword, all my buttons disappear and my frame is a blank...Oh and also thanks for the link! I will look into it rn! – Samya Feb 15 '16 at 15:17
1

use can use custom icons for your button as

jButton1.setIcon(new javax.swing.ImageIcon("C:\\Users\\admin\\Desktop\\image.jpg"));

if you want to set color to button then use following code

    jButton1.setBackground(Color.BLUE);
    jButton1.setForeground(Color.GREEN);

hope my code helps you in this regard.

SmashCode
  • 741
  • 1
  • 8
  • 14
  • When I try out the first suggestion it gives me a blank screen, and i am guessing the setBackground is supposed to set the blackground color and setForeground will change the color of the text written on the button, for that cause. What I am trying to do is change the color and shape of the whole button itself. Thanks for the help anyways... – Samya Feb 15 '16 at 14:39
  • then its better to use set icon method create your own .jpg files and place them on the button using above syntax that really works for me may be you should give it a try once i should arn you in this context that image dont make your imabe too big use appropriate sizes of images for the button hope this helps – SmashCode Feb 15 '16 at 18:00
  • Yeah! I resized the image! I am not sure what the problem was. But I ended up creating new shapes. But thanks for the input, :) I greatly appreciate it :D – Samya Feb 16 '16 at 02:57
1

Well, there are three issues with your code.The first one is already discussed. You can't reference non static variables from a static context.

this word is not static and main method is static

Now, to change background color, effectively you call the setBackground() method on the element you want the color to change.

Finally, to make the shape of the button you can follow this link: Making buttons round

Bassically what you are doing here is extending the jbutton class. Not allowing it to draw its own shape and use graphics to draw on it. You can use graphics to draw on every java GUI componnent. All you have to do is to implement de paint componnent method

By the way, here is a clearer and simpler way to display image icons.


One last comment, the key to make it easier to debug is trying to write clean and organized code. For example, I can see that you repeat the following code once and again:

c.gridx=0;
c.gridy=1;
p.add(b1,c);

What you can do is create a function to pack it

void pack(Insets target, int xCoord,int yCoord,Component comp ){
target.gridx=xCoord;
target.gridy=yCoord;
p.add(comp, target);
}

you can create an array with all the buttons too, so you can use a for loop to pack them:

JButton[] buttons= new JButton[8];

for(int j=0;j<16;j++){
    buttons[j]= new JButton("Button "+(j+1));
}

Packing of left buttons:

for(int j=0;j<8;j++){
    pack(c, 0, j+1,buttons[j+1]);
}

Thi is the final result

import javax.swing.*;
import java.awt.*;
import java.awt.*;
import java.awt.image.*;
public class Arrangement {



public void main(String[] args) {
    JFrame f= new JFrame();
    f.setVisible(true);
    f.setSize(600,400);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel p = new JPanel (new GridBagLayout());//in the constructor u specify the layout :)
    JPanel a = new JPanel (new GridBagLayout());
    Arrangement arr= new Arrangement();
    for(int j=0;j<16;j++){
       if(j==0)
        Image img= new ImageIcon(arr.getClass().getResource("/Red.png")).getImage();
//Supposing you followed the link and created the class
       buttons[j]= new RoundButton("Button "+(j+1));
    }

    GridBagConstraints c= new GridBagConstraints();
    GridBagConstraints d= new GridBagConstraints();

    c.insets = new Insets(5,5,5,5);//spacing

    for(int j=0;j<8;j++){
       arr.pack(c, 0, j+1,buttons[j],p);
    }
    d.insets = new Insets(5,5,5,5);

    for(int j=0;j<8;j++){
       arr.pack(d, 0, j+9,buttons[j+8],a);
    }


    f.add(p, BorderLayout.WEST);
    f.add(a, BorderLayout.EAST);

}
void pack(Insets target, int xCoord,int yCoord,Component comp, Panel container ){
target.gridx=xCoord;
target.gridy=yCoord;
container.add(comp, target);
}

}

Community
  • 1
  • 1
  • I have edited the code in regards with the first issue, it has been fixed! Thanks for the tagged links I will look into it! Also I am not trying to change the background colour, I am trying to change the colour of the buttons. Thanks tho :) – Samya Feb 15 '16 at 15:33
  • I get that, the thing is each componnent has a background color, so if you want to change the color of a button, you have to change it's background color – Absalon Castañon Feb 15 '16 at 15:54
  • Thank you so much :D! Yeah I was trying to create a for loops as well but kept messing it up :P Thanks for all the time you put in to solve my problem! I greatly appreciate it :) – Samya Feb 15 '16 at 17:27
  • This is what the community is about. I am sure when your turn comes you will help others as it's meant too – Absalon Castañon Feb 21 '16 at 15:03