0

好的,当我运行它时,它工作得很好,但它只是没有弹出答案。

我做错了什么?

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class AF implements ActionListener{

    double length;
    double width;
    double answer;

    JTextField twidth;
    JTextField tlength;

    void AFWindow() {

        JFrame AFwindow = new JFrame();
        JPanel pan2 = new JPanel(new GridBagLayout());
        AFwindow.setVisible(true);
        AFwindow.setSize(250, 150);
        AFwindow.setResizable(false);
        GridBagConstraints c = new GridBagConstraints();
        AFwindow.add(pan2);
        pan2.setBackground(Color.LIGHT_GRAY);

        c.gridx = 0;
        c.gridy = 5;
        tlength = new JTextField();
        tlength.setText("    Length    ");
        pan2.add(tlength, c);

        c.gridx = 0;
        c.gridy = 0;
        twidth = new JTextField();
        twidth.setText("     Width     ");
        pan2.add(twidth, c);

        JButton Find = new JButton("Ok");

        c.gridx = 0;
        c.gridy = -5;
        pan2.add(Find);
        Find.addActionListener(this);
        Find.setActionCommand("ok");
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equalsIgnoreCase("ok")){
            try {
            this.length = Double.parseDouble(tlength.getText());
            this.width = Double.parseDouble(twidth.getText());
        }
        catch(NumberFormatException ex){
            System.out.println("There was an issue!");
        }
        }
    }

    int area = (int) (length * width);
    public void answer(){
        JFrame answer = new JFrame();
        answer.setVisible(true);
        answer.setBackground(Color.yellow);
        JPanel pan2 = new JPanel();
        JLabel howanswer = new JLabel("Your answer is" + area + "We got this by multiplying the length and width");
        pan2.add(howanswer);
    }
}
4

3 回答 3

2

该代码有多个问题。忘记记录它们,但请查看此代码以了解修复的开始。

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class AF implements ActionListener{

    double length;
    double width;

    JTextField twidth;
    JTextField tlength;
    JFrame AFwindow;

    void AFWindow() {

        AFwindow = new JFrame();
        JPanel pan2 = new JPanel(new GridBagLayout());

        GridBagConstraints c = new GridBagConstraints();
        AFwindow.add(pan2);
        pan2.setBackground(Color.LIGHT_GRAY);
        pan2.setBorder(new EmptyBorder(100,100,100,100));

        c.gridx = 0;
        c.gridy = 5;
        tlength = new JTextField();
        tlength.setText("    Length    ");
        pan2.add(tlength, c);

        c.gridx = 0;
        c.gridy = 0;
        twidth = new JTextField();
        twidth.setText("     Width     ");
        pan2.add(twidth, c);

        JButton Find = new JButton("Ok");

        c.gridx = 0;
        c.gridy = -5;
        pan2.add(Find);
        Find.addActionListener(this);
        Find.setActionCommand("ok");
        AFwindow.pack();
        AFwindow.setLocationByPlatform(true);
        AFwindow.setVisible(true);
    }

    int area;

    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equalsIgnoreCase("ok")){
            try {
                this.length = Double.parseDouble(tlength.getText());
                this.width = Double.parseDouble(twidth.getText());
                area = (int) (length * width);
                answer();
            }
            catch(NumberFormatException ex){
                ex.printStackTrace();
                // System.out.println("There was an issue!");
                // Can you vague that up for me?!?!
                JOptionPane.showMessageDialog(AFwindow, ex);
            }
        }
    }

    public void answer(){
        JPanel pan2 = new JPanel();
        JLabel howanswer = new JLabel("Your answer is " + area + " We got this by multiplying the length and width");
        pan2.add(howanswer);
        JOptionPane.showMessageDialog(AFwindow, pan2);
    }

    public static void main(String[] args) {
        System.out.println("Hi!");
        new AF().AFWindow();
    }
}
于 2012-07-05T08:05:29.140 回答
1

您是否希望按下“确定”按钮时会弹出答案框?如果是这样,则需要调用 answer 方法:

    if (e.getActionCommand().equalsIgnoreCase("ok")){
        try {
            this.length = Double.parseDouble(tlength.getText());
            this.width = Double.parseDouble(twidth.getText());
            answer();       //  <--- This is missing
        }

此外,您还需要做一些其他事情:

  • 通常,在将组件添加到框架之后,您应该在 GUI 构造方法结束时调用 JFrame.setVisible。我已经在下面的示例中展示了这一点,但是您也应该将 setVisible(..) 移到 AFwindow() 方法的底部。

  • 当您int area = (int) (length * width);在任何方法之外执行此操作时,此语句将被解释为实例变量初始化,并在您实例化 AF 对象时发生。并且当对象被实例化时,您还没有为 length 和 with 分配任何值。因此,面积将始终为 0。改为将该语句移至 answer 函数的内部。

  • 您忘记在 answer 方法中将实际组件添加到框架中。因此,当您将框架设置为可见时,只会显示一个空框架。

以下是一些适当更改的示例:

public void answer(){
    int area = (int) (length * width);  //  <--- Move this inside answer method
    JFrame answer = new JFrame();
    answer.setBackground(Color.yellow);
    JPanel pan2 = new JPanel();
    JLabel howanswer = new JLabel("Your answer is" + area + "We got this by multiplying the length and width");
    pan2.add(howanswer);
    answer.add(pan2); //  <--- Don't forget to add your components to the frame
    answer.pack();    //  <--- Resize the frame to a suitable size
    answer.setVisible(true); //  <--- Set the frame to visible AFTER you have added the components

}
于 2012-07-05T07:48:58.217 回答
0

这段代码对我有用:

public class Test {
    public static void main(String[] args){
        JFrame test = new JFrame();
        test.setVisible(true);
        test.setSize(100,100);
        test.setResizable(false);
        test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

我假设您可以尝试减少代码并尝试以这种方式找到问题点。您可以放置​​一些日志记录以确保setVisible(true)被调用。此外,我建议setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);在退出 GUI 后使用终止线程(如果不想执行一些退出后操作)。

于 2012-07-05T07:22:26.210 回答