简体   繁体   中英

How to add a drop-down menu to a JFrame?

So, I have a main method that contains something like this:

public class ArcDrawer {
    public static void main ( String [ ] args ){
        JFrame f = new JFrame("Arc");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        points p = new points();
        f.add(p);
        f.setSize(850, 850);
        f.setVisible(true);
    }
}

Then, the class points contains a paintComponent method. This method takes a variable, complexity , and draws a circle based on the number. What I would like to do is add a drop-down menu and confirm button to my JFrame that can change this complexity variable and then draw the circle. Any suggestions? I have no idea how I would pass a variable to the paintComponent method.

I have no idea how I would pass a variable to the paintComponent method..

You don't. You set a property of the Points class. Then the paintComponent() method can reference the property during painting.

For example the Points class should have methods like setComplexity(...) and getComplexity() .

Also, follow Java naming conventions. Class names should start with an upper case character. "points" should be "Points". Any textbook or turorial you read will follow this convention so don't make up your own conventions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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