2

我为一个项目工作,我想创建一个评级系统。我希望当我按下第 5 颗星时,所有的星星都变成红色,当我按下第 4 颗星时;第一次开始,第二颗星和第三颗星变成红色,依此类推。如果你能给我一些建议,我会很高兴。谢谢你!

这是我的代码:

public class Rating {

private ArrayList<String> results = new ArrayList<String>();

private JLabel inputLabel1;

public static void main(String[] args) {

    new Rating();
}

public Rating() {
    JFrame f2 = new JFrame("Rating");
    inputLabel1 = new JLabel("Rate: ");

    Icon icon1 = new ImageIcon("star-icon.png");
    Icon icon2 = new ImageIcon("star-3-icon.png");

    JButton button1 = new JButton(icon1);
    button1.setPreferredSize(new Dimension(35, 35));
    button1.setBorder(BorderFactory.createEmptyBorder());
    button1.setContentAreaFilled(false);
    button1.setDisabledIcon(icon1);
    button1.setPressedIcon(icon2);
    button1.setSelectedIcon(icon2);
    button1.setRolloverEnabled(true);
    button1.setRolloverIcon(icon2);
    button1.setRolloverSelectedIcon(icon2);

    JButton button2 = new JButton(icon1);
    button2.setPreferredSize(new Dimension(35, 35));
    button2.setBorder(BorderFactory.createEmptyBorder());
    button2.setContentAreaFilled(false);
    button2.setDisabledIcon(icon1);
    button2.setPressedIcon(icon2);
    button2.setSelectedIcon(icon2);
    button2.setRolloverEnabled(true);
    button2.setRolloverIcon(icon2);
    button2.setRolloverSelectedIcon(icon2);

    JButton button3 = new JButton(icon1);
    button3.setBorder(BorderFactory.createEmptyBorder());
    button3.setContentAreaFilled(false);
    button3.setDisabledIcon(icon1);
    button3.setPressedIcon(icon2);
    button3.setSelectedIcon(icon2);
    button3.setPreferredSize(new Dimension(35, 35));
    button3.setRolloverEnabled(true);
    button3.setRolloverIcon(icon2);
    button3.setRolloverSelectedIcon(icon2);

    JButton button4 = new JButton(icon1);
    button4.setPreferredSize(new Dimension(35, 35));
    button4.setBorder(BorderFactory.createEmptyBorder());
    button4.setContentAreaFilled(false);
    button4.setDisabledIcon(icon1);
    button4.setPressedIcon(icon2);
    button4.setSelectedIcon(icon2);
    button4.setRolloverEnabled(true);
    button4.setRolloverIcon(icon2);
    button4.setRolloverSelectedIcon(icon2);

    JButton button5 = new JButton(icon1);
    button5.setPreferredSize(new Dimension(35, 35));
    button5.setBorder(BorderFactory.createEmptyBorder());
    button5.setContentAreaFilled(false);
    button5.setDisabledIcon(icon1);
    button5.setPressedIcon(icon2);
    button5.setSelectedIcon(icon2);
    button5.setRolloverEnabled(true);
    button5.setRolloverIcon(icon2);
    button5.setRolloverSelectedIcon(icon2);

    JButton button6 = new JButton("Subscribe");
    button6.setPreferredSize(new Dimension(95, 35));

    final JTextArea display = new JTextArea(5, 25);

    JButton button7 = new JButton("Clear");
    button7.setPreferredSize(new Dimension(95, 35));

    JPanel panel2 = new JPanel();
    f2.add(panel2);
    f2.setVisible(true);
    f2.setSize(500, 550);
    f2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    panel2.add(inputLabel1);
    panel2.add(button1);
    panel2.add(button2);
    panel2.add(button3);
    panel2.add(button4);
    panel2.add(button5);
    panel2.add(button6);
    panel2.add(button7);
    panel2.add(display);

    button1.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {

            String b1 = "1";
            results.add(b1);
            StringBuilder sb = new StringBuilder();
            for (String string : results) {

                sb.append(string);
                sb.append("\n");
            }

        }
    });
    button2.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            String b2 = "2";
            results.add(b2);
            StringBuilder sb = new StringBuilder();
            for (String string : results) {

                sb.append(string);
                sb.append("\n");
            }

        }
    });
    button3.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            String b3 = "3";
            results.add(b3);
            StringBuilder sb = new StringBuilder();
            for (String string : results) {

                sb.append(string);
                sb.append("\n");
            }

        }
    });
    button4.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {

            String b4 = "4";
            results.add(b4);
            StringBuilder sb = new StringBuilder();
            for (String string : results) {

                sb.append(string);
                sb.append("\n");
            }

        }
    });
    button5.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            String b5 = "5";
            results.add(b5);
            StringBuilder sb = new StringBuilder();
            for (String string : results) {

                sb.append(string);
                sb.append("\n");
            }

        }
    });

    button6.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            String s = display.getText().toString();
            results.add(s);
            StringBuilder sb = new StringBuilder();
            for (String string : results) {

                sb.append(string);
                sb.append("\n");
            }
            display.setText(sb.toString());
            results.clear();

        }
    });

    button7.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {

            results.clear();
            StringBuilder sb = new StringBuilder();
            for (String string : results) {
                sb.append(string);
            }

            display.setText(null);

        }
    });
}
}
4

3 回答 3

2

对于具有默认值的 aList<JToggleButton>和 a ,添加 的五个实例。为星号使用合适的 Unicode 字形,例如★ \u2605 ,如此所示。在您常见的情况下,将红色或背面的每个元素作为接收到的类型中的指示。JPanelFlowLayoutJToggleButtonItemListenersetForground()ListItemEventgetStateChange()

于 2012-12-12T22:40:11.977 回答
0

对于所有 5 个按钮,您的操作非常相似。

您可以通过将这些操作放入方法中来使代码整洁

public void buttonClicked(Button but)

然后将您的按钮传递给此方法:

buttonClicked(button1);
于 2012-12-12T21:50:38.907 回答
-2

如果您想为可能用于移动设备的用户反馈系统开发该评级系统,请注意拥有专利的 Lodsys 公司。请参阅因反馈系统而被起诉的愤怒的小鸟公司的信息。请参阅http://www.geek.com/articles/mobile/lodsys-targets-angry-birds-maker-rovio-for-infringing-in-app-purchase-patent-20110725/ 请注意 Apple 从他们那里购买了许可证,但独立律师表示,该许可不包括苹果以外开发的应用程序。

于 2012-12-12T20:57:01.783 回答