我想知道如何将我的第一堂课的价格和描述归还给我的第二堂课?

时间:2014-07-17 06:38:59

标签: java

我很抱歉没有经验,我只有15岁而且我刚刚开始尝试学习Java。这是一个测试程序。

第一类:

public class CoffeeProgram {
public static void main(String[] args) {



      CoffeeReturn.CoffeeDesc(1101101);
      System.out.print("Price of Coffee: $");
      System.out.println(CoffeeReturn.CoffeeCode(1101101)); 
     }

    }
    class CoffeeReturn{

      static double CoffeeCode(double code){

       double price = 0.0;

       if(code == 1101101){

        price = 1.99;
                }
        return price; 
        }
      public static void CoffeeDesc(double code){
       String desc = "Black Coffee w/ Sugar";
       if(code == 1101101){
       System.out.println("Description: "+desc);
       }}}

第二课:

import javax.swing.*;
import java.awt.*;
public class GUI extends JFrame {
public static void main (String args[]) {
GUI window = new GUI();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(200, 200);
window.setVisible(true);
window.setTitle("Coffee Program");}
JLabel CoffeeProgram;
JButton button;
JTextField textfield;

public GUI() {
 setLayout(new FlowLayout());

 CoffeeProgram = new JLabel("Enter Code Here:");
 add(CoffeeProgram);

 textfield = new JTextField(15);
 add(textfield);

 button = new JButton("Submit Code");
 add(button);

我想让程序理解我输入的代码,然后告诉我价格和描述。要求很多,抱歉,但我真的想学习。我很难尝试使第二类接收字符串desc和价格:( 提前感谢您的帮助,我很感激! 到目前为止,这是程序GUI:http://gyazo.com/daef469d089c8a09d9142038f031770c

2 个答案:

答案 0 :(得分:0)

这是您第一天的努力。

对于您想要使用ActionListener的交互。 Oracle在其网站上有some good documentation

此外,您应该考虑关注style guide,从长远来看,它会让您的代码更容易阅读。

答案 1 :(得分:0)

您的代码有两种主要方法,基本上(基本上), 未连接

您正在寻找的东西是对摇摆组件的参考。例如,修改程序将如下所示:

GUI.java

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.JLabel;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class GUI extends JFrame {

    JLabel CoffeeProgram;
    JButton button;
    JTextField textfield;
    JLabel outputPrice;
    JLabel outputDesc;

    public GUI() {
        setLayout(new FlowLayout());
        CoffeeProgram = new JLabel("Enter Code Here:");
        add(CoffeeProgram);
        textfield = new JTextField(15);
        add(textfield);
        outputPrice = new JLabel("Output price here");
        add(outputPrice);
        outputDesc = new JLabel("Desc: ");
        add(outputDesc);    
        button = new JButton("Submit Code");
        add(button);
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            CoffeeReturn returnOutputPrice = new CoffeeReturn();
            double returnPrice = returnOutputPrice.CoffeeCode(1101101);
            outputPrice.setText(String.valueOf(returnPrice));

            String returnDescription = returnOutputPrice.CoffeeDesc(1101101);
            outputDesc.setText(returnDescription);
        }});

    }
}

coffeeprogram.java:

导入javax.swing.JFrame;

public class coffeeprogram {
    public static void main(String[] args) {
        // GUI stuff
        GUI window = new GUI();
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setSize(200, 200);
        window.setVisible(true);
        window.setTitle("Coffee Program");


        CoffeeReturn returnPrice = new CoffeeReturn();

        String returnDesc = returnPrice.CoffeeDesc(1101101);
        window.outputDesc.setText(returnDesc);
    }
}

class CoffeeReturn {

        public double CoffeeCode(double code) {
            double price = 0.0;
            if (code == 1101101) {
                price = 1.99;
            }
            return price;
        }

        public String CoffeeDesc(double code) {
            String desc = "Black Coffee w/ Sugar";
            if (code == 1101101) {
                return desc;
            }
            else
            {
                return "Invalid code";
            }
        }
    }

我刚刚删除了GUI.java中的main方法,并为该按钮添加了一个事件监听器,即当您单击该按钮时,我们需要定义它需要做什么(称为ActionEvent)。然后,您只需使用为每个JLabel组件定义的setText(String)方法,并将值设置为输出。

简单的ActionEvent示例:

参考:[1] http://www.javaprogrammingforums.com/java-swing-tutorials/278-how-add-actionlistener-jbutton-swing.html

[2] http://docs.oracle.com/javase/tutorial/uiswing/ - >列出您可以通过的事项

希望这会有所帮助。