将值从子面板传递到父面板

时间:2017-10-23 04:31:25

标签: java swing

我在尝试将值从孩子传递到父级面板时遇到了麻烦,并希望能够获得一些见解。我的主面板上有一个标签式面板,可以固定一个带有两个JCheckbox按钮的面板。我试图将我分配给复选框的值传递给选项卡式面板,我可以在其中添加一些总计,然后将新值从选项卡式面板传递到主面板。

这是我的主要小组。

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

public class MasterPanel extends JPanel 
{   
    private JTabbedPane tp; 

    public MasterPanel () 
    {
        setLayout (new BorderLayout ());
        //------------------------------------------------------------------
        // CENTER - displays the tabs with selections for the order
        //------------------------------------------------------------------
        tp = new JTabbedPane();
        tp.addTab("Pizza", new PizzaOptionsPanel());
        add (tp, BorderLayout.CENTER);

        //-----------------------------------------------------------------
        // LINE_END (right) - displays running total for order
        //-----------------------------------------------------------------    
        add(new OrderSummaryPanel(),BorderLayout.LINE_END);
    }

    public void update()
    {
        OrderSummaryPanel.update("Pizza Subtotal: " + PizzaOptionsPanel.getValue());
    }

}

我想要添加多个值的中间面板,并将总数发送到主面板。

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

public class PizzaOptionsPanel extends JPanel {

    private JPanel size, crust, sauce, toppings;
    private int pizzaSubTotal, value;

    public PizzaOptionsPanel() {
        setLayout(new GridLayout (1, 4));
        size = new PizzaSizePanel();
        add(size);
    }

    public int getValue()
    {
        return value;
    }
}

和我的内部pannel,我试图将值从复选框传递给父。

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

public class PizzaSizePanel extends JPanel
{
    private JLabel label;
    private JCheckBox size1, size2, size3, size4;
    private int sizeTotal;
    private PizzaOptionsPanel parent;

    public PizzaSizePanel() 
    {
        sizeTotal = 0;
        setBorder(BorderFactory.createRaisedBevelBorder());
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        //creates size options
        label = new JLabel ("Pizza Size:");
        label.setFont(new Font("Helvetica", Font.BOLD, 18));
        size1 = new MyJCheckBox ("10 inch:  $9", false, 9);
        size2 = new MyJCheckBox ("12 inch:  $11", false, 11);

        ButtonGroup SizeGroup = new ButtonGroup();
        SizeGroup.add (size1);
        SizeGroup.add (size2);

        add(label);
        add(size1);
        add(size2);

        size1.addActionListener(new PizzaListener());
        size2.addActionListener(new PizzaListener());
    }

    private class MyJCheckBox extends JCheckBox
    {
        private int value;
        public MyJCheckBox(String text, boolean checked, int newValue)
        {
            super(text, checked);
            value = newValue;
        }

        public int getValue()
        {
        return value;
        }
    }

    private class PizzaListener implements ActionListener
    {
        public void actionPerformed(ActionEvent ae)
        {
            MyJCheckBox temp = (MyJCheckBox)ae.getSource();
            boolean selected = (temp).isSelected();
            int val = temp.getValue();

            if(temp == size1)
                sizeTotal = val;
            else if(temp == size2)
                sizeTotal = val;
        }
    }

}

我相信这是一个继承问题吗?

我尝试使用一些getValue方法,也许这是正确的尝试方法,但有可能我做错了吗?

1 个答案:

答案 0 :(得分:2)

您要做的第一件事是定义某种模型,该模型代表您尝试捕获/管理的数据。

public interface PizzaOrder {
    public int getSize();
    public int getCrust();
    public int getSauce();
    public String[] getToppings();

    public void setSize(int size);
    public void setCrust(int crust);
    public void setSauce(int sauce);
    public void addTopping(String topping);
    public void removeTopping(String topping);

    public void addChangeListener(ChangeListener listener);
    public void removeChangeListener(ChangeListener listener);
}

在我看来,这还应该包括一个“计数”方法,它可以计算选项的计数,这使它成为自包含。问题是,您如何提供价格信息?嗯,令人惊讶的是,这将与另一个模型相结合,根据业务规则管理项目及其成本/乘数。

这意味着价格信息与订单信息分开,但可以用它来执行它的工作,进一步解除状态。

然后,您可以实现模型

public class DefaultPizzaOrder implements PizzaOrder {

    private List<ChangeListener> listeners = new ArrayList<>(5);

    private int size;
    private int crust;
    private int sauce;
    private List<String> toppings = new ArrayList<>(25);

    @Override
    public int getSize() {
        return size;
    }

    @Override
    public int getCrust() {
        return crust;
    }

    @Override
    public int getSauce() {
        return sauce;
    }

    @Override
    public String[] getToppings() {
        return toppings.toArray(new String[toppings.size()]);
    }

    @Override
    public void addChangeListener(ChangeListener listener) {
        listeners.add(listener);
    }

    @Override
    public void removeChangeListener(ChangeListener listener) {
        listeners.remove(listener);
    }

    @Override
    public void setSize(int size) {
        this.size = size;
        fireStateChanged();
    }

    @Override
    public void setCrust(int crust) {
        this.crust = crust;
        fireStateChanged();
    }

    @Override
    public void setSauce(int sauce) {
        this.sauce = sauce;
        fireStateChanged();
    }

    @Override
    public void addTopping(String topping) {
        toppings.add(topping);
        fireStateChanged();
    }

