2014-10-28 66 views
2

我想创建一个用于POS系统的新窗口。用户输入用于客户具有的金额并且窗口必须显示交换金额。我新JOptionPane功能(我一直在使用JAVAFX,这是不同的)。Java Swing:从JOptionPane获取文本值

这是我的代码:

public static void main(String[] argv) throws Exception { 
    String newline = System.getProperty("line.separator"); 
    int cost = 100; 
    int amount = Integer.parseInt(JOptionPane.getText()) // this is wrong! This needs to come  from user input box in the same window. 
    JFrame frame = new JFrame(); 
    String message = "Enter the amount of money"+newline+"The exchange money is: "+amount-cost; 
    String text = JOptionPane.showInputDialog(frame, message); 
    if (text == null) { 
     // User clicked cancel 
} 

有没有什么建议?

+0

可能请你进一步澄清了一下!对我来说,你想为用户提出一个'JOptionPane',现在当用户在同一个'JOptionPane'中输入某些东西作为输入时,你希望在同一个'JOptionPane'内部的东西可以改变,关于用户输入。我对吗? – 2014-10-28 17:27:06

+0

好的 - 显示JOptionPane:有一个文本字段和消息字段。如果文本写入文本字段中,消息字段将会更改。 – charen 2014-10-28 17:33:48

+0

只需使用你自己的'Component'传递给'JOptionPane',并让你的'Component'处理事件和更新 – Robin 2014-10-28 17:40:40

回答

3

使用InputDialog为为获得userinput

public static void main(String[] argv) throws Exception { 
     //JFrame frame = new JFrame(); 
     //frame.setVisible(true); 
     int cost = 100; 
     JLabel l=new JLabel("The exchange money is"); 

     JPanel p=new JPanel(new GridLayout(1, 2, 10, 10)); 
     p.setPreferredSize(new Dimension(400, 50)); 
     JTextField t=new JTextField("Enter the amount of money"); 
     t.addKeyListener(new java.awt.event.KeyAdapter() { 
     public void keyReleased(java.awt.event.KeyEvent evt) { 
      try{ 
      int amount=Integer.parseInt(t.getText()); 
      l.setText("The exchange money is: \n" + (amount - cost)); 
      }catch(Exception ex){ 
       // ex.printStackTrace(); 
      } 
     } 
     }); 
     p.add(t); 
     p.add(l); 

    int option = JOptionPane.showConfirmDialog(null,p,"JOptionPane Example : ",JOptionPane.OK_CANCEL_OPTION,JOptionPane.PLAIN_MESSAGE); 
    if(option==0){ 
     System.out.println("ok clicked"); 
    }else{ 
     System.out.println("cancel clicked"); 
    } 
} 
+1

谢谢@fastsnail – charen 2014-10-28 17:58:50

+0

嘿@fastsnail,关于代码的一个更快的问题。你是否可以重新编辑并显示如果按下OK按钮,如何继续使用if子句,或者如果按下Cancel buttton按钮,则子句如何继续。 – charen 2014-10-28 19:08:23

+0

@charen plz解释我并不完全明白 – 2014-10-28 19:09:35

1

尝试使用这样的:

if(myJOptionPane.getValue() instanceOf String){ 
    String myString = (String) myJOptionPane.getValue(); 
} 

然后使用myString结果做任何你想要做的事。

+0

这会给我单元化变量。我想要的是在相同的JOptionPane对话框中显示字符串 – charen 2014-10-28 16:59:00

+0

您可以将此值并将其设置为新的Joption窗格元素。 – 2014-10-28 17:02:10

+0

我不能改变相同的JOptionPane元素吗? – charen 2014-10-28 17:08:36

2

你需要做的,就是创建自己的自定义JOptionPane,有它自己的组件,而不是一个人使用的版本。

在其中放置一个JTextField,并在其中添加DocumentListener,以便当您更改其中的某些内容时,可以根据需要对status label进行调整。

试试这个小例子:

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

public class JOptionPaneExample { 

    private JLabel label; 
    private JTextField tfield; 
    private JLabel statusLabel; 

    private static final int GAP = 5; 

    private void displayGUI() { 
     JOptionPane.showMessageDialog(null, getPanel()); 
    } 

    private JPanel getPanel() { 
     JPanel panel = new JPanel(new GridLayout(0, 1)); 
     label = new JLabel("Enter something: ", JLabel.CENTER); 
     tfield = new JTextField(10); 
     tfield.getDocument().addDocumentListener(new MyDocumentListener()); 
     JPanel controlPanel = new JPanel(); 
     controlPanel.add(label); 
     controlPanel.add(tfield); 
     panel.add(controlPanel); 

     statusLabel = new JLabel("", JLabel.CENTER); 
     panel.add(statusLabel); 

     return panel; 
    } 

    private class MyDocumentListener implements DocumentListener { 

     @Override 
     public void changedUpdate(DocumentEvent de) { 
      updateStatus(); 
     } 

     @Override 
     public void insertUpdate(DocumentEvent de) { 
      updateStatus(); 
     } 

     @Override 
     public void removeUpdate(DocumentEvent de) { 
      updateStatus(); 
     } 

     private void updateStatus() { 
      statusLabel.setText(tfield.getText()); 
     } 
    } 

    public static void main(String[] args) { 
     Runnable runnable = new Runnable() { 
      @Override 
      public void run() { 
       new JOptionPaneExample().displayGUI(); 
      } 
     }; 
     EventQueue.invokeLater(runnable); 
    } 
} 
+0

这似乎工作得很好,虽然这个显示部分我用作更大代码的一部分,所以你的例子对我来说不是真正的选择。Upvote和答案仍然是,谢谢你的时间:) – charen 2014-10-28 17:55:35

+1

@charen:其实,我确实喜欢上面的答案(他们两个),只是没有新的编辑(因为它没有在Swing中使用)。你需要的是'getPanel()'方法和'MyDocumentListener'类,从这段代码中,休息你已经在你的代码中:-)其余的,你最欢迎和保持微笑:-) – 2014-10-28 18:00:08

+1

@charen:尽量从任何地方复制一些文本,并尝试将其粘贴到“JTextField”中,但在这种情况下,“DocumentListener”仍然可以工作,但“KeyListener”会失败,因为它们只需要通过键输入。 – 2014-10-28 18:16:23