简体   繁体   中英

Why won't this JAVA GUI process past the first entry?

In class today we put together a basic GUI for calculating the distance between two points. Neither the instructor nor my classmates could figure out why this GUI won't process. The code given to us was a framework and we just edited the old code to develop this. I compared my work to that of two other students and theirs worked while mine didn't.

import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Graphics;
import java.text.DecimalFormat;
import java.awt.Color;
import java.awt.Font;
import java.lang.Math;
public class D2 extends JFrame
{
//****************************************
//** GUI Structure
//** Title: Holston Middle School
//** Weight prompt (jlabel, jtext)
//** planet pulldown
//** Weight on planet (jlabel, jtext)
//** calculate button
//****************************************
public JTextField entry1, entry2, entry3, entry4, output1;
public JLabel label1, label2, label3, label4, label5;
public JButton CalculateButton;
public String mtitle, cmessage;

public D2()
{
    setLayout(new FlowLayout(FlowLayout.LEFT, 10, 20));

    label1 = new JLabel("x1");
    add(label1);
    entry1 = new JTextField(8);
    add(entry1);
    setEnabled(true);
    setVisible(true);

    label2 = new JLabel("x2");
    add(label2);
    entry2 = new JTextField(8);
    add(entry2);
    setEnabled(true);
    setVisible(true);

    label3 = new JLabel("y1");
    add(label3);
    entry3 = new JTextField(8);
    add(entry3);
    setEnabled(true);
    setVisible(true);

    label4 = new JLabel("y2");
    add(label4);
    entry4 = new JTextField(8);
    add(entry4);
    setEnabled(true);
    setVisible(true);

    label5 = new JLabel("Distance");
    add(label5);
    output1 = new JTextField(8);
    add(output1);
    setEnabled(false);
    setVisible(true);
    CalculateButton = new JButton("Calculate");
    add(CalculateButton);
    CalculateButton.addActionListener( new ActionListener()
    {
        public void actionPerformed(ActionEvent event)
        {
            String entry1string = entry1.getText();
            Double e1value = Double.valueOf(entry1string);
            String entry2string = entry2.getText();
            Double e2value = Double.valueOf(entry2string);
            String entry3string = entry3.getText();
            Double e3value = Double.valueOf(entry3string);
            String entry4string = entry4.getText();
            Double e4value = Double.valueOf(entry4string);
            String wmessage = "You selected ";
            String wtitle = "Pop Up Box";
            if (true) JOptionPane.showMessageDialog(null, wmessage, wtitle, JOptionPane.PLAIN_MESSAGE);
            double distance = (Math.pow((e1value - e2value), 2) + Math.pow((e3value - e4value), 2));
            DecimalFormat fmt = new DecimalFormat("####.##");
            String outstring = fmt.format(distance);
            output1.setText("");
            output1.setText(outstring);
        }//** actionPerformed
    }); //** Action Listener
    } //** D2 constructor

    public static void main(String[] args)
    {
        D2 frame = new D2();
        frame.setTitle("Distance Calculator");
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 200);
        frame.setBackground(Color.CYAN);
        frame.getContentPane().setBackground(Color.lightGray);
        frame.setVisible(true);
    } //** main
} //** class

Inserting print statements and setting the value of label1 to another value results in no change in the GUI. Any help?

During creation of output1 you wrote setEnabled(false); . Possibly you meant that output1 controll should be disabled. But instead of that you disabled the whole container, thus all elements are not editable\\clickable. To fix the behaviour set this property directly to control:

output1 = new JTextField(8);
add(output1);
output1.setEnabled(false);
output1.setVisible(true);

Hope this will help.

PS It seems that you also need to improve calculation itself by adding Math.sqrt (if I understood your idea correctly):

double distance = Math.sqrt(Math.pow((e1value - e2value), 2) + Math.pow((e3value - e4value), 2));

@Mikita is right. You could also consider adding a JPanel in your JFrame or at least add all your components (JButtons, JTextfields etc) into the ContentPane of the JFrame .

See the Using Top-Level Containers tutorial and this FlowLayout sample .

getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT, 10, 20));
label1 = new JLabel("x1");
getContentPane().add(label1);
entry1 = new JTextField(8);
getContentPane().add(entry1);

or

JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 20));
label1 = new JLabel("x1");
panel.add(label1);
entry1 = new JTextField(8);
panel.add(entry1);
getContentPane().add(panel);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM