如何解决一次更改一组 JButton 的属性
嘿,我正在用 java swing 做一个计算器应用程序(Windows 计算器的克隆;)) 因为它是一个计算器,所以它有很多具有相同属性的 JButton。所以我的问题是我是否可以基于“DRY”一次更改一组 JButton 的通用属性。如果可能的话,它会对我有很大帮助...
解决方法
一个简单的 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);
}
}
如果您想了解更多信息,请研究: Creating a custom JButton in Java, 这是上述示例的引用参考。当然,您可以在 JButton Javadoc 的帮助下根据自己的喜好对属性进行编码:https://docs.oracle.com/javase/7/docs/api/javax/swing/JButton.html。
但是为了在实例化后修改自定义按钮:
确保添加包含实例化按钮的静态 ArrayList
,以及向数组列表添加构造按钮的方法:
public static ArrayList<MyButton> myButtons = new ArrayList<MyButton>();
public MyButton(){
//...
myButtons.add(this);
}
要修改所有按钮的属性/属性(例如,它们是否可见),请执行以下操作:
public void setVisibleAll(boolean b){
for(MyButton x: myButtons){
x.setVisible(b);
}
}
这不仅适用于按钮,还应适用于其他摆动组件,例如设计为具有按钮作用的 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,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,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;
}
}
所以TLDR,为自定义按钮创建一个单独的类,创建一个静态的按钮数组,并修改所有按钮,创建一个循环的方法通过按钮数组并设置它们各自的属性。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。