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();}
}
There were Two problems:
1)For
mybutton2you were usingusualshould bereverse.2)Even after that,the braces after :
were not correctly matched,so,nothing was displayable on
mybutton2click.Corrected code:
UPDATE: As requested to improve the given block of code:
Just make your variables
iandtwoinitialize after every button click: