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

时间:2014-11-23 05:36:44

标签: java swing 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,它有一个文本字段,我希望在我的Calculator类中找到华氏度到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));

}

这是我的Calculator类,它扩展了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 个答案:

答案 0 :(得分:0)

正如Andrew Thompson已经指出的那样,请确保您的代码至少编译并具有主方法。您提供的TextEditor课程缺少loadMenuBar& loadToolBar& actionPerformed方法loadCalculator TextEditor方法被合并在一起,两个类中都缺少main方法。请尽可能详尽地制定您的问题以获得良好的答案。

我认为转换结果未显示的原因是CalculatorCalculator.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); } } 对象,但此对象由计算器本身创建(请参阅字段声明),并且此文本编辑器不可见。这可以通过将文本编辑器的引用传递给计算器构造函数来解决。

在代码中:

{{1}}