关于validate()、revalidate()和repaint()的示例,看起来对于正确输出到GUI是必需的,由一些LayoutManagers进行布局。
编辑:正如trashgod
所注意到的那样,我添加了为EDT安排作业。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ValidateRevalidateRepaint {
private JPanel panel;
private GridBagConstraints gbc;
private boolean validate, revalidate, repaint;
public ValidateRevalidateRepaint() {
validate = revalidate = repaint = false;
panel = new JPanel(new GridBagLayout());
gbc = new GridBagConstraints();
gbc.insets = new Insets(0, 20, 0, 20);
panel.add(getFiller(), gbc);
JFrame f = new JFrame();
f.setJMenuBar(getMenuBar());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(panel);
f.getContentPane().add(getRadioPanel(), "East");
f.getContentPane().add(getCheckBoxPanel(), "South");
f.setSize(400, 200);
f.setLocation(200, 200);
f.setVisible(true);
}
private JMenuBar getMenuBar() {
JMenu menu = new JMenu("change");
ActionListener l = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JMenuItem item = (JMenuItem) e.getSource();
int n = Integer.parseInt(item.getActionCommand());
makeChange(n);
}
};
for (int j = 1; j < 5; j++) {
String s = String.valueOf(j) + " component";
if (j > 1) {
s += "s";
}
JMenuItem item = new JMenuItem(s);
item.setActionCommand(String.valueOf(j));
item.addActionListener(l);
menu.add(item);
}
JMenuBar menuBar = new JMenuBar();
menuBar.add(menu);
return menuBar;
}
private JPanel getRadioPanel() {
JPanel panel1 = new JPanel(new GridBagLayout());
GridBagConstraints gbc1 = new GridBagConstraints();
gbc1.insets = new Insets(2, 2, 2, 2);
gbc1.weighty = 1.0;
gbc1.gridwidth = GridBagConstraints.REMAINDER;
ButtonGroup group = new ButtonGroup();
ActionListener l = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JRadioButton radio = (JRadioButton) e.getSource();
int n = Integer.parseInt(radio.getActionCommand());
makeChange(n);
}
};
for (int j = 0; j < 4; j++) {
String s = String.valueOf(j + 1);
JRadioButton radio = new JRadioButton(s);
radio.setActionCommand(s);
radio.addActionListener(l);
group.add(radio);
panel1.add(radio, gbc1);
}
return panel1;
}
private JPanel getCheckBoxPanel() {
final String[] operations = {"validate", "revalidate", "repaint"};
ActionListener l = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JCheckBox checkBox = (JCheckBox) e.getSource();
String ac = checkBox.getActionCommand();
boolean state = checkBox.isSelected();
if (ac.equals("validate")) {
validate = state;
}
if (ac.equals("revalidate")) {
revalidate = state;
}
if (ac.equals("repaint")) {
repaint = state;
}
}
};
JPanel panel2 = new JPanel();
for (int j = 0; j < operations.length; j++) {
JCheckBox check = new JCheckBox(operations[j]);
check.setActionCommand(operations[j]);
check.addActionListener(l);
panel2.add(check);
}
return panel2;
}
private void makeChange(int number) {
panel.removeAll();
for (int j = 0; j < number; j++) {
panel.add(getFiller(), gbc);
}
if (validate) {
panel.validate();
}
if (revalidate) {
panel.revalidate();
}
if (repaint) {
panel.repaint();
}
}
private JPanel getFiller() {
JPanel panel3 = new JPanel();
panel3.setBackground(Color.red);
panel3.setPreferredSize(new Dimension(40, 40));
return panel3;
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
ValidateRevalidateRepaint rVR = new ValidateRevalidateRepaint();
}
});
}
}