import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.BoxLayout; import java.awt.FlowLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; class Program01 extends JFrame { JPanel topPanel = new JPanel(); JPanel bottomPanel = new JPanel(); JLabel taskLabel = new JLabel("Háromszög területszámítás"); JLabel baseLabel = new JLabel("Alap"); JLabel heightLabel = new JLabel("Magasság"); JLabel resultLabel = new JLabel("Eredmény"); JTextField baseField = new JTextField(10); JTextField heightField = new JTextField(10); JTextField resultField = new JTextField(10); JButton calcButton = new JButton("Számít"); Program01() { calcButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { calcButtonActionListener(event); } }); resultLabel.setVisible(false); resultField.setVisible(false); topPanel.add(taskLabel); bottomPanel.setLayout(new FlowLayout()); bottomPanel.add(baseLabel); bottomPanel.add(baseField); bottomPanel.add(heightLabel); bottomPanel.add(heightField); bottomPanel.add(resultLabel); bottomPanel.add(resultField); bottomPanel.add(calcButton); setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS)); add(topPanel); add(bottomPanel); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); centerWindow(this); setVisible(true); } public void calcButtonActionListener(ActionEvent event) { String baseStr = baseField.getText(); String heightStr = heightField.getText(); double base = Double.parseDouble(baseStr); double height = Double.parseDouble(heightStr); Double result = calcArea(base, height); resultField.setText(result.toString()); resultLabel.setVisible(true); resultField.setVisible(true); pack(); } public double calcArea(double base, double height) { if(base<=0 || height<=0) { throw new IllegalArgumentException(); } return (base * height) / 2.0; } public static void centerWindow(java.awt.Window frame) { java.awt.Dimension dimension = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); int x = (int)((dimension.getWidth() - frame.getWidth()) / 2); int y = (int)((dimension.getHeight() - frame.getHeight()) / 2); frame.setLocation(x, y); } public static void main(String[]args) { new Program01(); } }