如何将JTextField转换为int并再返回?

时间:2013-04-16 22:05:33

标签: java swing jtextfield

嗨,我正在为我的java类编写一个程序,并遇到了一些问题。分配是编辑教授的现有代码,以便在GUI中单击按钮时,它将获取当前JTextField的值并将其添加到下一个字段的值,并将该值放入该字段中。我已经设法编辑他到了正确的GUI布局,并试图让计算方面起作用,但我却失去了想法。我需要了解如何在启动时立即显示JTextFields中的值,以及如何添加内容。非常感谢您的帮助!

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Graphics2D;

public class U27
{
    public static void main(String[] args)
    {
        MiscFocusFrame myframe = new MiscFocusFrame();
        myframe.setVisible(true);
    }
}

class MiscFocusFrame extends JFrame
{
    private JTextField myField;
    private MiscFocusPanel myPanel;
    private final int FRAMEW = 900;
    private final int FRAMEH = 200;

    public MiscFocusFrame()
    {
        setTitle("U27");
        setSize(FRAMEW, FRAMEH);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        myPanel = new MiscFocusPanel();
        Container contentPane = getContentPane();
        contentPane.add(myPanel, "Center");
    }
}

class MiscFocusPanel extends JPanel
{
    private MiscFocusRegister register1;
    private MiscFocusRegister register2;
    private MiscFocusRegister register3;
    private MiscFocusRegister register4;

    public MiscFocusPanel()
    {
        register1 = new MiscFocusRegister(4);
        register2 = new MiscFocusRegister(4);
        register3 = new MiscFocusRegister(4);
        register4 = new MiscFocusRegister(4);

        register1.setRegisterToPassFocusTo(register2);
        register2.setRegisterToPassFocusTo(register3);
        register3.setRegisterToPassFocusTo(register4);
        register4.setRegisterToPassFocusTo(register1);

        setLayout(new GridLayout(2, 6));

        JLabel labelA = new JLabel("Cup 1", JLabel.LEFT);
        JPanel labelPanelA = new JPanel();
        labelPanelA.add(labelA);
        add(labelPanelA);

        JPanel regPanelA = new JPanel();
        regPanelA.add(register1.getMyField());
        add(regPanelA);

        JButton myButton1 = new JButton("Add To Next Cup");
        MiscButtonListener myButtonListener1 = new MiscButtonListener();
        myButton1.addActionListener(myButtonListener1);
        JPanel buttonPanel1 = new JPanel();
        buttonPanel1.add(myButton1);
        add(buttonPanel1);

        JLabel labelB = new JLabel("Cup 2", JLabel.LEFT);
        JPanel labelPanelB = new JPanel();
        labelPanelB.add(labelB);
        add(labelPanelB);

        JPanel regPanelB = new JPanel();
        regPanelB.add(register2.getMyField());
        add(regPanelB);

        JButton myButton2 = new JButton("Add To Next Cup");
        MiscButtonListener myButtonListener2 = new MiscButtonListener();
        myButton2.addActionListener(myButtonListener2);
        JPanel buttonPanel2 = new JPanel();
        buttonPanel2.add(myButton2);
        add(buttonPanel2);

        JLabel labelC = new JLabel("Cup 3", JLabel.LEFT);
        JPanel labelPanelC = new JPanel();
        labelPanelC.add(labelC);
        add(labelPanelC);

        JPanel regPanelC = new JPanel();
        regPanelC.add(register3.getMyField());
        add(regPanelC);

        JButton myButton3 = new JButton("Add To Next Cup");
        MiscButtonListener myButtonListener3 = new MiscButtonListener();
        myButton3.addActionListener(myButtonListener3);
        JPanel buttonPanel3 = new JPanel();
        buttonPanel3.add(myButton3);
        add(buttonPanel3);

        JLabel labelD = new JLabel("Cup 4", JLabel.LEFT);
        JPanel labelPanelD = new JPanel();
        labelPanelD.add(labelD);
        add(labelPanelD);

        JPanel regPanelD = new JPanel();
        regPanelD.add(register4.getMyField());
        add(regPanelD);

        JButton myButton4 = new JButton("Add To Next Cup");
        MiscButtonListener myButtonListener4 = new MiscButtonListener();
        myButton4.addActionListener(myButtonListener4);
        JPanel buttonPanel4 = new JPanel();
        buttonPanel4.add(myButton4);
        add(buttonPanel4);

    }

    public void paintComponent(Graphics g)
    {
        Graphics2D g2 = (Graphics2D) g;
        super.paintComponent(g2);
    }

    private class MiscButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            register1.swapRegisterContents(register2);
        }
    }
}


/*
This class contains a new method, setRegisterToPassFocusTo(), and in
the listener focus is passed to that register after the new value for
the text field is taken in.
*/

class MiscFocusRegister
{
    private MiscFocusByte registerValue;
    private JTextField myField;
    private MiscFocusRegister registerToPassFocusTo;

    public MiscFocusRegister()
    {
    }

    public MiscFocusRegister(int stringIn)
    {
        registerValue = new MiscFocusByte(stringIn);
        myField = new JTextField(stringIn);
        TextFieldListener myListener = new TextFieldListener();
        myField.addActionListener(myListener);
    }

    public void setRegisterValue(MiscFocusByte byteIn)
    {
        registerValue = byteIn;
    }

    public MiscFocusByte getRegisterValue()
    {
        return registerValue;
    }

    public void setMyField(JTextField fieldIn)
    {
        myField = fieldIn;
    }

    public JTextField getMyField()
    {
        return myField;
    }

    public void setRegisterToPassFocusTo(MiscFocusRegister registerIn)
    {
        registerToPassFocusTo = registerIn;
    }

    public void swapRegisterContents(MiscFocusRegister source)
    {
        MiscFocusByte tempValue;

        tempValue = source.getRegisterValue();
        source.setRegisterValue(this.getRegisterValue());
        source.myField.setText(source.getRegisterValue().getStringFromByte());
        this.setRegisterValue(tempValue);
        this.myField.setText(this.getRegisterValue().getStringFromByte());
    }

    private class TextFieldListener implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            String inputString = myField.getText();
            registerValue.setByteToThisString(inputString);
            registerToPassFocusTo.getMyField().requestFocusInWindow();
        }
    }
}

class MiscFocusByte
{
    public static int bitsinbyte = 8;
    private static final String junk = "00000000";
    private char bytearray[] = new char[bitsinbyte];
    public String astring;

    public MiscFocusByte()
    {
        junk.getChars(0, bitsinbyte, bytearray, 0);
    }

    public MiscFocusByte(int stringIn)
    {
        //junk.getChars(0, bitsinbyte, bytearray, 0);
        //int stringlength = stringIn.length();
        //if(stringlength > bitsinbyte)
        //  stringlength = bitsinbyte;
        //stringIn.getChars(0, stringlength, bytearray, 0);
        stringIn = stringIn + 4;
    }

    public void setByteToThisString(String astring)
    {
        //junk.getChars(0, bitsinbyte, bytearray, 0);
        //int stringlength = astring.length();
        //if(stringlength > bitsinbyte)
        //  stringlength = bitsinbyte;
        //astring.getChars(0, stringlength, bytearray, 0);
        astring = astring + 4;
    }

    public String getStringFromByte()
    {
        //return String.copyValueOf(bytearray);
        return String.valueOf(astring);
    }
}

1 个答案:

答案 0 :(得分:1)

尝试这样的事情......

String text=jTextField1.getText();
text+="1";
jTextField1.setText(text);
jTextField1.requestFocus();