2015-11-14 67 views
0

EDITED的Java Swing:管理整数/与听众和J *字段

我可能只是盯着屏幕WAYYY太久双打,但我觉得有必要提出这个问题,去之前睡觉incase我的deli妄仍然是真的...

我遇到的问题是我试图从JTextField textField得到一个int/double并将其转换为(n)int/double稍后使用一个侦听器,在从侦听器中检索它之后... ListenForText实现KeyListener,特别是KeyTyped,通过toString()获取并将其存储在一个String txtFldAmnt中。之后,如果复制到编辑器,我将把Integer.parseInt(txtFldAmnt)的结果存储到fldAmnt(fldAmnt = Integer.pareseInt(txtFldAmnt))LINE 99中。一旦将其转换为int,我希望能够操作它,然后再次使用它,在LINE 173处,在新窗口中显示fldAmnt。老实说,我并不在意它是显示一个int还是String,但我知道一个String是JTextField所必需的。我错过了什么魔法?总是欢迎回答,但我更喜欢有机会学习。此外,由于我是相当新到Java,题外话指针,批评和实现这个代码更好的方法是很大的欢迎:)

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


public class GUI extends JFrame{ 

/** 
* wtf is the serial version UID = 1L 
*/ 
private static final long serialVersionUID = 1L; 
private JButton wdBtn; 
private JButton dpBtn; 
private JButton xferBtn; 
private JButton balBtn; 
private JRadioButton chkRadio; 
private JRadioButton savRadio; 
private JTextField textField; 
private static String txtFldAmnt; 
private static int fldAmnt = 0; 
private static Double chkBal = 0.00; 
private static Double savBal = 0.00; 


public static void main(String[] args){ 
    new GUI(); 
} 


public GUI() { 

    //default location and size 
    this.setSize(300,182); 
    this.setLocationRelativeTo(null); 
    this.setResizable(false); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    this.setTitle("ATM Machine"); 

    //add buttons 
    wdBtn = new JButton("Withdraw"); 
    dpBtn = new JButton("Deposit"); 
    xferBtn = new JButton("Transfer"); 
    balBtn = new JButton("Show Balance"); 
    chkRadio = new JRadioButton("Checking"); 
    chkRadio.setSelected(false); 
    savRadio = new JRadioButton("Savings"); 
    savRadio.setSelected(false); 
    textField = new JTextField("", 20); 
    final JLabel textLabel = new JLabel("Enter amount: "); 
    textField.setToolTipText("Enter amount"); 

    //Listener class to pass button listeners 
    ListenForButton lForButton = new ListenForButton(); 
    wdBtn.addActionListener(lForButton); 
    dpBtn.addActionListener(lForButton); 
    xferBtn.addActionListener(lForButton); 
    balBtn.addActionListener(lForButton); 
    chkRadio.addActionListener(lForButton); 
    savRadio.addActionListener(lForButton); 

    ListenForText textFieldListener = new ListenForText(); 
    textField.addKeyListener(textFieldListener); 

    //Configure layouts 
    JPanel PANEL = new JPanel(); 
    PANEL.setLayout(new FlowLayout(FlowLayout.CENTER)); 
    JPanel panel1 = new JPanel(); 
    panel1.setLayout(new GridLayout(2,2, 5, 10)); 
    JPanel panel2 = new JPanel(); 
    panel2.setLayout(new GridLayout(1,2,10,10)); 
    panel2.setLayout(new FlowLayout(FlowLayout.CENTER)); 
    JPanel panel3 = new JPanel(); 
    panel3.setLayout(new GridLayout(2,1)); 

    //add buttons to their panels 
    panel1.add(wdBtn); 
    panel1.add(dpBtn); 
    panel1.add(xferBtn); 
    panel1.add(balBtn); 
    panel2.add(chkRadio); 
    panel2.add(savRadio); 
    panel3.add(textLabel); 
    panel3.add(textField); 

    PANEL.add(panel1); 
    PANEL.add(panel2); 
    PANEL.add(panel3); 

    this.add(PANEL); 
    //this.setAlwaysOnTop(true); 
    this.setVisible(true); 
} 
//implement listeners 
private class ListenForText implements KeyListener { 

