java输出JFrame中的整个循环
当按下“1-10”按钮时,我想从while循环中获得整个输出,而不必单击“OK”按钮来显示每个数字
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();}
}
# 1 楼答案
我认为您要做的是在while循环中构建一个字符串(或者更喜欢StringBuilder),然后在循环完成后输出一次。比如:
这至少会让你更亲近
请注意,如果希望消息对话框是模态的,请将“this”作为第一个参数(而不是null)传递给showMessageDialog