跨面板的动作监听器帮助

时间:2014-05-12 08:58:55

标签: java swing listener

好的,这是我的问题。我正在编写一个程序,它需要4个面板,将它们组合成一个用户输入在西方,总计在东方,以及一个横幅在北方。我的问题是如何实现一个动作监听器来获取有人在西面板中键入的数字自动显示在总计(东)面板中?代码有点长(对我来说),我正在接近这个,只是想完成它。我是否必须将事件处理程序类放在用户信息面板中,或者只将其全部放在总计面板中?我检查了其他问题但找不到答案。

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import javax.swing.*;
import javax.swing.border.TitledBorder;


/**
*
* User input panel 
* first panel
* has JTextFields for input of date for the hw/h, hours, and gallons
**/

public class UserInput extends JFrame implements ActionListener
{
    //variables/attributes
    private double kwh;
    private double hours;
    private double gallons;
    public double total;

    //JFrame componants
    private final  JTextField kwhField;
    private final  JTextField hoursField;
    private final  JTextField gallonsField;
    private final JLabel kwhLabel;
    private final JLabel hoursLabel;
    private final JLabel gallonsLabel;


    public UserInput()
    {
        // create the Grid
        setLayout (new GridLayout (3,2));

        //create the fields
        kwhField = new JTextField(15);
        hoursField = new JTextField(15);
        gallonsField = new JTextField(15);

        //create the labels
        kwhLabel = new JLabel ("Kw/H");
        hoursLabel = new JLabel ("Hours");
        gallonsLabel = new JLabel ("Gallons");

        //Add a border
        TitledBorder titled;
        titled = BorderFactory.createTitledBorder("User Input");

        //Add the tiles into the grid
        add(kwhField);
        add(kwhLabel);
        add(hoursField);
        add(hoursLabel);
        add(gallonsField);
        add(gallonsLabel);

        //add action listeners
        kwhLabel.addActionListener(this);
    }

    //The get method to retrieve the user inputs
    Scanner keyboard = new Scanner(System.in);



/**
 *
 * @return
 */
public double getKwh()
    {
        return kwh;
    }

/**
 *
 * @param k
 */
public void setKwh(double k)
    {
        kwh = k;
    }
    public void setHours (double h)
    {
        hours = h;
    }

    public double getHours ()
    {
        return hours;
    }

/**
 *
 * @param g
 */
public void getGallons(double g)
    {
        gallons = g;
    }

public double setGallons()
{
    return gallons;
}

//Now add action listeners??? we will try it


@Override
public void actionPerformed(ActionEvent ae) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of             
}

1 个答案:

答案 0 :(得分:0)

首先创建一个JLabel并将其添加到您的东面板以编写键入的文本。您可以在文本字段中添加keylistener。覆盖Key Typed方法并使用event.getKeyChar方法从事件中获取键入的键,并将其写入JLabel。

下面是This Link

的一些示例代码
public class KeyEventDemo ...  implements KeyListener ... {
    ...//where initialization occurs:
        textField = new JTextField(20);
        textField.addKeyListener(this);


    ...
    /** Handle the key typed event from the text field. */
    public void keyTyped(KeyEvent e) {
        // HERE You will Write Your Code

    }

    /** Handle the key-pressed event from the text field. */
    public void keyPressed(KeyEvent e) {

    }

    /** Handle the key-released event from the text field. */
    public void keyReleased(KeyEvent e) {

    }
 }

希望这有帮助。