Domanda

I'm building a Swing program and I want to be able to use a button to change certain features (Font, ForeGround Color, BackGround Color, etc.) of JComponents (JLabel, JButton).

I can do this without a problem if the components have all been explicitly declared and defined, but I cannot figure out how to do it if they are implicitly built using generic methods.

Below is the gist of what I have so far, minus some unimportant details. When styleButton1 and 2 are clicked, I want to refresh or rebuild the JFrame such that the new values for features/style (in this example, Font) are used for the components (testButton1 and 2), by changing currentFont and then repainting.

I'm not getting any errors with this, but frame and components are not being rebuilt/refreshed, i.e., nothing happens when the style buttons are clicked.

Any ideas on how to make this work? Or any other approaches I can use to get the desired effect?

//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;
    }

}
È stato utile?

Soluzione

You can store components, which need to refresh style in a list :

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

add then in your basicButton() method add new component to refreshing components:components.add(button);

And then in ActionListener you can execute next lines for refreshing style:

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

Or you can pass components directly to ActionListener like next :

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

and style1Button() code:

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;
}

Altri suggerimenti

Create a styleOne method in your JComponent class that sets all of the values you need. It will have access to all fields of your class. Inside the action listener call this method.

Also, don't create your buttons statically like that. Create them within the constructor directly. If you want to override the look of the buttons do it within an init method or constructor. Or, better yet, subclass JButton.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top