简体   繁体   English

一次更改一组 JButton 的属性

[英]Change properties of a group of JButtons at once

Hey I'm doing a calculator app with java swing (A clone of windows calculator;) ) As it is a calculator, it has a lot of JButtons with same properties.嘿,我正在用 java swing 做一个计算器应用程序(windows 计算器的克隆;)因为它是一个计算器,它有很多相同的属性。 So my question is can I change the common properties of a group JButtons at once, based on 'DRY'.所以我的问题是我可以根据“DRY”一次更改一组 JButtons 的通用属性。 If possible it will help me a lot...如果可能的话,它将对我有很大帮助...

A simple subclass of JButton should help, like this example - a modified version of what I saw from another question below:一个简单的JButton子类应该会有所帮助,就像这个例子 - 我从下面另一个问题中看到的修改版本:

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;

import javax.swing.JButton;
import javax.swing.JFrame;

@SuppressWarnings("serial")
public class MyButton extends JButton {

    private Color circleColor = Color.BLACK;

    public MyButton(String label) {
        super(label);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        Dimension originalSize = super.getPreferredSize();
        int gap = (int) (originalSize.height * 0.2);
        int x = originalSize.width + gap;
        int y = gap;
        int diameter = originalSize.height - (gap * 2);

        g.setColor(circleColor);
        g.fillOval(x, y, diameter, diameter);
    }

    @Override
    public Dimension getPreferredSize() {
        Dimension size = super.getPreferredSize();
        size.width += size.height;
        return size;
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        frame.getContentPane().setLayout(new FlowLayout());
        frame.getContentPane().add(new MyButton("Custom Button"));
        frame.setVisible(true);
    }

}

If you want more info, research: Creating a custom JButton in Java , which is the cited reference for the example above.如果您想了解更多信息,请研究: 在 Java 中创建自定义 JButton ,这是上面示例的引用参考。 Of course, you can code the properties to your liking with the assistance of the JButton Javadoc: https://docs.oracle.com/javase/7/docs/api/javax/swing/JButton.html .当然,您可以在 JButton Javadoc 的帮助下根据自己的喜好对属性进行编码: https://docs.oracle.com/javase/7/docs/api/javax/swing/JButton.ZFC35E988.28

But for modifying the custom button after instantiation: Make sure to add a static ArrayList that contains the instantiated buttons, as well as means to add a constructed button to the arraylist:但是要在实例化后修改自定义按钮:确保添加包含实例化按钮的 static ArrayList ,以及将构造按钮添加到 ZEB3A834246F50C9BDE2C825D7405C9188 的方法

public static ArrayList<MyButton> myButtons = new ArrayList<MyButton>();

public MyButton(){
//...
myButtons.add(this);
}

And for modification of the properties/attributes of all buttons (say for example, whether they are visible), do:并且要修改所有按钮的属性/属性(例如,它们是否可见),请执行以下操作:

public void setVisibleAll(boolean b){
for(MyButton x: myButtons){
x.setVisible(b);
}
}

Not only does this apply to buttons, it should apply to other swing components, like a JLayeredPane designed to act like a button:这不仅适用于按钮,还应适用于其他 swing 组件,例如设计为像按钮一样工作的 JLayeredPane:

package source_code.view.components.buttons;

import java.awt.Color;
import java.awt.Font;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.SwingConstants;

import source_code.controllers.ButtonListener350x40;
import source_code.plus_functions.Texture;

@SuppressWarnings("serial")
public class Button350x40 extends JLayeredPane {

    private JLabel buttonBg = new JLabel();
    private JLabel buttonText = new JLabel();
    private ButtonListener350x40 listener = new ButtonListener350x40();
    private static ArrayList<Button350x40> buttons = new ArrayList<Button350x40>();
    private String id;
    
    public Button350x40(String title, String id) {
        //Button Proper
        super();
        super.setSize(350, 40);
        super.setLayout(null);
        super.addMouseListener(listener);
            //Button BG
            buttonBg.setIcon(new ImageIcon(Texture.textures.get("button_350x40")));
            super.setLayer(buttonBg, 0);
            buttonBg.setBounds(0, 0, 350, 40);
            super.add(buttonBg);
            //Button Text
            buttonText.setText(title);
            buttonText.setForeground(Color.BLACK);
            super.setLayer(buttonText, 1);
            buttonText.setHorizontalAlignment(SwingConstants.CENTER);
            buttonText.setFont(new Font("Arial", Font.PLAIN, 20));
            buttonText.setBounds(0, 0, 350, 40);
            super.add(buttonText);
        this.id = id;
        buttons.add(this);
    }

    public String getId() {
        return id;
    }

    public static ArrayList<Button350x40> getButtons() {
        return buttons;
    }

    public JLabel getButtonBg() {
        return buttonBg;
    }

    public JLabel getButtonText() {
        return buttonText;
    }

    public ButtonListener350x40 getListener() {
        return listener;
    }

}

So TLDR , create a separate class for the custom button, have a static array of created buttons, and to modify all of the buttons, create a method that loops through the array of buttons and sets their respective attributes.所以TLDR ,为自定义按钮创建一个单独的 class ,创建一个 static 按钮数组,并修改所有按钮,创建一个循环遍历按钮数组并设置其各自属性的方法。

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

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