所以我希望我的第二个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();}
}
答案 0 :(得分: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();}
}
UPDATE:根据要求改进给定的代码块:
只需在每次点击按钮后初始化变量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////
}
}
答案 1 :(得分:0)
这是因为您使用了两个不同的StringBuilder,但在这两种情况下,您只显示usual
。
JOptionPane.showMessageDialog(null,usual, ...
在第二次调用中,使用reverse
。