简体   繁体   English

为什么该JAVA GUI进程不会超过第一个条目?

[英]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. 今天,我们在课堂上汇总了一个基本的GUI,用于计算两点之间的距离。 Neither the instructor nor my classmates could figure out why this GUI won't process. 老师和我的同学都无法弄清楚为什么此GUI无法处理。 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. 插入打印语句并将label1的值设置为另一个值不会导致GUI更改。 Any help? 有什么帮助吗?

During creation of output1 you wrote setEnabled(false); 在创建output1您编写了setEnabled(false); . Possibly you meant that output1 controll should be disabled. 您可能是说应该禁用output1 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): PS似乎您还需要通过添加Math.sqrt来改进计算本身(如果我正确理解了您的想法):

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

@Mikita is right. @Mikita是正确的。 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 . 您还可以考虑在JFrame添加一个JPanel或至少将所有组件(JButton,JTextfields等)添加到JFrameContentPane中。

See the Using Top-Level Containers tutorial and this FlowLayout sample . 请参阅《 使用顶级容器》教程和此FlowLayout示例

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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