1
當按下「1-10」按鈕時,我想從我的while循環中獲取整個輸出,而不必單擊每個數字顯示的「確定」按鈕。在JFrame中輸出整個循環
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();}
}
謝謝你我的好先生! – Krappington