简体   繁体   English

按钮生成其他按钮

[英]Buttons Generating other Buttons

So, i'm asking cause i searched and didn't found nothing about this, i don't know if i'm only searching it wrong.所以,我问的是因为我搜索过但没有发现任何关于此的内容,我不知道我是否只是搜索错误。

I'm building a POS (Point of Sale) for my final School work but instead off adding the buttons manually i wanted to make an interface for the admn where he could add the buttons to the main project (ex. I want to add the button for Meat, Fish, etc.) It's much likely to be easy to do it, my other doubt becomes with, if the button is generated how it will be called so i can use it later on?我正在为我最后的学校工作构建一个 POS(销售点),但我想为管理员创建一个界面,以便他可以将按钮添加到主项目中(例如,我想添加按钮)肉、鱼等的按钮)很可能很容易做到,我的另一个疑问变成了,如果按钮是生成的,它将如何调用,以便我以后可以使用它?

I think you shouldn't generate new buttons.我认为您不应该生成新按钮。 The best way is to hide the buttons you've created by calling button.setVisibility(View.Gone).最好的方法是通过调用 button.setVisibility(View.Gone) 隐藏您创建的按钮。 So just create buttons and call setVisibility(View.Gone) in onCreate.所以只需创建按钮并在onCreate 中调用setVisibility(View.Gone)。 And where needed make them visible by calling button.setVisibility(View.visible).并在需要时通过调用 button.setVisibility(View.visible) 使它们可见。

With the NetBeans form designer you can see what code must be created.使用 NetBeans 表单设计器,您可以查看必须创建哪些代码。

Then instead of jButton1, jButton2 use List<JButton> buttons = new ArrayList<>();然后代替 jButton1,jButton2 使用List<JButton> buttons = new ArrayList<>();

In the initComponents (or after its call) create the buttons dynamically, using some list with button data: caption Meat / Fish / ... and so on.initComponents (或在其调用之后)动态创建按钮,使用一些带有按钮数据的列表:caption Meat / Fish / ... 等等。 These data could come from a file you generated, so they are persist even if quitting the application.这些数据可能来自您生成的文件,因此即使退出应用程序,它们也会持久存在。

A file can be read as:一个文件可以读为:

Path path = Paths.get("buttons.txt");
List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
for (String line : lines) {
    String[] words = line.split(";\\s*");
    if (words.length > 2 && words[0].equals("button")) {
        JButton button = new JButton(word[1]);
        button.addActionListener(this); ...
        ... add(button);
        buttons.add(button);
    }
}

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

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