我想用Java在java中创建一个评级系统

时间:2012-12-12 20:49:34

标签: java swing icons jbutton

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

这是我的代码:

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

        }
    });
}
}

3 个答案:

答案 0 :(得分:2)

List<JToggleButton>JPanel拥有默认FlowLayout,请添加JToggleButton的五个实例。为星形使用合适的Unicode字形,例如★ \u2605,如here所示。在您的ItemListenersetForground()List红色或返回ItemEvent的每个元素,如getStateChange()中收到的{{1}}类型所示。

答案 1 :(得分:0)

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

您可以通过将这些操作放入方法

来使代码整洁
public void buttonClicked(Button but)

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

buttonClicked(button1);

答案 2 :(得分:-2)

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