当我操作ActionListener时,为什么我的原始组件会消失?

时间:2014-11-26 09:33:56

标签: java swing user-interface graphics

----------------------------------------------- --------- EDITED ---------------------------------------- --------------------------

添加部分现在可以正常使用按钮!

我使用相同的逻辑添加了新实现,但这一次,删除刚刚添加的部分。我试了一下,但问题是它没有删除刚刚添加的部分。

另外,我想在添加部分时使用g.addString但是我该怎么做呢?非常感谢你!

import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Main {

    private JFrame jf;
    private JTextField jtf1;
    private JTextField jtf2;
    private Panel p;
    private JComboBox jcb1;
    private JComboBox jcb2;
    private JButton button;
    private String tools[] = {""};
    //ActionListener Variables
    private int string1 = 170;
    private int string2 = 170;
    private int yJtf1 = 140;
    private int yJtf2 = 165;
    private int cb1 = 143;
    private int cb2 = 168;
    private int count = 0;

    public Main() {
        jf = new JFrame();
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setSize(700, 700);
        p = new Panel();
        jtf1 = new JTextField("", 20);
        jtf2 = new JTextField("", 20);

        jcb1 = new JComboBox(tools);
        jcb2 = new JComboBox(tools);
        button = new JButton("+");
        p.add(jtf1);
        jtf1.setBounds(75, 30, 135, 25);
        p.add(jtf2);
        jtf2.setBounds(75, 60, 135, 25);
        p.add(button);
        plusButton.setBounds(350, 153, 41, 25);
        button.addActionListener(new ButtonClicked());
        p.add(button2);
        plusButton.setBounds(350, 153, 41, 25);
        minusButton.addActionListener(new NewButtonClicked());
        p.add(jcb1);
        jcb1.setBounds(80, 143, 80, 20);
        p.add(jcb2);
        jcb2.setBounds(80, 168, 80, 20);
        jf.add(p);
        jf.setVisible(true);
    `
    }

public class Panel extends JPanel {

        public Panel() {
            this.setLayout(null);
        }

        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Font font = new Font("Arial", Font.BOLD, 12);
            g.setFont(font);
            g.drawString("Things:", 27, 45);
            g.drawString("Price:", 27, 75);
        }
    }

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

    class ButtonClicked implements ActionListener {

        public void actionPerformed(ActionEvent ae) {
            if (count < 3) {

                string1 += 60;
                string2 += 60;
                jtf1 += 60;
                jtf2 += 60;
                cb1 += 60;
                cb2 += 59;

                JTextField jtf1 = new JTextField("", 20);
                jtf1.setBounds(40, yJtf1, 40, 25);
                p.add(jtf1);

                JTextField jtf2 = new JTextField("", 20);
                jtf2.setBounds(40, yJtf2, 40, 25);
                p.add(jtf2);

                JComboBox jcb1 = new JComboBox(tools);
                jcb1.setBounds(80, cb1, 80, 20);
                p.add(jcb1);

                JComboBox jcb2 = new JComboBox(tools);
                jcb2.setBounds(80, cb2, 80, 20);
                p.add(jcb2);

------------->>>//NEW I WOULD LIKE TO IMPLEMENT (BUT HOW DO I USE 'g' TO ADD TO ANOTHER PANEL?)
                Font font = new Font("TimesRoman", Font.BOLD, 12);
                g.setFont(font);
                g.drawString("Things:", 7, string1);
                g.drawString("Price:", 165, string2);

                p.revalidate();
                p.repaint();

                count++;
            }
        }
    }
------------->>>//////NEWLY EDITED IMPLEMENTATION (DELETING SECTION)
    class NewButtonClicked implements ActionListener {

        public void actionPerformed(ActionEvent ae) {
            if (count > 0 && count < 4) {

                JTextField jtf1 = new JTextField("", 20);
                JTextField jtf2 = new JTextField("", 20);          
                JComboBox jcb1 = new JComboBox(tools);
                JComboBox jcb2 = new JComboBox(tools);

                p.remove(jtf1);
                p.remove(jtf2);
                p.remove(jcb1);
                p.remove(jcb2);

                jtf1 -= 60;
                jtf2 -= 60;
                cb1 -= 60;
                cb2 -= 59;

                p.revalidate();
                p.repaint();

                count--;
            }
        }
    }

}

1 个答案:

答案 0 :(得分:0)

所以我运行你的代码,我改变了一点代码结构,并对重点进行了评论:

public class Main {

    private JFrame jf;
    private Panel p;
    private JButton button;
    private Object options[];
    private String[] tools = {"value1","value2"}; // here put the different values you need in your combobox
    //ActionListener Variables
    private int string1 = 170;
    private int string2 = 170;
    private int yJtf1 = 80;
    private int yJtf2 = 105;
    private int cb1y = 83;
    private int cb2y = 108;
    private int count = 0;
    private List<JComboBox<String>> cbs = new ArrayList<JComboBox<String>>(); //Keep References to your comboBoxs
    private List<JTextField> tfs = new ArrayList<JTextField>(); //Keep References to your TextFields

    public Main() {
        initView();
    }

    //Method to create the view
    public void initView(){
        jf = new JFrame();
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setSize(700, 700);
        p = new Panel();
        JTextField thingsTextField = new JTextField("", 20);
        thingsTextField.setBounds(75, 30, 135, 25);
        JTextField priceTextField = new JTextField("", 20);
        priceTextField.setBounds(75, 60, 135, 25);
        p.add(thingsTextField);
        p.add(priceTextField);
        addSection();
        button = new JButton("+");
        button.setBounds(350, 153, 41, 25);
        button.addActionListener(new ButtonClicked());
        p.add(button);
        jf.add(p);
        jf.setVisible(true);

        //You have to call those methods to redraw the panel
        p.revalidate();
        p.repaint();
    }
    public static void main(String[] args) {
        new Main();
    }

    //This method is called to add a section with the "Plus" Button
    public void addSection(){
        if (count < 3) {
            string1 += 60;
            string2 += 60;
            yJtf1 += 60;
            yJtf2 += 60;
            cb1y += 60;
            cb2y += 59;
            JTextField jtf1 = new JTextField("", 20);
            jtf1.setBounds(40, yJtf1, 40, 25);

            JTextField jtf2 = new JTextField("", 20);
            jtf2.setBounds(40, yJtf2, 40, 25);
            tfs.add(jtf1);
            tfs.add(jtf2);
            p.add(jtf1);
            p.add(jtf2);

            JComboBox<String> cb1 = new JComboBox<>(tools);
            cb1.setBounds(80, cb1y, 80, 20);
            JComboBox<String> cb2 = new JComboBox<>(tools);
            cb2.setBounds(80, cb2y, 80, 20);
            p.add(cb1);
            p.add(cb2);
            cbs.add(cb1);
            cbs.add(cb2);
            p.revalidate();
            p.repaint();
            //Font font = new Font("TimesRoman", Font.BOLD, 18);
            //g.setFont(font);


        }
        count++;
    }
    class ButtonClicked implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent ae) {
            addSection();
        }


    }

    public class Panel extends JPanel {

        public Panel() {
            this.setLayout(null);
        }

        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Font font = new Font("Arial", Font.BOLD, 12);
            g.setFont(font);
            g.drawString("Things:", 27, 45);
            g.drawString("Price:", 27, 75);

        }
    }

}