1

所以我希望我的第二个 JButton 打印第一个的反面。1-10 和 10-1。但我无法弄清楚我的第二个按钮中缺少什么。另外,我如何设置按钮的值,以便您可以多次按下它们?

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();}
    }
4

2 回答 2

1

有两个问题:

1)对于mybutton2您使用的usual应该是reverse.

2)即使在那之后,后面的大括号:

    if (source == mybutton)
       {

没有正确匹配,所以,mybutton2点击时没有显示任何内容。

更正的代码:

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();}
}

更新:根据要求改进给定的代码块:

只需在每次单击按钮后创建变量i并初始化:two

  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////
   }

     }
于 2013-01-23T14:32:00.543 回答
0

这是因为您使用了两个不同的 StringBuilder,但在这两种情况下您只显示usual.

JOptionPane.showMessageDialog(null,usual, ...

在第二次调用中,使用reverse.

于 2013-01-23T14:21:03.297 回答