当前位置: 代码迷 >> Java相关 >> 用java编写一个图形化界面应用程序,该如何解决
  详细解决方案

用java编写一个图形化界面应用程序,该如何解决

热度:7051   发布时间:2013-02-25 21:52:16.0
用java编写一个图形化界面应用程序
本人java入门级菜鸟,想求教各位大神如何用java编写一个图形化界面应用程序,用来计算三角形面积,由用户输入三遍长,若按空格则三边数值清空,按回车确定光标跳至下一条边输入框,三边输完即可出现三角形面积及缩略图,求赐教。。。

------解决方案--------------------------------------------------------
Java code
import java.awt.GridLayout;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JTextField;import javax.swing.SwingConstants;public class MainFrame extends JFrame {    private JTextField length1;    private JTextField length2;    private JTextField length3;    private JTextField array[];    public MainFrame() {        init();    }    private void init() {        length1 = createTextField("1");        length2 = createTextField("2");        length3 = createTextField("3");        array = new JTextField[3];        array[0] = length1;        array[1] = length2;        array[2] = length3;        this.setLayout(new GridLayout(3, 2));        this.add(new JLabel("边长A", SwingConstants.CENTER));        this.add(length1);        this.add(new JLabel("边长B", SwingConstants.CENTER));        this.add(length2);        this.add(new JLabel("边长C", SwingConstants.CENTER));        this.add(length3);        this.setSize(200, 150);        this.setTitle("计算三角形面积");        this.setVisible(true);        this.setDefaultCloseOperation(EXIT_ON_CLOSE);    }    private JTextField createTextField(String name) {        final JTextField t = new JTextField(6);        t.setName(name);        t.addKeyListener(new KeyListener() {            @Override            public void keyTyped(KeyEvent arg0) {            }            @Override            public void keyReleased(KeyEvent arg0) {            }            @Override            public void keyPressed(KeyEvent arg0) {                if (arg0.getKeyCode() == KeyEvent.VK_SPACE) {                    clear();                    length1.grabFocus();                } else if (arg0.getKeyCode() == KeyEvent.VK_ENTER) {                    if (check()) {                        JOptionPane                                .showMessageDialog(MainFrame.this, getArea());                    } else {                        JOptionPane.showMessageDialog(MainFrame.this,                                "请完整填写数据或者三条边无法构成三角形!");                    }                }            }        });        return t;    }    private Object getArea() {        return 123;    }    private void clear() {        length1.setText("");        length2.setText("");        length3.setText("");    }    private boolean check() {        for (JTextField t : array) {            if (!t.getText().matches("^[0-9]+$")) {                return false;            }        }        int x = Integer.parseInt(length1.getText());        int y = Integer.parseInt(length2.getText());        int z = Integer.parseInt(length3.getText());        if (x + y < z || x + z < y || y + z < x)            return false;        return true;    }    public static void main(String[] args) {        new MainFrame();    }}
  相关解决方案