}
}
try { Thread.sleep(100);
} catch (InterruptedException e) { e.printStackTrace(); }
taskThread.stopTask();
实现电话拨号器程序,界面如下:
设计要求如下:
设计一个MyTelephone类,该类从JFrame继承。 窗体的尺寸大小是:宽500,高300。
在主窗体的中央区域放入一个JPanel容器,该容器使用GridLayout布局,共有12个JButton按钮,各按钮排放位置如上图所示。
在主窗体的南方区域放入1个JLabel对象。 当点击按钮时,可以将点击的按钮所对应的数字填入JLabel中。比如,图中的“1338899999”就是依次点击“1”、“3”、“3”、“8”、“8”、“9”、“9”、“9”、“9”、“9”等按钮后显示的电话号码。
import java.awt.BorderLayout; import java.awt.GridLayout;
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel;
public class MyTelephone extends JFrame implements ActionListener { private JLabel numLabel = new JLabel(); public MyTelephone(){ this.setSize(500, 300); JPanel numPanel = new JPanel(); numPanel.setLayout(new GridLayout(4, 3)); JButton temp = null; for(int i = 1; i <= 9; i++){ temp = new JButton(\ temp.addActionListener(this); numPanel.add(temp); } temp = new JButton(\ temp.addActionListener(this); numPanel.add(temp); temp = new JButton(\ temp.addActionListener(this); numPanel.add(temp); temp = new JButton(\ temp.addActionListener(this); numPanel.add(temp); this.add(numPanel, BorderLayout.CENTER); this.add(numLabel, BorderLayout.SOUTH); }
}
@Override
public void actionPerformed(ActionEvent e) { numLabel.setText(numLabel.getText() + ((JButton)e.getSource()).getText()); }
public static void main(String[] args) { new MyTelephone().setVisible(true); }
相关推荐: