• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Array Help

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do I create an array of buttons instead of each individual button? Anything else you see that I could make more effiecent, please let me know
package really.trying;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;

public class swingStuff implements ActionListener{
JButton red1 = new JButton("Red");
JButton black1 = new JButton("Black");
JButton blue1 = new JButton("Blue");
JFrame frame = new JFrame("Tester");
Container content = frame.getContentPane();
Color color1, color2;
public swingStuff(){
content.add(red1);
content.add(blue1);
content.add(black1);
LayoutManager flow1 = new FlowLayout(FlowLayout.RIGHT);
content.setLayout(flow1);
frame.setSize(300,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
red1.addActionListener(this);
blue1.addActionListener(this);
black1.addActionListener(this);

}
public void actionPerformed(ActionEvent ae){
if(ae.getSource() == red1){
color1 = new Color(255, 0, 0);
color2 = content.getBackground();
}
else if(ae.getSource() == blue1) {
color1 = new Color(0, 0, 255);
color2 = content.getBackground();
}
else if(ae.getSource() == black1) {
color2 = content.getBackground();
color1 = new Color(0, 0, 0);
}
content.setBackground(color1);
red1.setBackground(color2);
blue1.setBackground(color2);
black1.setBackground(color2);
}

public static void main(String args[]) {
swingStuff new1 = new swingStuff();
}

}
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how do you create an array of anything?

first, you declare what your array will hold, and how big it will be. then, you have to create each of the things going into it.

so, can you create an array of, say, Integers?
 
Jesse Walker
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes I think so,
int[3] = new int[1, 2, 3];
int[0] = 1
int[1] = 2
int[2] = 3

Right, but when I try to create an array of buttons I get the following error
Cannot resolve symbol and { expected

JButton [3] = new JButton["Red", "Blue", "Black"]

So what am I doing wrong?
 
Jesse Walker
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry my line of code should read
JButton[3] button1 = new JButton[ "Blue","Black","Red"];
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To declare and initialize an int array in a single line you would do this.
for an object array you must initialize the actual values usng the new keyword.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic