More Java Fun!

QuaunautQuaunaut The longest seven days in history... Join Date: 2003-03-21 Member: 14759Members, Constellation, Reinforced - Shadow
edited April 2005 in Off-Topic
<div class="IPBDescription">Books suck.</div> I'm on my 2nd book here. Out of both, niether of you show you how to properly make a button: Sure, I can make it and put it there, but I can't have each button do it's own thing- it doesn't show me at all how to even tell it what the hell to do!

I'm wanting to use either the AWT package or Swing package, preferably through java's built in way to make a button. But if you have a alternate way(say, to hardcode a button) that'd be nice too.

<span style='color:red'>Edit: Hell, while were at it, howabout someone reccommends a worthwhile java book?</span>

Comments

  • RPG_JssmfulhudRPG_Jssmfulhud Join Date: 2002-11-02 Member: 4006Members
    edited April 2005
    I hope I can still remember it correctly. Otherwise, I'm not responsible for any computer explosions that the faulty JFrame might cause.

    In your JFrame, you have a button, right? Something like:
    <!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->  JPanel buttonPanel = new JPanel();
     JButton okButton = new JButton("Ok");

     buttonPanel.add(okButton);
     okButton.addActionListener(new ButtonListenerThingy());<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->

    And then you need another class to tell the listener what to do, when he "hears" the button being pressed. Something like:
    <!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
    class ButtonListenerThingy implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
     System.out.println("[OK]");
    }
    }
    <!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
    Of course, instead of the print, you can make the system exit or whatever. <!--emo&:)--><img src='http://www.unknownworlds.com/forums/html/emoticons/smile-fix.gif' border='0' style='vertical-align:middle' alt='smile-fix.gif' /><!--endemo-->
  • SnidelySnidely Join Date: 2003-02-04 Member: 13098Members
    I like the Black Book of Java (which I affectionately call the Java Bible). It's fairly comprehensive, and user-friendly. I find it better than <i>Java: How To Program</i> (which I gather you've been using from previous posts, IIRC).

    I googled it, and found the details <a href='http://www.isbn.nu/1576105318' target='_blank'>here</a>. I'd try to get it second hand, though. Shouldn't be too hard, with any luck.
  • QuaunautQuaunaut The longest seven days in history... Join Date: 2003-03-21 Member: 14759Members, Constellation, Reinforced - Shadow
    edited April 2005
    <!--QuoteBegin-RPG Jssmfulhud+Apr 5 2005, 08:16 AM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (RPG Jssmfulhud @ Apr 5 2005, 08:16 AM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> I hope I can still remember it correctly. Otherwise, I'm not responsible for any computer explosions that the faulty JFrame might cause.

    In your JFrame, you have a button, right? Something like:
    <!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->  JPanel buttonPanel = new JPanel();
     JButton okButton = new JButton("Ok");

     buttonPanel.add(okButton);
     okButton.addActionListener(new ButtonListenerThingy());<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->

    And then you need another class to tell the listener what to do, when he "hears" the button being pressed. Something like:
    <!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
    class ButtonListenerThingy implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
     System.out.println("[OK]");
    }
    }
    <!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
    Of course, instead of the print, you can make the system exit or whatever. <!--emo&:)--><img src='http://www.unknownworlds.com/forums/html/emoticons/smile-fix.gif' border='0' style='vertical-align:middle' alt='smile-fix.gif' /><!--endemo--> <!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd-->
    But how would I specify one button from another with that?

    Edit: Oh, wait a sec. I see. Lemme see if that works for me o.o
  • SnidelySnidely Join Date: 2003-02-04 Member: 13098Members
    edited April 2005
    There's a method you use to distinguish buttons.

    Also, when using addActionListener, just use <span style='color:blue'>this</span>.

    <!--QuoteBegin--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> </td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->JPanel buttonPanel = new JPanel();
    JButton buttonOne = new JButton("One");
    JButton buttonTwo = new JButton("Two");
    JLabel result = new JLabel("No button has been pressed.");

    buttonPanel.add(buttonOne);
    buttonPanel.add(buttonTwo);
    buttonPanel.add(result);
    buttonOne.addActionListener(this);
    buttonTwo.addActionListener(this);

    ...

    class ButtonListenerThingy implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
      <b> if (e.getSource() == buttonOne)</b>
        {
          result.setText("One.");
        }
      <b>  else if (e.getSource() == buttonTwo)</b>
        {
          result.setText("Two.");
        }
    }
    }
    <!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd-->

    The "else if" isn't strictly necessary, it just makes it clearer.

    <span style='color:red'>IMPORTANT EDIT: I missed out the brackets on the "getSource()" parts. My bad.

    Also note that you call the method on whatever ActionEvent you used <i>(public void actionPerformed(ActionEvent e){...}).</i> In this case it's "e", but it doesn't have to be (I usually use ae if I'm using other listeners in the same program).</span>
  • CForresterCForrester P0rk(h0p Join Date: 2002-10-05 Member: 1439Members, Constellation
    All of that just for two buttons? Geez.
  • SnidelySnidely Join Date: 2003-02-04 Member: 13098Members
    To get the buttons to appear, know when they're clicked and do something, yes. If you want a button that doesn't appear and does nothing, it's a lot easier. (:
  • QuaunautQuaunaut The longest seven days in history... Join Date: 2003-03-21 Member: 14759Members, Constellation, Reinforced - Shadow
    Snidely, can I see the format you would personally do this? AKA, could you write a program using this code, that I could run instantly? I'm having trouble setting it up.
  • KungFuDiscoMonkeyKungFuDiscoMonkey Creator of ns_altair 日本福岡県 Join Date: 2003-03-15 Member: 14555Members, NS1 Playtester, Reinforced - Onos
    edited April 2005
    This is the way I implment actions in java

    <!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
         jbStart = new JButton("Start");
         jbStart.addMouseListener(
         new MouseAdapter()
         {
           public void mousePressed( MouseEvent event)
           {
             jbStart_Click();
             //jbStart_Click() is a method I make to make the code
             // easier to read since I move all the actions to be performed
            // on a click event into it's own method by creating an inner class
            //when I add the listener.
           }
         }
         ); // Closing paren for jbStart.addMouseListener()
    <!--c2--></td></tr></table><div class='postcolor'><!--ec2-->

    Keeps it nice and neat and much more OO than just checking for each event in a different area of the code.
  • QuaunautQuaunaut The longest seven days in history... Join Date: 2003-03-21 Member: 14759Members, Constellation, Reinforced - Shadow
    <!--QuoteBegin-KungFuDiscoMonkey+Apr 5 2005, 09:34 AM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (KungFuDiscoMonkey @ Apr 5 2005, 09:34 AM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> This is the way I implment actions in java

    <!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
         jbStart = new JButton("Start");
         jbStart.addMouseListener(
         new MouseAdapter()
         {
           public void mousePressed( MouseEvent event)
           {
             jbStart_Click();
             //jbStart_Click() is a method I make to make the code
             // easier to read since I move all the actions to be performed
            // on a click event into it's own method by creating an inner class
            //when I add the listener.
           }
         }
         ); // Closing paren for jbStart.addMouseListener()
    <!--c2--></td></tr></table><div class='postcolor'><!--ec2-->

    Keeps it nice and neat and much more OO than just checking for each event in a different area of the code. <!--QuoteEnd--> </td></tr></table><div class='postcolor'> <!--QuoteEEnd-->
    While I see this, I'm unsure of how to use it.

    Sorry for my nubbishness. <!--emo&:(--><img src='http://www.unknownworlds.com/forums/html/emoticons/sad-fix.gif' border='0' style='vertical-align:middle' alt='sad-fix.gif' /><!--endemo-->
  • KungFuDiscoMonkeyKungFuDiscoMonkey Creator of ns_altair 日本福岡県 Join Date: 2003-03-15 Member: 14555Members, NS1 Playtester, Reinforced - Onos
    First we make a new button.

    Then you're adding a Mouse Listener

    Then we're going to make a new mouse adapter inside that method.

    The line where it calls jbStart_Click(); is just what I do to call a method where I store everything that I want to execute when the mouse event Clicks. Then you just have to make sure all the brackets and parens close properly.
  • SnidelySnidely Join Date: 2003-02-04 Member: 13098Members
    edited April 2005
    Here we go.

    <!--QuoteBegin--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> </td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class ButtonFun extends JFrame implements ActionListener
    {
    private JPanel zePanel;
    private JButton buttonOne, buttonTwo;
    private JLabel result;
    private Container c;

    <span style='color:green'>/* zePanel is where we will put all the objects.
    result will display text showing the last button pressed.
    The container © stores the panel.*/</span>

    public ButtonFun()
    {
      zePanel = new JPanel();
      buttonOne = new JButton("One");
      buttonTwo = new JButton("Two");
      result = new JLabel("No button has been clicked.");
      c = getContentPane();

    <span style='color:green'>// Declarations.</span>

      zePanel.add(buttonOne);
      zePanel.add(buttonTwo);
      zePanel.add(result);

    <span style='color:green'>// The above adds the objects to zePanel.</span>

      buttonOne.addActionListener(this);
      buttonTwo.addActionListener(this);

    <span style='color:green'>/* actionListeners tell the program when a button has been pressed.
    Without these, nothing will happen when we press the buttons, as the program
    can't "hear" them.*/</span>

      c.add(zePanel);

    <span style='color:green'>/* This line adds zePanel to the container. Without out it,
    zePanel will not appear - and neither will anything inside it!

    The folowing method describes what happens when a button is heard.
    If you don't specify an "if...getSource(){}", then the code will happen
    no matter which button you press. For example,

    public void actionPerformed(ActionEvent action)
    {
      result.setText("One.");
    }

    would cause result to display "One.", no matter which button you pressed.*/</span>

    } <span style='color:green'>//end init.</span>

    public void actionPerformed(ActionEvent action)
    {
      if (action.getSource() == buttonOne)
      {
      result.setText("One.");
      }

    <span style='color:green'>// Button one is pressed. It sets result to say "One."</span>

      else if (action.getSource() == buttonTwo)
      {
      result.setText("Two.");
      }
    <span style='color:green'>// Same, but says "Two."</span>

    } <span style='color:green'>// End ActionListener</span>

    public static void main(String[] args)
    {
      ButtonFun app = new ButtonFun();
      app.setVisible(true); <span style='color:green'>//Otherwise, won't appear!</span>
      app.pack(); <span style='color:green'>//Sets default size (juuuuusssst big enough)</span>

    } <span style='color:green'>// End main</span>

    } <span style='color:green'>//End program</span><!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd-->
    Editing for ease of reading. Green will be comments.

    Here's what it looks like.

    <a href='http://img121.exs.cx/my.php?loc=img121ℑ=buttonfun4yw.jpg' target='_blank'><img src='http://img121.exs.cx/img121/4861/buttonfun4yw.th.jpg' border='0' alt='user posted image' /></a>

    (Hurrah for Imageshack.)
  • QuaunautQuaunaut The longest seven days in history... Join Date: 2003-03-21 Member: 14759Members, Constellation, Reinforced - Shadow
  • SnidelySnidely Join Date: 2003-02-04 Member: 13098Members
    "The Java Programmers are sticking together."
    "Sticking together is what good Java Programmers do."
Sign In or Register to comment.