简体   繁体   English

JFrame-ActionListener

[英]JFrame - ActionListener

I'm working on building a program that uses JFrame. 我正在构建一个使用JFrame的程序。 What I want for my end result, is to implement an ActionListener which will remove labels when the user clicks a button. 我想要的最终结果是实现一个ActionListener,当用户单击一个按钮时它将删除标签。 For example: when the user clicks the JButton, one of 5 labels is removed from the frame. 例如:当用户单击JButton时,将从框架中删除5个标签之一。 When they click the button again, one of the remaining 4 labels is removed...and so on a so forth, until 0 labels remain. 当他们再次单击该按钮时,将删除剩余的4个标签之一...依此类推,直到剩下0个标签。 Technically, I have the program working as required however, I'm trying to see if there is a way to implement the ActionListener event via a loop as opposed to listing an if statement for each individual label. 从技术上讲,我让程序按要求工作,但是,我试图查看是否有一种通过循环实现ActionListener事件的方法,而不是为每个单独的标签列出if语句。 Thank you so much! 非常感谢!

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

//calls for public class to inherit features of JFrame within Java
public class NoPurchaseReason extends JFrame implements ActionListener {

private int removeText = 0;

JButton btn = new JButton("Select");

JLabel lbl = new JLabel("Found better price");
JLabel lbl1 = new JLabel("Not as shown on website");
JLabel lbl2 = new JLabel("Wrong product");
JLabel lbl3 = new JLabel("Damaged upon delivery");
JLabel lbl4 = new JLabel("None of the above");

public static void main(String[] args) {
    JFrame f = new NoPurchaseReason("Please tell us why you wish to return your purchase.");
    f.setBounds(300, 100, 500, 500);
    f.setVisible(true);
    f.setBackground(Color.blue);
}

public NoPurchaseReason(String title) {
    super(title);
    setLayout(null);
    lbl.setBounds(40, 40, 600, 40);
    btn.setBounds(320, 10, 80, 20);
    lbl.setBounds(100, 40, 100, 20);
    lbl1.setBounds(100, 70, 100, 20);
    lbl2.setBounds(100, 100, 150, 20);
    lbl3.setBounds(100, 130, 100, 20);
    lbl4.setBounds(100, 160, 100, 20);

    add(btn);
    add(lbl);
    add(lbl);
    add(lbl1);
    add(lbl2);
    add(lbl3);
    add(lbl4);
    btn.addActionListener(this);
}

public void actionPerformed(ActionEvent e) {
    removeText++;
    if (removeText == 1) {
        lbl.setVisible(false);
        lbl1.setBounds(100, 40, 100, 20);
        lbl2.setBounds(100, 70, 100, 20);
        lbl3.setBounds(100, 100, 150, 20);
        lbl4.setBounds(100, 130, 100, 20);
    }
    if (removeText == 2) {
        lbl1.setVisible(false);
        lbl2.setBounds(100, 40, 100, 20);
        lbl3.setBounds(100, 70, 150, 20);
        lbl4.setBounds(100, 100, 100, 20);
    }
    if (removeText == 3) {
        lbl2.setVisible(false);
        lbl3.setBounds(100, 40, 150, 20);
        lbl4.setBounds(100, 70, 100, 20);
    }
    if (removeText == 4) {
        lbl3.setVisible(false);
        lbl4.setBounds(100, 40, 100, 20);
    }
    if (removeText == 5) {
        lbl4.setVisible(false);
    }
}

} }

Learning how to properly use layout managers will save you a lot of trouble in the long run. 从长远来看,学习如何正确使用布局管理器将为您节省很多麻烦。

You'll also find that people will tell you to adhere to the single responsibility principle , and avoid making classes that violate this principle (eg, extending JFrame and implementing ActionListener). 您还将发现人们会告诉您遵守单一责任原则 ,并避免创建违反该原则的类(例如,扩展JFrame和实现ActionListener)。

You'll also hear folks tell you to prefer using actions over action listeners (if you need to share functionality across multiple components, that is). 您还将听到人们告诉您,与使用动作侦听器相比,您更喜欢使用动作(也就是说,如果您需要在多个组件之间共享功能)。

A simple way would be to dedicate an entire panel to holding your labels, and simply remove the first label in the panel until there are no more labels. 一种简单的方法是将整个面板专用于存放标签,然后简单地删除面板中的第一个标签,直到没有更多标签为止。 Here's an example: 这是一个例子:

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

class LabelDemo
{
    public static void main(String[] args) {
        String[] labels = {
                "Found better price",
                "Not as shown on website",
                "Wrong product",
                "Damaged upon delivery",
                "None of the above"
        };
        final JFrame frame = new JFrame();
        final JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        for (String s: labels) {
            panel.add(new JLabel(s));
        }
        frame.add(panel);
        JButton button = new JButton("Select");
        button.addActionListener(new ActionListener() {
            @Override public void actionPerformed(ActionEvent e) {
                if (panel.getComponentCount() > 0)
                    panel.remove(0);
                frame.repaint();
            }
        });
        frame.add(button, BorderLayout.NORTH);
        frame.pack();
        frame.setVisible(true);
    }
}

Also, you may just have a certain goal in mind that I'm not aware of, but it honestly seems like a list would be better in this case. 另外,您可能只是想起一个我不知道的特定目标,但是说实话,在这种情况下,列表会更好。 Here's an example of that as well: 这也是一个例子:

String[] labels = {
        "Found better price",
        "Not as shown on website",
        "Wrong product",
        "Damaged upon delivery",
        "None of the above"
};
JList<String> list = new JList<>(labels);
int option = JOptionPane.showConfirmDialog(null, list, 
        "Please tell us why you wish to return your purchase.", 
        JOptionPane.OK_CANCEL_OPTION);
if (option == JOptionPane.OK_OPTION) {
    String selectedValue = list.getSelectedValue();
    System.out.println(selectedValue); // Do something with it.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM