简体   繁体   English

如何在JFrame中添加下拉菜单?

[英]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. 然后,类points包含一个paintComponent方法。 This method takes a variable, complexity , and draws a circle based on the number. 此方法采用一个变量, complexity并根据该数字绘制一个圆。 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. 我想做的是在我的JFrame中添加一个下拉菜单并确认按钮,该按钮可以更改此复杂度变量,然后绘制圆。 Any suggestions? 有什么建议么? I have no idea how I would pass a variable to the paintComponent method. 我不知道如何将变量传递给paintComponent方法。

I have no idea how I would pass a variable to the paintComponent method.. 我不知道如何将变量传递给paintComponent方法。

You don't. 你不知道 You set a property of the Points class. 您设置Points类的属性。 Then the paintComponent() method can reference the property during painting. 然后paintComponent()方法可以在绘画过程中引用该属性。

For example the Points class should have methods like setComplexity(...) and getComplexity() . 例如,分类应该有类似的方法setComplexity(...)getComplexity()

Also, follow Java naming conventions. 另外,请遵循Java命名约定。 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. 您阅读的任何教科书或turourial都将遵循此约定,因此请不要自己制定约定。

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

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