Java Swing: Get text value from JOptionPane

£可爱£侵袭症+ 提交于 2019-12-11 03:13:00

问题


I'd like to create a new window which is used in POS system. The user input is for an amount of money the customer has and the window has to display the exchange amount. I'm new with JOptionPane feature (I have been using JAVAFX and it's different).

This is my code:

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
}

Is there any suggestion?


回答1:


use InputDialog for get 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");
    }
}



回答2:


What you need to do, is to create your own custom JOptionPane, that has it's own components, instead of using the build in one's.

Place a JTextField in it, and add a DocumentListener to that, so that when you change something on it, it can be reciprocated on to the status label, as need be.

Try this small example:

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);
    }
}



回答3:


Try using this:

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

Then use the result of myString to do whatever you intend to do.



来源:https://stackoverflow.com/questions/26613879/java-swing-get-text-value-from-joptionpane

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!