Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9186375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:30:42+00:00 2026-06-17T19:30:42+00:00

so i want my second JButton to print the reverse of the first one.

  • 0

so i want my second JButton to print the reverse of the first one.
1-10 and 10-1.
But i cant figure out whats missing in my second button.
Also, how do i setback the value of the buttons so u can press them more than once?

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

    public class Testgui1 extends JFrame implements ActionListener //Alist visar vad
    //som görs när man utför ett klick
    {
        int i = 1;
        int two = 11;
        JLabel myLabel = new JLabel();//Ny panelen
        JPanel mypanel = new JPanel();
        JButton mybutton = new JButton("1-10");
        JButton mybutton2 = new JButton("10-1");
        Testgui1()
        {
            super("Meny");
            setSize(200,200);//Storlek på frame
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//stänger ner rutan vid X
            Container con = this.getContentPane();//ärver mainframe
            con.add(mypanel); 
            mybutton.addActionListener(this);
            mybutton2.addActionListener(this);
            mypanel.add(myLabel); 
            mypanel.add(mybutton);
            mypanel.add(mybutton2);
            setVisible(true);
        }
        public void actionPerformed(ActionEvent event)
        {  
           Object source = event.getSource();
            if (source == mybutton)
           {
            StringBuilder usual = new StringBuilder();
            while(i < 11) {
            usual.append(" ").append(i);
                i++;
    }
            JOptionPane.showMessageDialog(null, usual, "1-10", 
                 JOptionPane.PLAIN_MESSAGE);
                     setVisible(true);
         {
         if (source == mybutton2)
            {
        StringBuilder reverse = new StringBuilder(); 
            while (two > 0) {
            reverse.append("").append(two);
            two--;
    }
            JOptionPane.showMessageDialog(null,usual,"10-1",
                    JOptionPane.PLAIN_MESSAGE);
                    setVisible(true);
    }}}}
      public static void main(String[] args) {new Testgui1();}
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T19:30:44+00:00Added an answer on June 17, 2026 at 7:30 pm

    There were Two problems:

    1)For mybutton2 you were using usual should be reverse.

    2)Even after that,the braces after :

        if (source == mybutton)
           {
    

    were not correctly matched,so,nothing was displayable on mybutton2 click.

    Corrected code:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class Testgui1 extends JFrame implements ActionListener //Alist visar vad
    //som görs när man utför ett klick
    {
        int i = 1;
        int two = 11;
        JLabel myLabel = new JLabel();//Ny panelen
        JPanel mypanel = new JPanel();
        JButton mybutton = new JButton("1-10");
        JButton mybutton2 = new JButton("10-1");
        Testgui1()
        {
            super("Meny");
            setSize(200,200);//Storlek på frame
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//stänger ner rutan vid X
            Container con = this.getContentPane();//ärver mainframe
            con.add(mypanel);
            mybutton.addActionListener(this);
            mybutton2.addActionListener(this);
            mypanel.add(myLabel);
            mypanel.add(mybutton);
            mypanel.add(mybutton2);
            setVisible(true);
        }
        public void actionPerformed(ActionEvent event)
        {
           Object source = event.getSource();
            if (source == mybutton)
           {
            StringBuilder usual = new StringBuilder();
            while(i < 11) {
            usual.append(" ").append(i);
                i++;
                         }
            JOptionPane.showMessageDialog(null, usual, "1-10",
                 JOptionPane.PLAIN_MESSAGE);
                     setVisible(true);}
         {
         if (source == mybutton2)
            {
        StringBuilder reverse = new StringBuilder();
            while (two > 0) {
            reverse.append(" ").append(two);
            two--;
    }
            JOptionPane.showMessageDialog(null,reverse,"10-1",
                    JOptionPane.PLAIN_MESSAGE);
                    setVisible(true);
    }}}
      public static void main(String[] args) {new Testgui1();}
    }
    

    UPDATE: As requested to improve the given block of code:

    Just make your variables i and two initialize after every button click:

      if (source == mybutton)
           {
            StringBuilder usual = new StringBuilder();
            while(i < 11) {
            usual.append(" ").append(i);
                i++;
                         }
            JOptionPane.showMessageDialog(null, usual, "1-10",
                 JOptionPane.PLAIN_MESSAGE);
                     setVisible(true);
            i=1;//////////////////MAKE i=1 EVERYTIME////////////////
            }
         {
         if (source == mybutton2)
            {
        StringBuilder reverse = new StringBuilder();
            while (two > 0) {
            reverse.append(" ").append(two);
            two--;
       }
            JOptionPane.showMessageDialog(null,reverse,"10-1",
                    JOptionPane.PLAIN_MESSAGE);
                    setVisible(true);two=11;/////////////////MAKE two=11 EVERYTIME////
       }
    
         }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Two input boxes: when I set the first one, I want the second one
I want to loop a recorded sound 3 times, but want one second of
I'm calling some JavaScript from within my PHP code, but I want the second
I want to get 'second' in the following sample web address. http://www.mywebsite.com/first/sedond/third.html first can
I have different select boxes. I want to select second one with related id.
I have cross-tab and i want put second cross-tab to right side, but i
I have two forms on the same page, but I want the second form
How to unpack() the first structure in this list ? I want the second
I have two forms. First one is to decide numbers of button by using
I want to get one jbutton when I click another jbutton. Here the link

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.