自分でタイトルを広げてRadio付きのTitledBorder

4826 ワード




1. TitledBorder  , :

public class ComponentTitledBorder implements Border, MouseListener,
		SwingConstants {
	int offset = 5;

	Component comp;
	JComponent container;
	Rectangle rect;
	Border border;

	public ComponentTitledBorder(Component comp, JComponent container,
			Border border) {
		this.comp = comp;
		this.container = container;
		this.border = border;
		container.addMouseListener(this);
	}

	public boolean isBorderOpaque() {
		return true;
	}

	public void paintBorder(Component c, Graphics g, int x, int y, int width,
			int height) {
		Insets borderInsets = border.getBorderInsets(c);
		Insets insets = getBorderInsets(c);
		int temp = (insets.top - borderInsets.top) / 2;
		border.paintBorder(c, g, x, y + temp, width, height - temp);
		Dimension size = comp.getPreferredSize();
		rect = new Rectangle(offset, 0, size.width, size.height);
		SwingUtilities.paintComponent(g, comp, (Container) c, rect);
	}

	public Insets getBorderInsets(Component c) {
		Dimension size = comp.getPreferredSize();
		Insets insets = border.getBorderInsets(c);
		insets.top = Math.max(insets.top, size.height);
		return insets;
	}

	private void dispatchEvent(MouseEvent me) {
		if (rect != null && rect.contains(me.getX(), me.getY())) {
			Point pt = me.getPoint();
			pt.translate(-offset, 0);
			comp.setBounds(rect);
			comp.dispatchEvent(new MouseEvent(comp, me.getID(), me.getWhen(),
					me.getModifiers(), pt.x, pt.y, me.getClickCount(), me
							.isPopupTrigger(), me.getButton()));
			if (!comp.isValid())
				container.repaint();
		}
	}

	public void mouseClicked(MouseEvent me) {
		dispatchEvent(me);
	}

	public void mouseEntered(MouseEvent me) {
		dispatchEvent(me);
	}

	public void mouseExited(MouseEvent me) {
		dispatchEvent(me);
	}

	public void mousePressed(MouseEvent me) {
		dispatchEvent(me);
	}

	public void mouseReleased(MouseEvent me) {
		dispatchEvent(me);
	}
}



2. , 

public class MyBorder  {

	public static void main(String[] args) {
	    try{ 
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
        } catch(Exception e){ 
            e.printStackTrace(); 
        } 
        final JPanel proxyPanel = new JPanel(); 
        proxyPanel.add(new JLabel("google: ")); 
        proxyPanel.add(new JTextField("www.google.com.hk")); 
        proxyPanel.add(new JLabel("google Port")); 
        proxyPanel.add(new JTextField("8080")); 
        final JCheckBox checkBox = new JCheckBox(" Checkbox TitledBorder", true); 
        final JRadioButton radio = new JRadioButton(" Radio TitledBorder", true); 
        checkBox.setFocusPainted(false); 
        ComponentTitledBorder componentBorder = 
                new ComponentTitledBorder(radio, proxyPanel 
                , BorderFactory.createEtchedBorder()); 
        checkBox.addActionListener(new ActionListener(){ 
            public void actionPerformed(ActionEvent e){ 
                boolean enable = checkBox.isSelected(); 
                Component comp[] = proxyPanel.getComponents(); 
                for(int i = 0; i<comp.length; i++){ 
                    comp[i].setEnabled(enable); 
                } 
            } 
        }); 
        
        radio.addActionListener(new ActionListener(){ 
            public void actionPerformed(ActionEvent e){ 
                boolean enable = radio.isSelected(); 
                Component comp[] = proxyPanel.getComponents(); 
                for(int i = 0; i<comp.length; i++){ 
                    comp[i].setEnabled(enable); 
                } 
            } 
        }); 
        proxyPanel.setBorder(componentBorder); 
        JFrame frame = new JFrame("ComponentTitledBorder - [email protected]"); 
        Container contents = frame.getContentPane(); 
        contents.setLayout(new FlowLayout()); 
        contents.add(proxyPanel); 
        frame.pack(); 
        frame.setBounds(200, 200, 400, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        frame.setVisible(true); 
	}

}