开发者

Java GUI assignment, no idea how to tackle it?

Sorry about this question but I have been struggling with an assignment my professor gave us for days and have no idea where to begin. I don't want someone to do it for me or anything, I am just looking to learn/get some good pointers because I can't find a foothold in this at all. The assignment is as follows :

Implement a graphical user interface with the GridLayout class with a 10 5 grid of JButtons and JLabels. The JButtons should be on the top five rows and the JLabels should be on the next five rows. (The first JButton should have the text 1-1 and the last the text 5-5.) The JButton on the i th row and j th column should have the text i - j on it. The text of the JLabels should be 0.

The purpose of the JLabels is to count the clicks of the corresponding JButtons. For example, when a user clicks button i - j for the first time, the text of the JLabel of the (5 + i )th row and j th column should change to 1.

You are not allowed to use any instance variables.

Hint 1: use and inner class for the labels.

Hint 2: you can increment the “number” the label by getting the text of the label, parsing it to an开发者_开发百科 int with Integer.parseInt( ), and by changing the text of the label. You must also add one more JButton which resets the counters on the JLabels. The text on the JButton should be reset.

So far I have just been studying notes with no understanding or cluelessly typing away and come up with a completely non-functioning desperate attempt which is as follows :

    import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class NewFrame extends JFrame {

 private static JButton[] buttons;

 public static void main ( String[] args ) {
  NewFrame frame = new NewFrame( ); 
 }

  public void NewFrame( ){
    JFrame frame = new JFrame ("JFrame");
    JPanel panel = new JPanel( );
    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
    int noOfButtons = 25;

    buttons = new JButton[ noOfButtons ];
    for(int i = 0; i<buttons.length ; i++){
      buttons[i] = new JButton(); 
      panel.add(buttons[i]);
      JLabel label = new JLabel( "Initial Text" );
    }
    frame.getContentPane( ).add( panel );
    frame.setSize( 500, 500);
    frame.setVisible( true );      
  } 

}

Can someone please offer me some advice or hints here as I have been struggling with this for an age ?


Please have a look at this link: So, You Need to Write a Program but Don't Know How to Start which will give you great tips on how to start. The key is to break down your big problem into small steps and then try to solve each step in isolation. Then if you get stuck at least you'll be able to post a much more specific question, one that is more easily answered with a specific and helpful answer. Another good resource are the Swing tutorials which can show you how to use the various Swing components including JFrames, JPanels, JButtons, text components, and what not. You can find them here: Using Swing Components

Best of luck!

Edit 1: Your code now compiles but there are two glaring issues that first need to be fixed: 1) Your class has no constructor but rather a "pseudo-constructor". Remember constructors have no return type, not even void. 2) You don't use a GridLayout.

Other issues: again the key is to break the problem down. I recommend you do just that on paper, and then type your version of the steps you think you need to solve the problem. Then we can go through them one at a time.


Generally, when you write a Swing application, your main class extends JFrame. You do all the initialization / create the buttons / etc in the constructor, then create an instance of the class in main()

-- edit --
see hovercraft's comment - you don't have to do the following in the constructor of an extended JFrame. Just change this to your JFrame variable if you do it externally.

The java documentation is your best friend - use it. http://download.oracle.com/javase/6/docs/api/

Create a GridLayout object, assign it to your content pane (this.getContentPane()) with setLayout

Create your buttons / labels, add those to the content pane

etc, etc

Look at the documentation for JButton.setActionCommand, JButton.addActionListener

You can access the labels later to increment / reset them with this.getContentPane().getComponents(), or one of the other access methods


Before I look at the code, you will surely need the Java tutorials on buttons and actions:

http://download.oracle.com/javase/tutorial/uiswing/components/button.html

http://download.oracle.com/javase/tutorial/uiswing/misc/action.html

The first issue with your code is that you use an instance variable to store the buttons in an array. You don't need this, since you are already adding them to the panel immediately.

The second issue is actually getting your buttons to do stuff when clicked. You need to add an ActionListener of your own design to each button, as follows:

myButton = new JButton();
myButton.addActionListener(new MyButtonListener());

and declare some MyButtonListener:

public class MyButtonListener implements ActionListener
{
}

the contents of the ActionListener class are beyond the scope of my help for your homework. But the Java tutorials are a great resource if you have no idea how to implement ActionListener.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