将点转换为按钮

时间:2016-10-03 02:13:31

标签: java user-interface jbutton point

是否可以在java中从点转换为JButton,就像从int转换为string一样..任何帮助都是可以接受的,我需要帮助。谢谢!

    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;

    import javax.swing.*;

    import java.awt.*;


    @SuppressWarnings("serial")
    public class Beginner extends JPanel {

        static JButton quest;
        Random rand = new Random();

        int n = 10;

        static List <Point> points = new ArrayList<Point> ();


        public Beginner() {

                  int radius = 200;
                  Point center = new Point (250, 250);
                  double angle = Math.toRadians(360 / n);
                  points.add(center);

                  for (int i = 0; i < n; i++) {
                      double theta = i * angle;
                      int dx = (int) (radius * Math.sin(theta));
                      int dy = (int) (radius * Math.cos(theta));
                      Point p = new Point (center.x + dx , center.y + dy);
                      points.add(p);

                  }
                draw (points); 
                  }

                   public void draw (List<Point> points) {
                       JPanel panels = new JPanel();
                       SpringLayout spring = new SpringLayout();

                       int count = 1;
                       for (Point point: points) {

                           quest = new JButton("Question " + count);
                           quest.setForeground(Color.BLACK);
                            Font fonte = new Font("Script MT Bold", Font.PLAIN, 20);
                            quest.setFont(fonte);

                           add (quest);
                           count++;

                           spring.putConstraint(SpringLayout.WEST, quest, point.x, SpringLayout.WEST, panels );

                           spring.putConstraint(SpringLayout.NORTH, quest, point.y, SpringLayout.NORTH, panels );

                           setLayout(spring);

                           panels.setOpaque(false);
                           panels.setVisible(true);
                           panels.setLocation(5,5);

                           add(panels); 


                          quest.addActionListener(new ActionListener(){
                         public void actionPerformed(ActionEvent p) {

                             JButton source = (JButton) p.getSource();
                             //where the problem lies
                             if (source.equals(points.get(0))) {

                                //some action....

                             }

                         }
                     });

                      }
                       }
          public static void main(String [] args){
                 JFrame frame = new JFrame();
                 //set JFrame properties
                 JButton start = new JButton ("CLick here to start");

          start.addActionListener(new ActionListener(){
                  public void actionPerformed (ActionEvent et) {
                  Beginner novice = new Beginner();
                  //set Beginner properties (remember it extends JPanel);
           }
              });

                 } }

1 个答案:

答案 0 :(得分:1)

Probbaly你需要在代码中添加缺少的行,就像这样,我只是测试了它并且它可以工作。

  • Point包中导入awt课程。
  • 使用main方法通过创建类的新实例来执行代码。
  • 此外,您需要扩展JFrame课程,以便能够调用setLayoutadd方法。

    package javaapplication6;
    
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Point;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import javax.swing.SpringLayout;
    import javax.swing.JFrame;
    
    public class Beginner extends JFrame {
    
        Random rand = new Random();
    
        int n = 10;
    
        static List<Point> points = new ArrayList<Point>();
    
        public Beginner() {
    
            int radius = 200;
            Point center = new Point(250, 250);
            double angle = Math.toRadians(360 / n);
            points.add(center);
    
            for (int i = 0; i < n; i++) {
                double theta = i * angle;
                int dx = (int) (radius * Math.sin(theta));
                int dy = (int) (radius * Math.cos(theta));
                Point p = new Point(center.x + dx, center.y + dy);
                points.add(p);
    
            }
            draw(points);
        }
    
        public void draw(List<Point> points) {
            JPanel panels = new JPanel();
            SpringLayout spring = new SpringLayout();
    
            int count = 1;
            for (Point point : points) {
    
                JButton quest = new JButton("Question " + count);
    
                quest.setForeground(Color.BLACK);
                Font fonte = new Font("Script MT Bold", Font.PLAIN, 20);
                quest.setFont(fonte);
                quest.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent et) {
                        System.out.println(quest.getText());
                        //do something else
                    }
    
                });
                add(quest);
    
                spring.putConstraint(SpringLayout.WEST, quest, point.x, SpringLayout.WEST, panels);
    
                spring.putConstraint(SpringLayout.NORTH, quest, point.y, SpringLayout.NORTH, panels);
    
                setLayout(spring);
    
                panels.setOpaque(false);
                panels.setVisible(true);
                panels.setLocation(5, 5);
    
                add(panels);
    
                count++;
            }
            setSize(700, 700);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true);
        }
    
        public static void main(String[] args) {
            new Beginner();
        }
    }