今天突然想到这样一个问题:用SWING来实现类似EXCEL中条件格式效果的实现请见下图:
点击“添加”添加一个条件,点击“删除”把选中的一个条件删除。再加一个按钮,点击“隐藏”把选中的条件隐藏。
个人感觉实现有一定的难度,希望大家来发表一下你们的建议。
点击“添加”添加一个条件,点击“删除”把选中的一个条件删除。再加一个按钮,点击“隐藏”把选中的条件隐藏。
个人感觉实现有一定的难度,希望大家来发表一下你们的建议。
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.*;
public class T {
public static void main(String[] args) {
final JPanel cp = new JPanel(new GridLayout(0, 1, 0, 5));
JPanel bp = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 5));
JButton addBtn = new JButton("Add");
JButton removeBtn = new JButton("Remove");
addBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cp.add(new ConditionPanel(cp.getComponentCount() + 1));
SwingUtilities.getWindowAncestor(cp).pack();
}
});
removeBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (cp.getComponentCount() > 0) {
cp.remove(cp.getComponentCount()-1);
SwingUtilities.getWindowAncestor(cp).pack();
}
}
});
bp.add(addBtn);
bp.add(removeBtn);
JFrame f = new JFrame();
f.getContentPane().add(cp, BorderLayout.CENTER);
f.getContentPane().add(bp, BorderLayout.SOUTH);
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}class ConditionPanel extends JPanel
{
public ConditionPanel(int id) {
super(new BorderLayout());
setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(), "条件" + id));
JLabel l = new JLabel("test");
l.setBorder(BorderFactory.createEmptyBorder(20, 200, 20, 200));
add(l, BorderLayout.CENTER);
}
}
佩服。
http://blog.csdn.net/bovy/archive/2007/04/06/1554644.aspx