This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Array Help  RSS feed

 
Jesse Walker
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • 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();
}

}
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
sorry my line of code should read
JButton[3] button1 = new JButton[ "Blue","Black","Red"];
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!