    @Override 
    public void keyTyped(KeyEvent e) { 
     txtFldAmnt = (e.toString()); 
     fldAmnt = Integer.parseInt(txtFldAmnt); 

    } 

    @Override 
    public void keyPressed(KeyEvent e) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void keyReleased(KeyEvent e) { 
     // TODO Auto-generated method stub 

    } 

} 
private class ListenForButton implements ActionListener { 

    public void actionPerformed(ActionEvent e){ 

     //maybe do case/switch statements 
     if (e.getSource() == wdBtn) { 
      JFrame newFrame = new JFrame("Withdraw Title"); 
      newFrame.setResizable(false); 
      newFrame.setLocationRelativeTo(null); 
      newFrame.setSize(300, 91); 

      JPanel panel = new JPanel(); 
      JLabel newLbl = new JLabel("Withdraw Frame", JLabel.CENTER); 

      panel.add(newLbl); 
      newFrame.add(panel); 
      newFrame.setVisible(true); 
     } 
     if (e.getSource() == dpBtn) { 
      JFrame newFrame = new JFrame("Deposit Title"); 
      /* 
      * Set the newFrame.setSize(300,182); to this comment in the if statement to place 
      * the window directly over current window 
      */ 
      newFrame.setResizable(false); 
      newFrame.setLocationRelativeTo(null); 
      newFrame.setSize(300, 91); 

      JPanel panel = new JPanel(); 
      JLabel newLbl = new JLabel("Deposit Frame", JLabel.CENTER); 

      panel.add(newLbl); 
      newFrame.add(panel); 
      newFrame.setVisible(true); 
     } 
     if (e.getSource() == xferBtn) { 
      JFrame newFrame = new JFrame("Transfer Title"); 

      newFrame.setResizable(false); 
      newFrame.setLocationRelativeTo(null); 
      newFrame.setSize(300, 91); 

      JPanel panel = new JPanel(); 
      JLabel newLbl = new JLabel("Transfer Frame", JLabel.CENTER); 

      panel.add(newLbl); 
      newFrame.add(panel); 
      newFrame.setVisible(true); 
     } 
     if (e.getSource() == balBtn){ 
      JFrame newFrame = new JFrame("Balance Title"); 

      newFrame.setResizable(false); 
      newFrame.setLocationRelativeTo(null); 
      newFrame.setSize(300, 91); 

      JPanel panel = new JPanel(); 
      JTextField newLbl = new JTextField(Integer.toString(fldAmnt)); 

      panel.add(newLbl); 
      newFrame.add(panel); 
      newFrame.setVisible(true); 

     } 
    } 
} 
} 

-cheers

EDITED低于

谢谢你的回答,我对这个可怕的描述感到抱歉...但是我找到了一个解决办法,我不确定它是否合适。 与上面的例子:

fldAmnt = Integer.pareseInt(txtFldAmnt) 

我只是增加了一个.getText()//虽然我重写了整个源代码

fldAmnt = Integer.pareseInt(txtFldAmnt.getText()) 

这对我的工作我的整个程序。

我希望我不必在下面发布我的整个代码,但我还没有决定存储我的所有代码的好地方。 (建议,欢迎:d)

但在这里它是: 进口的javax.swing。 ; import java.awt.GridLayout; import java.awt.event。; import javax.swing.border。*;

