从侦听器/事件中更改JComponent

时间:2013-12-18 21:00:31

标签: java swing events listener jcomponent

我正在构建一个Swing程序,我希望能够使用一个按钮来更改JComponentsJLabel,{JButton的某些功能(字体,ForeGround颜色,背景颜色等)。 {1}})。

如果组件都已经显式声明和定义,我可以毫无问题地执行此操作,但如果使用泛型方法隐式构建它们,我无法弄清楚如何执行此操作。

以下是我目前所拥有的 gist ,减去了一些不重要的细节。单击styleButton1和2时,我想刷新或重建JFrame,以便将要素/样式的新值(在此示例中为Font)用于组件(testButton1和2),通过更改currentFont然后重新绘制。

我没有遇到任何错误,但是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;
    }

}

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

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

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;
}
相关问题