java中的类详细!

来源:互联网 发布:淘宝小蜜投诉有用吗 编辑:程序博客网 时间:2024/04/30 17:49

 编写程序,通过按钮数组来管理界面中的所有按钮控件,从而使用最少的代码实现模拟的计算器界面。

  思路如下:

  创建一个类,通过extends使其继承窗体类JFrame;

  创建一个JFrame对象,使用JFrame类的setVisible()方法设置窗体可见;

  在构造函数中,使用super()方法继承父类的构造方法;

  使用setTitle()方法设置窗体的标题;

  使用setBounds()方法设置窗体的显示位置及大小;

  使用setDefaultCloseOperation()方法设置窗体关闭按钮的动作为退出;

  使用GridLayout创建网格布局管理器对象;

  使用GridLayout类的setHgap()方法设置组件的水平间距;

  使用GridLayout类的setVgap()方法设置组件的垂直间距;

  创建JPanel容器对象;

  通过JPanel类的setLayout()方法设置容器采用网格布局管理器;

  创建一个字符串型二维数组,初始化其值为计算器上对应按钮上显示的值;

  创建一个JButton型二维数组,并为其分配和之前的字符串型二维数组对应的空间;

  遍历字符串型二维数组,对它的每个元素都将其赋值给JButton型二维数组中的对应按钮,并对每个按钮添加事件,使得点击按钮时在文本输入框中显示对应的值,最后使用JPanel类的add()方法将按钮添加到面板中。

  代码如下:

  import java.awt.BorderLayout;

  import java.awt.Dimension;

  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.JPanel;

  import javax.swing.JTextField;

  import javax.swing.SwingConstants;

  import javax.swing.UIManager;

  public class ButtonArrayExample extends JFrame { // 继承窗体类JFrame

  /**

  *

  */

  private static final long serialVersionUID = 6626440733001287873L;

  private JTextField textField;

  public static void main(String args[]) {

  try {

  UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");

  } catch (Throwable e) {

  e.printStackTrace();

  }

  ButtonArrayExample frame = new ButtonArrayExample();

  frame.setVisible(true); // 设置窗体可见,默认为不可见

  }

  public ButtonArrayExample() {

  super(); // 继承父类的构造方法

  BorderLayout borderLayout = (BorderLayout) getContentPane().getLayout();

  borderLayout.setHgap(20);

  borderLayout.setVgap(10);

  setTitle("按钮数组实现计算器界面 "); // 设置窗体的标题

  setBounds(100, 100, 290, 282); // 设置窗体的显示位置及大小

  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置窗体关闭按钮的动作为退出

  textField = new JTextField();

  textField.setHorizontalAlignment(SwingConstants.TRAILING);

  textField.setPreferredSize(new Dimension(12, 50));

  getContentPane().add(textField, BorderLayout.NORTH);

  textField.setColumns(10);

  final GridLayout gridLayout = new GridLayout(4, 0); // 创建网格布局管理器对象

  gridLayout.setHgap(5); // 设置组件的水平间距

  gridLayout.setVgap(5); // 设置组件的垂直间距

  JPanel panel = new JPanel(); // 获得容器对象

  panel.setLayout(gridLayout); // 设置容器采用网格布局管理器

  getContentPane().add(panel, BorderLayout.CENTER);

  String[][] names = { { "1", "2", "3", "+" }, { "4", "5", "6", "-" }, { "7", "8", "9", "×" }, { ".", "0", "=", "÷" } };

  JButton[][] buttons = new JButton[4][4];

  for (int row = 0; row < names.length; row++) {

  for (int col = 0; col < names.length; col++) {

  buttons[row][col] = new JButton(names[row][col]); // 创建按钮对象

  buttons[row][col].addActionListener(new ActionListener() {

  @Override

  public void actionPerformed(ActionEvent e) {

  JButton button = (JButton) e.getSource();

  String text = textField.getText();

  textField.setText(text + button.getText());

  }

  });

  panel.add(buttons[row][col]); // 将按钮添加到面板中

  }

  }

  }

  }

0 0
原创粉丝点击