public class ATM extends JFrame{ 
//buttons needed 
JButton wBtn; 
JButton dBtn; 
JButton xBtn; 
JButton bBtn; 
//radios needed 
JRadioButton cRadio; 
JRadioButton sRadio; 
//Text field needed 
JTextField txt; 
JLabel txtLabel; 
static int withdraw = 0; 
Double amount = 0.00; 
Double cBal = 100.00; 
Double sBal = 100.00; 


double number1, number2, totalCalc; 

public static void main(String[] args){ 

    new Lesson22(); 

} 
public ATM(){ 
    this.setSize(400, 200); 
    this.setLocationRelativeTo(null); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    this.setTitle("My Third Frame"); 

    wBtn = new JButton("Withdraw"); 

    dBtn = new JButton("Deposit"); 
    xBtn = new JButton("Transfer"); 
    bBtn = new JButton("Show Balance"); 
    cRadio = new JRadioButton("Checking"); 
    sRadio = new JRadioButton("Savings"); 
    txtLabel = new JLabel("Amount: $"); 
    txt = new JTextField("", 10); 

    JPanel thePanel = new JPanel(); 

    // Create an instance of ListenForEvents to handle events 

    ListenForButton lForButton = new ListenForButton(); 

    wBtn.addActionListener(lForButton); 
    dBtn.addActionListener(lForButton); 
    xBtn.addActionListener(lForButton); 
    bBtn.addActionListener(lForButton); 


    // How to add a label -------------------------- 

    // Creates a group that will contain radio buttons 
    // You do this so that when 1 is selected the others 
    // are deselected 

    ButtonGroup operation = new ButtonGroup(); 

    // Add radio buttons to the group 

    operation.add(cRadio); 
    operation.add(sRadio); 

    // Create a new panel to hold radio buttons 

    JPanel operPanel = new JPanel(); 
    JPanel btnPanel1 = new JPanel(); 
    JPanel btnPanel2 = new JPanel(); 
    btnPanel1.setLayout(new GridLayout(1,2)); 
    btnPanel2.setLayout(new GridLayout(1,2)); 

    Border btnBorder = BorderFactory.createEmptyBorder(); 
    btnPanel1.setBorder(btnBorder); 
    btnPanel1.add(wBtn); 
    btnPanel1.add(dBtn); 
    btnPanel2.setBorder(btnBorder); 
    btnPanel2.add(xBtn); 
    btnPanel2.add(bBtn); 


    Border operBorder = BorderFactory.createBevelBorder(1); 

    // Set the border for the panel 

    operPanel.setBorder(operBorder); 

    // Add the radio buttons to the panel 

    operPanel.add(cRadio); 
    operPanel.add(sRadio); 


    // Selects the add radio button by default 
    cRadio.setSelected(true); 
    JPanel txtPanel = new JPanel(); 
    txtPanel.add(txtLabel); 
    txtPanel.add(txt); 


    thePanel.add(btnPanel1); 
    thePanel.add(btnPanel2); 
    thePanel.add(operPanel); 
    thePanel.add(txtPanel); 
    thePanel.setLayout(new GridLayout(4,2)); 
    this.add(thePanel); 

    this.setVisible(true); 

    txt.requestFocus(); 

} 

private class ListenForButton implements ActionListener{ 

    // This method is called when an event occurs 

