【问题标题】:Alter JComponent from a Listener/Event从侦听器/事件中更改 JComponent
【发布时间】:2025-12-07 12:45:01
【问题描述】:

我正在构建一个 Swing 程序,我希望能够使用一个按钮来更改 JComponentsJLabelJButton)的某些功能(字体、前景色、背景色等)。

如果所有组件都已显式声明和定义,我可以毫无问题地做到这一点,但如果它们是使用泛型方法隐式构建的,我无法弄清楚如何做到这一点。

以下是我目前所拥有的要点,减去一些不重要的细节。单击styleButton1 和 2 时,我想刷新或重建 JFrame,以便通过更改 @987654326 为组件(testButton1 和 2)使用功能/样式的新值(在本例中为字体) @ 然后重新绘制。

我没有收到任何错误,但frame 和组件没有被重建/刷新,即,单击样式按钮时没有任何反应。

关于如何使这项工作发挥作用的任何想法?或者我可以使用任何其他方法来获得所需的效果?

//imports

public class GuiTesting extends JFrame {

    public static void main(String[] args) {
        frame = new GuiTesting();
        frame.setVisible(true);
    }

    static JFrame frame;
    static Font standardFont = new Font("Arial", Font.BOLD, 10);
    static Font secondFont = new Font("Times New Roman", Font.PLAIN, 10);
    static Font currentFont = standardFont;

    public GuiTesting() {
        setTitle("GUI Testing");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 400);
        JPanel mainPanel = new JPanel();
        getContentPane().add(mainPanel);
        mainPanel.add(basicButton("Button1"));
        mainPanel.add(basicButton("Button2"));
        mainPanel.add(style1Button("Style 1"));
        mainPanel.add(style2Button("Style 2"));
    }

    public static JButton basicButton(String title) {
        JButton button = new JButton(title);
        button.setPreferredSize(new Dimension(80, 30));
        button.setFont(currentFont);
        return button;
    }

    public static JButton style1Button(String title) {
        JButton button = new JButton(title);
        button.setPreferredSize(new Dimension(80, 30));
        button.setFont(standardFont);
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                currentFont = standardFont;
                frame.repaint();

            }
        });
        return button;
    }

    public static JButton style2Button(String title) {
        JButton button = new JButton(title);
        button.setPreferredSize(new Dimension(80, 30));
        button.setFont(secondFont);
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                currentFont = secondFont;
                frame.repaint();
            }
        });
        return button;
    }

}

【问题讨论】:

    标签: java swing events listener jcomponent


    【解决方案1】:

    在您的 JComponent 类中创建一个 styleOne 方法,用于设置您需要的所有值。它将可以访问您班级的所有领域。在动作侦听器内部调用此方法。

    另外,不要像那样静态地创建按钮。直接在构造函数中创建它们。如果要覆盖按钮的外观,请在 init 方法或构造函数中进行。或者,更好的是,子类 JButton。

    【讨论】:

      【解决方案2】:

      您可以将需要刷新样式的组件存储在列表中:

      private static List<JComponent> components = new ArrayList<JComponent>();
      

      然后在您的basicButton() 方法中添加新组件以刷新组件:components.add(button);

      然后在ActionListener 中你可以执行下一行来刷新样式:

      for(JComponent c : components){
           c.setFont(currentFont);
      }
      

      或者您可以直接将组件传递给ActionListener,如下所示:

      JButton b1;
      JButton b2;
      mainPanel.add(b1 = basicButton("Button1"));
      mainPanel.add(b2 = basicButton("Button2"));
      mainPanel.add(style1Button("Style 1",b1,b2));
      

      style1Button()代码:

      public static JButton style1Button(String title,final JComponent... components) {
          JButton button = new JButton(title);
          button.setPreferredSize(new Dimension(80, 30));
          button.setFont(standardFont);
          button.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  currentFont = standardFont;
                  for(JComponent c : components){
                      c.setFont(currentFont);
                  }
                  frame.repaint();
              }
          });
          return button;
      }
      

      【讨论】:

      • 我看到你在这里做了什么,我认为这将适用于我正在尝试做的事情。非常感谢!