【问题标题】:Why do my original components disappear when I operate ActionListener?为什么我操作ActionListener时我原来的组件消失了?
【发布时间】:2014-11-26 09:33:56
【问题描述】:

----------------------------------- ---------已编辑---------------------------------------- --------------------------

添加部分现在可以很好地使用按钮!

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

另外,我想在添加部分时使用 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--;
            }
        }
    }

}

【问题讨论】:

  • 为了获得更好的帮助,请尽快发布 SSCCE/MCVE,简短、可运行、可编译,大约在上午发布
  • 使用 null 布局将是我的首要考虑
  • 您实际上并没有制作新组件,您只是重新添加现有组件,或者您正在移动现有组件
  • @MadProgrammer 在不删除现有/原始组件并添加新组件的情况下,我该如何做呢?我必须使用硬编码 xy 位置。
  • 创建要添加的组件的新实例

标签: java swing user-interface graphics


【解决方案1】:

所以我运行了你的代码,我稍微改变了代码结构,并对重点发表了评论:

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

        }
    }

}

【讨论】:

  • 我确实想在现有/原始组件之上添加其他组件。在不删除现有/原始组件并添加新组件的情况下,我该怎么做?
  • 您必须创建新对象。如果您想在每次单击按钮时添加无限数量的元素,我建议您在类中创建一个元素列表。我将使用此解决方案编辑我的答案
  • 我在我的 iPhone 上编辑了这个问题,所以我不确定它是否可以编译,但想法就在这里。 :)
  • 谢谢,但我收到 JComboBox jtf1 = new JComboBox(tools); 行的错误。有什么问题?错误是:javax.swing.DefaultComboBoxModel.(Unknown Source) at javax.swing.JComboBox.(Unknown Source)的线程“AWT-EventQueue-0”java.lang.NullPointerException 中的异常>
  • 可以的话请帮忙
猜你喜欢
  • 2016-06-16
  • 2015-05-22
  • 2022-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-01
  • 1970-01-01
  • 2015-03-25
相关资源
最近更新 更多