Results 1 to 9 of 9
- 04-29-2011, 06:29 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 13
- Rep Power
- 0
What am I doing wrong here? (JButton issue)
I've been trying to troubleshoot this and I cannot seem to figure out what I am doing wrong. There are only two errors (that I know of) in this code. I can't seem to understand why button.length in "for (int i = 0; i <=button.length; i++)" is underlined as an error and why the line after that "button = new JButton(button[i]);" is also an error.
What do you think I should do? I am trying to create a Gridbag layout and that is the method that I am using for it.
Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; public class MyWindow { public static void main(String[] arg) { MyJFrame f = new MyJFrame(Constants.TITLE); JButton b; GridBagConstraints c = new GridBagConstraints(); Toolkit toolkit = f.getToolkit(); Dimension size = toolkit.getScreenSize(); Rectangle r = new Rectangle(Constants.X_POS, Constants.Y_POS, size.width/Constants.X_FACTOR, size.height/Constants.Y_FACTOR); f.setBounds(r); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { JOptionPane.showMessageDialog(null, "The Window is closing"); } }); Container e = f.getContentPane(); //>>>>>This is where I am having the problem<<<<<< for (int i = 0; i <= button.length; i++) { button = new JButton(button[i]); switch(i) { case 0: b = new JButton("Write"); c.gridx = 0; // Specify the (x,y) coordinate for this component c.gridy = 0; // (x,y) = (0,0) c.insets = new Insets(0,50,0,0); break; case 1: b = new JButton("Draw"); c.gridx = 1; c.gridy = 0; c.insets = new Insets(20,0,40,200); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { MyGraphics myGraphics = new MyGraphics(); } } ); break; case 2: b = new JButton("Image", new ImageIcon()); c.gridx = 0; c.gridy = 1; c.insets = new Insets(20,300,0,0); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Browser br = new Browser(); } } ) ; break; case 3: b = new JButton("Print"); c.gridx = 0; c.gridy = 2; c.ipadx = 200; c.insets = new Insets(25,50,0,50); break; } e.setCursor( Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); e.setBackground(Color.white); e.setLayout(new GridBagLayout()); c.fill = GridBagConstraints.HORIZONTAL; f.setVisible(true); } } }
- 04-29-2011, 06:39 PM #2
I don't know what you're trying to do.
There is no length variable in the JButton class, and you can't use the index[] operator on something that isn't an array. And there is no JButton constructor that takes a JButton as an argument.How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 04-29-2011, 06:52 PM #3
Member
- Join Date
- Mar 2010
- Posts
- 13
- Rep Power
- 0
I'm an idiot, ignore the second error. I changed the index operator everywhere but there and didn't realize it.
This is just one class from a class of 10. In this class, I am attempting to create buttons in the JFrame and arrange them in a gridbag layout (I haven't put the specific coordinates yet). My teacher instructed me to do it this way although I am a little confused by his way. He had provided the "for (int i = 0; i <= button.length; i++)" and told me that I should do the rest (which I did). I'm getting the red error line under the "button" and not length. It says that it cannot find the symbol variable button. I'm just really confused. This one little error is stopping my entire application from running.
- 04-29-2011, 06:55 PM #4
Well, what is button supposed to be? An array? Where do you declare it? I see you have a JButton b, but not an array of JButtons.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 04-29-2011, 07:04 PM #5
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
The variable 'button' isn't defined, and if it was, this line thinks it'a both an array and a JButton, which doesn't make sense:
Java Code:button = new JButton(button[i])
- 04-29-2011, 07:30 PM #6
Member
- Join Date
- Mar 2010
- Posts
- 13
- Rep Power
- 0
Ok. So I changed a few things around. Still getting an error but I think its a step in the right direction.
I changed
for (int i = 0; i <= b.length; i++)
{
b = new JButton("");
EDIT:
Not supposed to be an array. Just trying to create a gridbag layout with those various buttons in it.Last edited by fsuarjun03; 04-29-2011 at 07:36 PM.
- 04-29-2011, 07:37 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
- 04-29-2011, 07:54 PM #8
Member
- Join Date
- Mar 2010
- Posts
- 13
- Rep Power
- 0
What would you guys do if you were in my position? How would you correct my mistake? I'm getting frustrated at myself because this one little error is preventing my entire project from running.
- 04-29-2011, 10:18 PM #9
Honestly, this is a pretty weird way to go about setting up 4 JButtons. When are you adding them to anything? I would ditch the for loop completely, and work on them one at a time.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
Similar Threads
-
Wrong output (well.. the one who's wrong is probably me ;) )
By shacht1 in forum New To JavaReplies: 4Last Post: 06-11-2013, 01:37 AM -
JButton with Icon & text alignment issue
By ShardaD in forum AWT / SwingReplies: 4Last Post: 01-11-2011, 11:10 AM -
JButton issue
By globo in forum New To JavaReplies: 16Last Post: 11-14-2010, 02:35 AM -
Issue with obtaining data from the JTextField when JButton is pressed
By turc0033 in forum AWT / SwingReplies: 7Last Post: 08-29-2010, 10:33 AM -
Lo4j issue - logs are written in a wrong file
By Dhamo in forum Advanced JavaReplies: 0Last Post: 05-09-2010, 09:11 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks