Swing Wie funktioniert dieser ChangePropertyListener???

JavaCat++

Mitglied
Java:
/*
* Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
*   - Redistributions of source code must retain the above copyright
*     notice, this list of conditions and the following disclaimer.
*
*   - Redistributions in binary form must reproduce the above copyright
*     notice, this list of conditions and the following disclaimer in the
*     documentation and/or other materials provided with the distribution.
*
*   - Neither the name of Oracle or the names of its
*     contributors may be used to endorse or promote products derived
*     from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import java.text.*;
/**
* FormattedTextFieldDemo.java requires no other files.
*
* It implements a mortgage calculator that uses four
* JFormattedTextFields.
*/
public class FormattedTextFieldDemo extends JPanel
                                    implements PropertyChangeListener {
    //Values for the fields
    private double amount = 100000;
    private double rate = 7.5;  //7.5%
    private int numPeriods = 30;
    //Labels to identify the fields
    private JLabel amountLabel;
    private JLabel rateLabel;
    private JLabel numPeriodsLabel;
    private JLabel paymentLabel;
    //Strings for the labels
    private static String amountString = "Loan Amount: ";
    private static String rateString = "APR (%): ";
    private static String numPeriodsString = "Years: ";
    private static String paymentString = "Monthly Payment: ";
    //Fields for data entry
    private JFormattedTextField amountField;
    private JFormattedTextField rateField;
    private JFormattedTextField numPeriodsField;
    private JFormattedTextField paymentField;
    //Formats to format and parse numbers
    private NumberFormat amountFormat;
    private NumberFormat percentFormat;
    private NumberFormat paymentFormat;
    public FormattedTextFieldDemo() {
        super(new BorderLayout());
        setUpFormats();
        double payment = computePayment(amount,
                                        rate,
                                        numPeriods);
        //Create the labels.
        amountLabel = new JLabel(amountString);
        rateLabel = new JLabel(rateString);
        numPeriodsLabel = new JLabel(numPeriodsString);
        paymentLabel = new JLabel(paymentString);
        //Create the text fields and set them up.
        amountField = new JFormattedTextField(amountFormat);
        amountField.setValue(new Double(amount));
        amountField.setColumns(10);
        amountField.addPropertyChangeListener("value", this);
        rateField = new JFormattedTextField(percentFormat);
        rateField.setValue(new Double(rate));
        rateField.setColumns(10);
        rateField.addPropertyChangeListener("value", this);
        numPeriodsField = new JFormattedTextField();
        numPeriodsField.setValue(new Integer(numPeriods));
        numPeriodsField.setColumns(10);
        numPeriodsField.addPropertyChangeListener("value", this);
        paymentField = new JFormattedTextField(paymentFormat);
        paymentField.setValue(new Double(payment));
        paymentField.setColumns(10);
        paymentField.setEditable(false);
        paymentField.setForeground(Color.red);
        //Tell accessibility tools about label/textfield pairs.
        amountLabel.setLabelFor(amountField);
        rateLabel.setLabelFor(rateField);
        numPeriodsLabel.setLabelFor(numPeriodsField);
        paymentLabel.setLabelFor(paymentField);
        //Lay out the labels in a panel.
        JPanel labelPane = new JPanel(new GridLayout(0,1));
        labelPane.add(amountLabel);
        labelPane.add(rateLabel);
        labelPane.add(numPeriodsLabel);
        labelPane.add(paymentLabel);
        //Layout the text fields in a panel.
        JPanel fieldPane = new JPanel(new GridLayout(0,1));
        fieldPane.add(amountField);
        fieldPane.add(rateField);
        fieldPane.add(numPeriodsField);
        fieldPane.add(paymentField);
        //Put the panels in this panel, labels on left,
        //text fields on right.
        setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        add(labelPane, BorderLayout.CENTER);
        add(fieldPane, BorderLayout.LINE_END);
    }
    /** Called when a field's "value" property changes. */
    public void propertyChange(PropertyChangeEvent e) {
        Object source = e.getSource();
        if (source == amountField) {
            amount = ((Number)amountField.getValue()).doubleValue();
        } else if (source == rateField) {
            rate = ((Number)rateField.getValue()).doubleValue();
        } else if (source == numPeriodsField) {
            numPeriods = ((Number)numPeriodsField.getValue()).intValue();
        }
        double payment = computePayment(amount, rate, numPeriods);
        paymentField.setValue(new Double(payment));
    }
    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event dispatch thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("FormattedTextFieldDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //Add contents to the window.
        frame.add(new FormattedTextFieldDemo());
        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }
    public static void main(String[] args) {
        //Schedule a job for the event dispatch thread:
        //creating and showing this application's GUI.
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                //Turn off metal's use of bold fonts
            UIManager.put("swing.boldMetal", Boolean.FALSE);
                createAndShowGUI();
            }
        });
    }
    //Compute the monthly payment based on the loan amount,
    //APR, and length of loan.
    double computePayment(double loanAmt, double rate, int numPeriods) {
        double I, partial1, denominator, answer;
        numPeriods *= 12;        //get number of months
        if (rate > 0.01) {
            I = rate / 100.0 / 12.0;         //get monthly rate from annual
            partial1 = Math.pow((1 + I), (0.0 - numPeriods));
            denominator = (1 - partial1) / I;
        } else { //rate ~= 0
            denominator = numPeriods;
        }
        answer = (-1 * loanAmt) / denominator;
        return answer;
    }
    //Create and set up number formats. These objects also
    //parse numbers input by user.
    private void setUpFormats() {
        amountFormat = NumberFormat.getNumberInstance();
        percentFormat = NumberFormat.getNumberInstance();
        percentFormat.setMinimumFractionDigits(3);
        paymentFormat = NumberFormat.getCurrencyInstance();
    }
}

