有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java从JTextField中获取文本并将其放入JTextArea中

这是我的主课

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
            Program p = new Program();
        }

}

这是我的课程

public class Program {

    public Program(){
        //System.out.println("Let's begin...");

        TextEditor textEditor = new TextEditor();
        textEditor.setVisible(true);
        textEditor.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    }
}

这是我的TextEditor类。在这个类中,我有一个JFrame,它有一个文本字段,我希望将计算器类中的华氏温度到celcus的转换结果放在其中

public class TextEditor extends JFrame implements ActionListener{
JTextArea textArea;

public TextEditor(){
    super("TextMe");
    this.setLayout(new BorderLayout());
    loadMenuBar();
    loadToolBar();
    loadTextArea();
    this.pack();
}

private void loadTextArea() {
    // TODO Auto-generated method stub
    textArea = new JTextArea();
    textArea.setPreferredSize(new Dimension(800,600));
    this.add(BorderLayout.CENTER, textArea);
}

    @Override
    public void actionPerformed(ActionEvent e) {
}   loadCalculator()
private void loadCalculator() {
    // TODO Auto-generated method stub
    Calculator c = new Calculator();
    Calculator calculator = new Calculator();
    calculator.setVisible(true);
    calculator.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

    textArea.setText(String.valueOf(c.fahrenheit));

}

这是我的计算器类,它扩展了JFrame并实现了ActionListener。在这里,JFrame会出现一个JButton,旁边有一个JTextField。当用户在文本字段中输入一个数字时,它会进行计算并将华氏温度转换为摄氏温度。完成后,我希望结果显示在TextEditor类的JTextArea上

public class Calculator extends JFrame implements ActionListener{
JButton fToCButton;
JTextField fToC;
double fahrenheit;
TextEditor a = new TextEditor();

public Calculator(){
            super("Unit Converter");
            this.setLayout(new FlowLayout());

            fToC = new JTextField(5);

fToCButton = new JButton("Ferenheit To Celcius");
            fToCButton.addActionListener(this);

add(fToCButton, BorderLayout.WEST);
            add(fToC);
this.pack()

@Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            if(e.getSource() == fToCButton){
                degreeConversion();
            }
        }

        private void degreeConversion() {
            // TODO Auto-generated method stub
            double conversion = Double.parseDouble(fToC.getText());
            fahrenheit = (((conversion -32) * 5) / 9);
            a.textArea.setText(String.valueOf(fahrenheit));
            System.out.println(fahrenheit);
        }


 }

共 (1) 个答案

  1. # 1 楼答案

    正如Andrew Thompson已经指出的,确保您的代码至少可以编译并且有一个main方法。您提供的TextEditor类缺少loadMenuBar&loadToolBar方法actionPerformed&loadCalculator方法在某种程度上被合并在一起,两个类中都缺少一个主方法。请尽可能地阐述你的问题,以得到好的答案

    我认为转换结果没有出现的原因是TextEditorCalculator对象之间没有连接。在Calculator.degreeConversion方法中,您试图将结果传递给TextEditor对象,但该对象是由计算器本身创建的(请参见字段声明),并且该文本编辑器不可见。这可以通过将文本编辑器的引用传递给计算器构造函数来解决

    代码:

    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    
    
    /**
     * TextEditor class.
     */
    public class TextEditorVersion2 extends JFrame implements ActionListener {
        JTextArea textArea;
    
        public static void main(String[] args) {
            final TextEditorVersion2 textEditor = new TextEditorVersion2();
            textEditor.setVisible(true);
            final CalculatorVersion2 calculatorVersion2 = new CalculatorVersion2(textEditor);
            calculatorVersion2.setVisible(true);
        }
    
        public TextEditorVersion2(){
            super("TextMe");
            this.setLayout(new BorderLayout());
            // Freek: these methods are missing.
            //loadMenuBar();
            //loadToolBar();
            // Freek: these methods are missing.
            loadTextArea();
            this.pack();
        }
    
        private void loadTextArea() {
            // TODO Auto-generated method stub
            textArea = new JTextArea();
            textArea.setPreferredSize(new Dimension(800,600));
            this.add(BorderLayout.CENTER, textArea);
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
        }
    
        private void loadCalculator() {
            // TODO Auto-generated method stub
            CalculatorVersion2 c = new CalculatorVersion2(this);
            CalculatorVersion2 calculatorVersion2 = new CalculatorVersion2(this);
            calculatorVersion2.setVisible(true);
            calculatorVersion2.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    
            textArea.setText(String.valueOf(c.fahrenheit));
        }
    }
    
    
    /**
     * Calculator class.
     */
    class CalculatorVersion2 extends JFrame implements ActionListener {
        JButton fToCButton;
        JTextField fToC;
        double fahrenheit;
        TextEditorVersion2 a;
    
        public CalculatorVersion2(final TextEditorVersion2 textEditor) {
            super("Unit Converter");
            this.a = textEditor;
            this.setLayout(new FlowLayout());
    
            fToC = new JTextField(5);
    
            fToCButton = new JButton("Ferenheit To Celcius");
            fToCButton.addActionListener(this);
    
            add(fToCButton, BorderLayout.WEST);
            add(fToC);
            this.pack();
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            if(e.getSource() == fToCButton){
                degreeConversion();
            }
        }
    
        private void degreeConversion() {
            // TODO Auto-generated method stub
            double conversion = Double.parseDouble(fToC.getText());
            fahrenheit = (((conversion -32) * 5) / 9);
            a.textArea.setText(String.valueOf(fahrenheit));
            System.out.println(fahrenheit);
        }
    }