    @Override
    public void removeTopping(String topping) {
        toppings.remove(topping);
        fireStateChanged();
    }

    protected void fireStateChanged() {
        ChangeEvent evt = new ChangeEvent(this);
        for (ChangeListener listener : listeners) {
            listener.stateChanged(evt);
        }
    }
}

这是非常基本的,但总体思路就在那里。

然后,在您的代码中,您创建模型的实例并将其传递给需要它的所有组件。然后,组件与模型交互,根据需要更新它(或绘制信息)并触发感兴趣的各方可能感兴趣的适当事件

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class Test {

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

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

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

    public class MasterPanel extends JPanel {

        private JTabbedPane tp;

        private PizzaOrder order;

        public MasterPanel() {
            order = new DefaultPizzaOrder();
            order.addChangeListener(new ChangeListener() {
                @Override
                public void stateChanged(ChangeEvent e) {
                    update();
                }
            });
            setLayout(new BorderLayout());
            //------------------------------------------------------------------
            // CENTER - displays the tabs with selections for the order
            //------------------------------------------------------------------
            tp = new JTabbedPane();
            tp.addTab("Pizza", new PizzaOptionsPanel(order));
            add(tp, BorderLayout.CENTER);

            //-----------------------------------------------------------------
            // LINE_END (right) - displays running total for order
            //-----------------------------------------------------------------    
            add(new OrderSummaryPanel(), BorderLayout.LINE_END);
        }

        public void update() {
            OrderSummaryPanel.update("Pizza Subtotal: " + PizzaOptionsPanel.getValue());
        }

    }

    public interface PizzaOrder {
        public int getSize();
        public int getCrust();
        public int getSauce();
        public String[] getToppings();

        public void setSize(int size);
        public void setCrust(int crust);
        public void setSauce(int sauce);
        public void addTopping(String topping);
        public void removeTopping(String topping);

        public void addChangeListener(ChangeListener listener);
        public void removeChangeListener(ChangeListener listener);
    }

    public class DefaultPizzaOrder implements PizzaOrder {

        private List<ChangeListener> listeners = new ArrayList<>(5);

        private int size;
        private int crust;
        private int sauce;
        private List<String> toppings = new ArrayList<>(25);

        @Override
        public int getSize() {
            return size;
        }

        @Override
        public int getCrust() {
            return crust;
        }

        @Override
        public int getSauce() {
            return sauce;
        }

        @Override
        public String[] getToppings() {
            return toppings.toArray(new String[toppings.size()]);
        }

        @Override
        public void addChangeListener(ChangeListener listener) {
            listeners.add(listener);
        }

        @Override
        public void removeChangeListener(ChangeListener listener) {
            listeners.remove(listener);
        }

        @Override
        public void setSize(int size) {
            this.size = size;
            fireStateChanged();
        }

        @Override
        public void setCrust(int crust) {
            this.crust = crust;
            fireStateChanged();
        }

        @Override
        public void setSauce(int sauce) {
            this.sauce = sauce;
            fireStateChanged();
        }

        @Override
        public void addTopping(String topping) {
            toppings.add(topping);
            fireStateChanged();
        }

        @Override
        public void removeTopping(String topping) {
            toppings.remove(topping);
            fireStateChanged();
        }

        protected void fireStateChanged() {
            ChangeEvent evt = new ChangeEvent(this);
            for (ChangeListener listener : listeners) {
                listener.stateChanged(evt);
            }
        }
    }

    public class PizzaOptionsPanel extends JPanel {

        private JPanel size, crust, sauce, toppings;
        private int pizzaSubTotal, value;

        private PizzaOrder order;

        public PizzaOptionsPanel(PizzaOrder order) {
            this.order = order;
            setLayout(new GridLayout(1, 4));
            size = new PizzaSizePanel(order);
            add(size);
        }

        public int getValue() {
            return value;
        }
    }

    public class PizzaSizePanel extends JPanel {

        private JLabel label;
        private JCheckBox size1, size2, size3, size4;
        private int sizeTotal;
        private PizzaOptionsPanel parent;
        private PizzaOrder order;

        public PizzaSizePanel(PizzaOrder order) {
            this.order = order;
            sizeTotal = 0;
            setBorder(BorderFactory.createRaisedBevelBorder());
            setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
            //creates size options
            label = new JLabel("Pizza Size:");
            label.setFont(new Font("Helvetica", Font.BOLD, 18));
            size1 = new MyJCheckBox("10 inch:  $9", false, 9);
            size2 = new MyJCheckBox("12 inch:  $11", false, 11);

            ButtonGroup SizeGroup = new ButtonGroup();
            SizeGroup.add(size1);
            SizeGroup.add(size2);

            add(label);
            add(size1);
            add(size2);

            size1.addActionListener(new PizzaListener());
            size2.addActionListener(new PizzaListener());
        }

        private class MyJCheckBox extends JCheckBox {

            private int value;

            public MyJCheckBox(String text, boolean checked, int newValue) {
                super(text, checked);
                value = newValue;
            }

            public int getValue() {
                return value;
            }
        }

        private class PizzaListener implements ActionListener {

            public void actionPerformed(ActionEvent ae) {
                MyJCheckBox temp = (MyJCheckBox) ae.getSource();
                boolean selected = (temp).isSelected();
                int val = temp.getValue();

                order.setSize(val);
            }
        }

    }
}

这是模型/视图范例的合理简单示例,具有观察者模式