Nun meine Frage:
Dieser EventListener: warum reicht für amount eine zuweisung wie " amount = ((Number)amountField.getValue()).doubleValue();" und warum muss ich bei payment extra einen setValue machen: "

Code:
 double payment = computePayment(amount, rate, numPeriods);
        paymentField.setValue(new Double(payment));

???

Hier noch ein Link, falls jemand mehr infos braucht: https://docs.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html
https://docs.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html
Vielen Dank!
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
S 2D-Grafik Warum funktioniert dieser Code ? (GUI) AWT, Swing, JavaFX & SWT 9
Juelin if Abfrage funktioniert nicht richtig AWT, Swing, JavaFX & SWT 10
C Button ActionListener funktioniert nicht AWT, Swing, JavaFX & SWT 1
P AWT Programm funktioniert nicht richtig AWT, Swing, JavaFX & SWT 35
MartinNeuerlich Kann mir jemand, der einen Mac mit einem m1 oder m2-Chip hat, eine POM geben mit der Javafx-Fullscreen beim Mac mit m-Chip funktioniert? AWT, Swing, JavaFX & SWT 1
R auto. Importanweisungen für javafx funktioniert in Eclipse nicht mehr AWT, Swing, JavaFX & SWT 4
M Scrollbar funktioniert nicht AWT, Swing, JavaFX & SWT 10
D Repaint Funktioniert nicht AWT, Swing, JavaFX & SWT 2
W JEditorPane textausrichtung nach settext funktioniert nicht mehr AWT, Swing, JavaFX & SWT 11
H Swing Anpassen der Textgröße im JLabel funktioniert nur bedingt AWT, Swing, JavaFX & SWT 7
sserio JFrame setIconImmage() funktioniert nicht AWT, Swing, JavaFX & SWT 3
T Swing Tooltip-Delay funktioniert nicht immer. AWT, Swing, JavaFX & SWT 1
B Output GUI funktioniert nur beim ersten Mal richtig. AWT, Swing, JavaFX & SWT 4
sserio Wie funktioniert ein Controller bei JavaFx? AWT, Swing, JavaFX & SWT 1
U Wie funktioniert das rotieren unter 2dGraphics, also wie stelle ich z. B. 90° ein? AWT, Swing, JavaFX & SWT 1
U Wie funktioniert Polygon? AWT, Swing, JavaFX & SWT 1
U Wie genau funktioniert 2dgraphics, in diesem Bezug? AWT, Swing, JavaFX & SWT 4
S ChoiceBox aus ArrayList per setValue() mit Wert belegen funktioniert nicht. AWT, Swing, JavaFX & SWT 0
H KeyListener funktioniert nicht AWT, Swing, JavaFX & SWT 1
H BufferedImage zurücksetzen funktioniert nicht AWT, Swing, JavaFX & SWT 12
H RPG Programmieren, label.setLocation funktioniert nicht AWT, Swing, JavaFX & SWT 7
EinNickname9 Einfacher parser funktioniert nicht AWT, Swing, JavaFX & SWT 2
F Swing Adapt Row Height funktioniert nicht richtig :( AWT, Swing, JavaFX & SWT 7
P Swing jxmapviewer hinzufügen/nutzen funktioniert nicht AWT, Swing, JavaFX & SWT 7
CptK Wie funktioniert contains() für Path2D.Double AWT, Swing, JavaFX & SWT 10
J Anbindung Textfeldklasse an Table funktioniert nicht AWT, Swing, JavaFX & SWT 3
R Actionlistener funktioniert nicht AWT, Swing, JavaFX & SWT 4
B Stylen eines JTextPane funktioniert nicht AWT, Swing, JavaFX & SWT 1
VPChief Swing Eclipse: Nach Exportieren, Button funktioniert nicht mehr AWT, Swing, JavaFX & SWT 26
H Bewegung funktioniert nicht AWT, Swing, JavaFX & SWT 3
N Pixelfarbe abgleichen funktioniert nicht AWT, Swing, JavaFX & SWT 5
A Swing JTextField an Button übergeben für Popup-Fenster funktioniert nicht AWT, Swing, JavaFX & SWT 3
N eclipse Java, bilder benutzten Funktioniert nicht AWT, Swing, JavaFX & SWT 6
Zrebna JavaFX-Projekt mit Bildern funktioniert nicht - um Hilfe wird gebeten AWT, Swing, JavaFX & SWT 14
steven789hjk543 Swing Weiß jemand, warum dieses Programm nicht funktioniert? AWT, Swing, JavaFX & SWT 7
M Swing setMaximumSize funktioniert nicht AWT, Swing, JavaFX & SWT 1
K JavaFX funktioniert nicht AWT, Swing, JavaFX & SWT 2
B AWT actionPerformed Method funktioniert nicht AWT, Swing, JavaFX & SWT 12
L JavaFX Drag and Drop funktioniert nicht AWT, Swing, JavaFX & SWT 3
M Swing Code funktioniert auf Windows aber nicht Linux... AWT, Swing, JavaFX & SWT 3
T LookAndFeel LookAndFeel funktioniert nicht beim JFrame wechsel AWT, Swing, JavaFX & SWT 3
J JavaFX addListener funktioniert nicht AWT, Swing, JavaFX & SWT 1
P CardLayout funktioniert fehlerhaft AWT, Swing, JavaFX & SWT 13
L WrapLayout funktioniert nicht AWT, Swing, JavaFX & SWT 1
kodela Accalerator für einige Menüoptionen funktioniert nicht mehr AWT, Swing, JavaFX & SWT 3
S JavaFX mit javac compilieren funktioniert nicht AWT, Swing, JavaFX & SWT 2
K Swing Entfernen von Panel funktioniert nicht AWT, Swing, JavaFX & SWT 5
J AWT System Farben / java.awt.SystemColor funktioniert nicht AWT, Swing, JavaFX & SWT 4
G Swing Swing Binding JList funktioniert nicht AWT, Swing, JavaFX & SWT 5
it_is_all ActionListener umlenken/ updaten mit AddActionListener funktioniert nicht AWT, Swing, JavaFX & SWT 3
K javafx app > "run in browser" funktioniert nicht AWT, Swing, JavaFX & SWT 3
N JavaFX GridPane Halignment funktioniert nicht AWT, Swing, JavaFX & SWT 1
it_is_all JLabel.setIcon - funktioniert nicht mehr AWT, Swing, JavaFX & SWT 2
R Ausgabe über JOptionPane.showMessageDialog funktioniert nicht AWT, Swing, JavaFX & SWT 2
L 2D-Grafik Frage zu Ellipse2D.Double, Abfrage, ob Punkt enthalten ist funktioniert nicht AWT, Swing, JavaFX & SWT 3
J JTable Selection Listener funktioniert nicht AWT, Swing, JavaFX & SWT 4
F "ActionListener" funktioniert nicht AWT, Swing, JavaFX & SWT 4
Z BoxLayout funktioniert nicht und Buttongröße AWT, Swing, JavaFX & SWT 18
C Java Hintergrund funktioniert nicht AWT, Swing, JavaFX & SWT 9
GreenTeaYT Button funktioniert nicht für Ein-und Auszahlungen? AWT, Swing, JavaFX & SWT 8
C Keylistener funktioniert nicht AWT, Swing, JavaFX & SWT 1
A Swing Programm funktioniert aber zwei Buttons werden angezeigt AWT, Swing, JavaFX & SWT 3
M UIManager funktioniert nicht mit Farben AWT, Swing, JavaFX & SWT 9
M Swing UIManager funktioniert nicht mit Farben AWT, Swing, JavaFX & SWT 0
T KeyListener funktioniert nicht wenn ich ein JButton hinzufüge AWT, Swing, JavaFX & SWT 1
S JavaFX wie funktioniert CSS und javaFX AWT, Swing, JavaFX & SWT 19
S KeyListener funktioniert nicht AWT, Swing, JavaFX & SWT 2
J JScrollPane funktioniert nicht AWT, Swing, JavaFX & SWT 5
M JavaFX Eventhandler Funktioniert nicht AWT, Swing, JavaFX & SWT 3
Sogomn JavaFX Injektion funktioniert nicht AWT, Swing, JavaFX & SWT 4
Blender3D Swing KeyBoardListener funktioniert nicht unter UBUNTU AWT, Swing, JavaFX & SWT 7
J JavaFX Button funktioniert(nicht) AWT, Swing, JavaFX & SWT 3
J DefaultCloseOperation funktioniert nicht AWT, Swing, JavaFX & SWT 9
F JavaFX ObservableList -- warum funktioniert das so.... AWT, Swing, JavaFX & SWT 3
R Swing Bewegung eines Graphics Objektes innerhalb eines JPanels funktioniert nicht richtig AWT, Swing, JavaFX & SWT 2
Paul15 Button funktioniert nur einmal AWT, Swing, JavaFX & SWT 9
D Swing Warum erhalte ich keine Ausgabe? Funktioniert der equals-vergleich in actionPeformed nicht richtig? AWT, Swing, JavaFX & SWT 3
H UpdatePolicy wird nicht richtig gesetzt / funktioniert nicht AWT, Swing, JavaFX & SWT 5
W Paint-Methode in anderer Klasse funktioniert nicht AWT, Swing, JavaFX & SWT 7
N KeyListener funktioniert nicht richitg AWT, Swing, JavaFX & SWT 4
L Event Handling wie funktioniert .getComponent? AWT, Swing, JavaFX & SWT 1
B JavaFX Scene Builder: resize funktioniert (meist) nicht AWT, Swing, JavaFX & SWT 6
J Swing Basics - JButton funktioniert nicht. AWT, Swing, JavaFX & SWT 1
D Passwort [Aber mit 3 Versuchen] Funktioniert nicht wie erwartet AWT, Swing, JavaFX & SWT 4
C Applet Applet funktioniert in Eclipse aber nicht in Browser AWT, Swing, JavaFX & SWT 1
J Zellen in JavaFx einfärben funktioniert nicht AWT, Swing, JavaFX & SWT 1
S Swing Warum funktioniert der automatische Zeilenumbruch mit arabischen Zeichen beim JTextPane nicht AWT, Swing, JavaFX & SWT 3
Joew0815 JDialog repaint() funktioniert nicht wie gewünscht. AWT, Swing, JavaFX & SWT 2
C KeyPressed funktioniert auf iOS nicht richtig? AWT, Swing, JavaFX & SWT 4
D Event Handling MouseListener funktioniert seit Java 8 nicht mehr AWT, Swing, JavaFX & SWT 13
M LayoutManager GridLayout funktioniert nicht AWT, Swing, JavaFX & SWT 0
I Objekte aus contentPane löschen funktioniert nicht AWT, Swing, JavaFX & SWT 3
F JavaFX Antialiasing funktioniert nicht? AWT, Swing, JavaFX & SWT 8
F CardLayout.show() funktioniert teilweise nicht AWT, Swing, JavaFX & SWT 5
M Listener funktioniert nicht AWT, Swing, JavaFX & SWT 7
M "Update" der JTable funktioniert nicht AWT, Swing, JavaFX & SWT 2
T JAXB funktioniert ohne IDE nicht verlässlich AWT, Swing, JavaFX & SWT 12
C Repaint() funktioniert nicht in TabbedPanel AWT, Swing, JavaFX & SWT 5
T LayoutManager GridLayout panel.setSize funktioniert nicht AWT, Swing, JavaFX & SWT 1
R JavaFX Stage.close() funktioniert nicht im jar-File AWT, Swing, JavaFX & SWT 2

Ähnliche Java Themen

Neue Themen


Oben