    public void actionPerformed(ActionEvent e){ 
     /****************************************************** 
     * THIS IS FOR CHECKING 
     *****************************************************/ 
     // Check if the source of the event was the button 
     if(cRadio.isSelected()){ 
      if(e.getSource() == wBtn){ 
       try { 
        amount = Double.parseDouble(txt.getText()); 
        cBal -= amount; 
       } 
       catch(NumberFormatException excep){ 
        JOptionPane.showMessageDialog(ATM.this, 
          "Please enter a valid number in multiples of 20", 
          "ERROR", JOptionPane.ERROR_MESSAGE); 
       } 
      } 
      if(e.getSource() == bBtn){ 
       JFrame bFrame = new JFrame(); 
       bFrame.setSize(300, 182); 
       bFrame.setLocationRelativeTo(null); 
       JPanel panel = new JPanel(); 
       JLabel cLabel = new JLabel(Double.toString(cBal)); 
       JLabel CLBL = new JLabel("Checking: "); 
       panel.add(CLBL); 
       panel.add(cLabel); 
       bFrame.add(panel); 
       bFrame.setVisible(true); 
      } 

      if(e.getSource() == dBtn){ 
       amount = Double.parseDouble(txt.getText()); 
       cBal += amount; 
      } 
      if(e.getSource() == xBtn){ 
       amount = Double.parseDouble(txt.getText()); 
       if (sBal >= 0 && sBal >= amount){ 
        cBal += amount; 
        sBal -= amount; 
       } 
       else if (sBal < amount) { 
        JOptionPane.showMessageDialog(ATM.this, 
          "INSUFFICENT FUNDS", "ERROR", 
          JOptionPane.ERROR_MESSAGE); 
       } 
      } 
     } 
     /****************************************************** 
     * THIS IS FOR SAVINGS 
     *****************************************************/ 
     if(sRadio.isSelected()){ 
      if(e.getSource() == wBtn){ 
       try { 
        amount = Double.parseDouble(txt.getText()); 
        if(sBal >= 0 && sBal >= amount){ 
         sBal -= amount; 
        } 
        else if (sBal < amount) { 
         JOptionPane.showMessageDialog(ATM.this, 
           "INSUFFICENT FUNDS", "ERROR", 
           JOptionPane.ERROR_MESSAGE); 
        } 
       } 
       catch(NumberFormatException excep){ 
        JOptionPane.showMessageDialog(ATM.this, 
          "Please enter a valid number in multiples of 20", 
          "ERROR", JOptionPane.ERROR_MESSAGE); 
       } 
      } 
      if(e.getSource() == bBtn){ 
       JFrame bFrame = new JFrame(); 
       bFrame.setSize(300, 182); 
       bFrame.setLocationRelativeTo(null); 
       JPanel panel = new JPanel(); 
       JLabel sLabel = new JLabel(Double.toString(sBal)); 
       JLabel SLBL = new JLabel("Savings: "); 
       panel.add(SLBL); 
       panel.add(sLabel); 

       bFrame.add(panel); 
       bFrame.setVisible(true); 
      } 

      if(e.getSource() == dBtn){ 
       try{ 
        amount = Double.parseDouble(txt.getText()); 
        sBal += amount; 
       } 
       catch(NumberFormatException excep){ 
        JOptionPane.showMessageDialog(ATM.this, 
          "Please enter a valid number!", 
          "ERROR", JOptionPane.ERROR_MESSAGE); 
       } 
      } 
      if(e.getSource() == xBtn){ 
       amount = Double.parseDouble(txt.getText()); 
       if (cBal >= 0 && cBal >= amount){ 
        sBal += amount; 
        cBal -= amount; 
       } 
       else if (cBal < amount) { 
        JOptionPane.showMessageDialog(ATM.this, 
          "INSUFFICENT FUNDS", "ERROR", 
          JOptionPane.ERROR_MESSAGE); 
       } 
      } 
     } 
    } 
} 
} 

...顺便说任何人都可以解释此警告消息: 的序列化类ATM没有声明型长

如果不是简单地穿上”的静态最后的serialVersionUID领域麻烦了。我有空的时候会继续研究它。

这仍然不完整,但谢谢大家的帮助!

+1

'我可能只是盯着WAYYY的屏幕太长了 - 所以你需要学会简化问题。你的问题是关于一个JTextField。因此,只用JTextField创建一个简单的JFrame,并忘记所有其他组件。一旦你解决了问题,你就可以将解决方案应用到你的真实代码中。如果你找不到解决方案,那么你有一个简单的演示来发布(我们不想看数百行代码,这被称为[SSCCE](http://sscce.org/)和 – camickr

+0

你的程序有什么问题?你根本没有解释实际的问题,这使得我们很难理解。另外,你将不会**想要使用KeyListeners可能是JTextField,DocumentListener或DocumentFilter,但不是KeyListener。 –

回答

3

KeyListener在任何JTextComponent是一个坏主意。

要监视更改文本组件,使用DocumentListener,该文本组件可以处理,使用DocumentFilter内容过滤器。有关更多详细信息,请参阅Listening for Changes on a DocumentImplementing a Document FilterDocumentFilter Examples

在你的情况下,很可能最好使用一个JSpinnerJFormattedTextField,因为它们是设计来处理(除其他事项外)的数字

How to Use SpinnersHow to Use Formatted Text Fields更多细节