Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法设置textLabel,因为它与parseDouble冲突_Java_Swing_Parsing - Fatal编程技术网

Java 无法设置textLabel,因为它与parseDouble冲突

Java 无法设置textLabel,因为它与parseDouble冲突,java,swing,parsing,Java,Swing,Parsing,我有一个ItemListener,如下所示: private class Listener implements ItemListener { public void itemStateChanged(ItemEvent e) { calculate(); } } subtotalLbl.setText("\t\t\t\t\t\t\t\tSubtotal:\t\t\t\t\t\t\t\t\t " + String.valueOf(determinedSu

我有一个ItemListener,如下所示:

private class Listener implements ItemListener
{
    public void itemStateChanged(ItemEvent e)
    {
        calculate();
    }
}
subtotalLbl.setText("\t\t\t\t\t\t\t\tSubtotal:\t\t\t\t\t\t\t\t\t " + String.valueOf(determinedSubTotal + priceIncrease) + "\t\t\t\t\t\t\t\t\t");
taxLbl.setText("\t\t\t\t\t\t\t\tTax:\t\t\t\t\t\t\t\t\t " + String.valueOf(determinedTax + priceIncrease) + "\t\t\t\t\t\t\t\t\t");
totalLbl.setText("\t\t\t\t\t\t\tTotal:\t\t\t\t\t\t\t\t\t " + String.valueOf(determinedTotal + priceIncrease) + "\t\t\t\t\t\t\t\t\t");
在我的
calculate()
方法的底部,我设置了如下标签:

private class Listener implements ItemListener
{
    public void itemStateChanged(ItemEvent e)
    {
        calculate();
    }
}
subtotalLbl.setText("\t\t\t\t\t\t\t\tSubtotal:\t\t\t\t\t\t\t\t\t " + String.valueOf(determinedSubTotal + priceIncrease) + "\t\t\t\t\t\t\t\t\t");
taxLbl.setText("\t\t\t\t\t\t\t\tTax:\t\t\t\t\t\t\t\t\t " + String.valueOf(determinedTax + priceIncrease) + "\t\t\t\t\t\t\t\t\t");
totalLbl.setText("\t\t\t\t\t\t\tTotal:\t\t\t\t\t\t\t\t\t " + String.valueOf(determinedTotal + priceIncrease) + "\t\t\t\t\t\t\t\t\t");
然后,我有一个ActionListener,它使用
totalBL
中的文本进行语法分析

private class BtnClicked implements ActionListener
{       
    public void actionPerformed(ActionEvent e)
    {
        String input = totalLbl.getText().trim();
        Double parsedString = Double.parseDouble(input) * 0.20; 

        Object src = e.getSource();

        if(src == submit)
        {
            JOptionPane.showMessageDialog(null,"Thank you for your order - the tip will be " + fmt.format(parsedString), "Thank you" , JOptionPane.INFORMATION_MESSAGE);
        }
        else if(src == cancel)
        {
            JOptionPane.showMessageDialog(null,"Order was canceled" ,"Order Canceled" , JOptionPane.INFORMATION_MESSAGE);
        }
    }
显然,程序在
BtnClicked
actionPerformed
方法内的行崩溃,其中
parseDouble(input)
位于该行,因为
totalbl
JLabel中有“Total:”。。我还能在哪里设置这个,或者如何解决这个问题?“总计:”是必需的。(无法使用
split()

下面是整个
JFrame
的屏幕截图,单击submit按钮时程序崩溃:


有很多不同的方法可以做到这一点,最简单的方法可能是:

String[] parts = totalLbl.getText().split(":");
String input = parts[1].trim();
Double parsedString = Double.parseDouble(input) * 0.20; 

有很多不同的方法可以做到这一点,最简单的方法可能是:

String[] parts = totalLbl.getText().split(":");
String input = parts[1].trim();
Double parsedString = Double.parseDouble(input) * 0.20; 
你可以这样做

str = str.replaceAll("\\D+","");
这将从字符串中删除非数字 所以你希望它是这样的

 Double parsedString = Double.parseDouble(input.replaceAll("\\D+","")*0.20);
你可以这样做

str = str.replaceAll("\\D+","");
这将从字符串中删除非数字 所以你希望它是这样的

 Double parsedString = Double.parseDouble(input.replaceAll("\\D+","")*0.20);

创建两个
JLable
s,一个写着
Total:
另一个实际上保存着总值

所以你的总计算看起来更像

totalLblText.setText("Total:");
totalLbl.setText(String.valueOf(determinedTotal + priceIncrease));
那你就不必在意了

您应该更好地使用布局管理器,以支持您尝试实现的格式设置,而不是使用像
\t
这样的格式设置字符,这些字符最终总是会乱七八糟

使用布局示例更新

这个简单的示例演示了如何使用布局管理器(以及一种称为复合布局的技术),并且不再需要尝试使用单个标签来显示更多信息

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestLayout {

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

    public TestLayout() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JTextField numberOfPizzas;
        private JCheckBox pepperoni;
        private JCheckBox sausage;
        private JCheckBox peppers;
        private JCheckBox onions;
        private JCheckBox mushrooms;
        private JCheckBox extracheese;

        private JLabel lblSubTotal;
        private JLabel lblTax;
        private JLabel lblTotal;

        public TestPane() {

            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.gridwidth = 1;
            gbc.weightx = 1;
            gbc.fill = GridBagConstraints.HORIZONTAL;

            JPanel header = new JPanel();
            JPanel extras = new JPanel(new GridBagLayout());
            JPanel totals = new JPanel(new GridBagLayout());

            add(header, gbc);
            gbc.gridy++;
            add(extras, gbc);
            gbc.gridy++;
            add(totals, gbc);

            numberOfPizzas = new JTextField(5);
            header.add(new JLabel("Number of pizzas"));
            header.add(numberOfPizzas);

            gbc = new GridBagConstraints();

            pepperoni = new JCheckBox("Pepperoni");
            sausage = new JCheckBox("Sausage");
            peppers = new JCheckBox("Peppers");
            onions = new JCheckBox("Onions");
            mushrooms = new JCheckBox("mushrooms");
            extracheese = new JCheckBox("Extra Cheeses");

            JCheckBox left[] = new JCheckBox[] {pepperoni, peppers, mushrooms};
            JCheckBox right[] = new JCheckBox[] {sausage, onions, extracheese};

            gbc.anchor = GridBagConstraints.WEST;
            gbc.gridx = 0;
            gbc.gridy = 0;
            add(left, extras, 0, 1, gbc);
            gbc.gridx = 1;
            gbc.gridy = 0;
            add(right, extras, 0, 1, gbc);

            gbc = new GridBagConstraints();
            gbc.weightx = 1;
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.EAST;
            gbc.insets = new Insets(2, 12, 2, 12);

            totals.add(new JLabel("Subtotal:"), gbc);
            gbc.gridy++;
            totals.add(new JLabel("Tax:"), gbc);
            gbc.gridy++;
            totals.add(new JLabel("Total:"), gbc);

            gbc.weightx = 0;
            gbc.gridx = 1;
            gbc.gridy = 0;

            lblSubTotal = new JLabel("8.0");
            lblTax = new JLabel("0.78");
            lblTotal = new JLabel("8.78");

            totals.add(lblSubTotal, gbc);
            gbc.gridy++;
            totals.add(lblTax, gbc);
            gbc.gridy++;
            totals.add(lblTotal, gbc);

        }

        protected void add(JComponent[] comps, JComponent parent, int deltaX, int deltaY, GridBagConstraints gbc) {

            for (JComponent comp : comps) {
                parent.add(comp, gbc);
                gbc.gridy += deltaY;
                gbc.gridx += deltaX;
            }

        }
    }

}


创建两个
JLable
s,一个写着
Total:
另一个实际上保存着总值

所以你的总计算看起来更像

totalLblText.setText("Total:");
totalLbl.setText(String.valueOf(determinedTotal + priceIncrease));
那你就不必在意了

您应该更好地使用布局管理器,以支持您尝试实现的格式设置,而不是使用像
\t
这样的格式设置字符,这些字符最终总是会乱七八糟

使用布局示例更新

这个简单的示例演示了如何使用布局管理器(以及一种称为复合布局的技术),并且不再需要尝试使用单个标签来显示更多信息

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestLayout {

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

    public TestLayout() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JTextField numberOfPizzas;
        private JCheckBox pepperoni;
        private JCheckBox sausage;
        private JCheckBox peppers;
        private JCheckBox onions;
        private JCheckBox mushrooms;
        private JCheckBox extracheese;

        private JLabel lblSubTotal;
        private JLabel lblTax;
        private JLabel lblTotal;

        public TestPane() {

            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.gridwidth = 1;
            gbc.weightx = 1;
            gbc.fill = GridBagConstraints.HORIZONTAL;

            JPanel header = new JPanel();
            JPanel extras = new JPanel(new GridBagLayout());
            JPanel totals = new JPanel(new GridBagLayout());

            add(header, gbc);
            gbc.gridy++;
            add(extras, gbc);
            gbc.gridy++;
            add(totals, gbc);

            numberOfPizzas = new JTextField(5);
            header.add(new JLabel("Number of pizzas"));
            header.add(numberOfPizzas);

            gbc = new GridBagConstraints();

            pepperoni = new JCheckBox("Pepperoni");
            sausage = new JCheckBox("Sausage");
            peppers = new JCheckBox("Peppers");
            onions = new JCheckBox("Onions");
            mushrooms = new JCheckBox("mushrooms");
            extracheese = new JCheckBox("Extra Cheeses");

            JCheckBox left[] = new JCheckBox[] {pepperoni, peppers, mushrooms};
            JCheckBox right[] = new JCheckBox[] {sausage, onions, extracheese};

            gbc.anchor = GridBagConstraints.WEST;
            gbc.gridx = 0;
            gbc.gridy = 0;
            add(left, extras, 0, 1, gbc);
            gbc.gridx = 1;
            gbc.gridy = 0;
            add(right, extras, 0, 1, gbc);

            gbc = new GridBagConstraints();
            gbc.weightx = 1;
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.EAST;
            gbc.insets = new Insets(2, 12, 2, 12);

            totals.add(new JLabel("Subtotal:"), gbc);
            gbc.gridy++;
            totals.add(new JLabel("Tax:"), gbc);
            gbc.gridy++;
            totals.add(new JLabel("Total:"), gbc);

            gbc.weightx = 0;
            gbc.gridx = 1;
            gbc.gridy = 0;

            lblSubTotal = new JLabel("8.0");
            lblTax = new JLabel("0.78");
            lblTotal = new JLabel("8.78");

            totals.add(lblSubTotal, gbc);
            gbc.gridy++;
            totals.add(lblTax, gbc);
            gbc.gridy++;
            totals.add(lblTotal, gbc);

        }

        protected void add(JComponent[] comps, JComponent parent, int deltaX, int deltaY, GridBagConstraints gbc) {

            for (JComponent comp : comps) {
                parent.add(comp, gbc);
                gbc.gridy += deltaY;
                gbc.gridx += deltaX;
            }

        }
    }

}


您可以为标签和值使用单独的小部件。例如,在total标签中,创建一个JLabel对象并将文本设置为静态值“total:”,然后为total值创建一个JTextField对象并将文本设置为实际值。提交时,从文本字段而不是标签中获取值。不要忘记调用textfield的setEditable(false),因为textfield只显示值,不接受输入。

您可以为标签和值使用单独的小部件。例如,在total标签中,创建一个JLabel对象并将文本设置为静态值“total:”,然后为total值创建一个JTextField对象并将文本设置为实际值。提交时,从文本字段而不是标签中获取值。别忘了调用textfield的setEditable(false),因为textfield只显示值,不接受输入。

当然,我正在深入iOS和Android应用程序开发:)这对我来说是很好的材料。绝对,我正在深入研究iOS和Android应用程序开发:)这对我来说是很好的材料。