当按下“1-10”按钮时,我想从我的while循环中获取整个输出,而不必单击“确定”按钮来显示每个数字。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Testgui1 extends JFrame implements ActionListener
{
int i = 1;
JLabel myLabel = new JLabel();
JPanel mypanel = new JPanel();
JButton mybutton = new JButton("1-10");
Testgui1()
{
super("Meny");
setSize(200,200);//Storlek på frame
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container con = this.getContentPane();
con.add(mypanel);
mybutton.addActionListener(this);
mypanel.add(myLabel); mypanel.add(mybutton);
setVisible(true);
}
public void actionPerformed(ActionEvent event)
{
// Object source = event.getSource();
//if (source == mybutton)
{
while (i < 11){
System.out.print(+i);
{
JOptionPane.showMessageDialog(null,i,"1-10",
JOptionPane.PLAIN_MESSAGE);
setVisible(true);
++i;
}
}
}
}
public static void main(String[] args) {new Testgui1();}
}