2012-05-31 56 views

回答

3

這不是「一個JButton內JPasswordField中」 - 這僅僅是一個「焦點指示」。

您應該將此表單(圖標,用戶名textfield,密碼textfield等)添加到例如JPanel。在此JPanel中覆蓋paintComponent並繪製gradient和一個邊框。

這就是你打算做的我相信。

2

您可以通過使用JPanel具有類似的效果。只需添加所有需要的組件並添加MouseListener。這裏是例子。

class ButtonTest extends JFrame { 
    class ClickablePanell extends JPanel { 
     JTextField field; 
     JPasswordField passwordField; 
     public ClickablePanell() { 
      setBorder(BorderFactory.createRaisedBevelBorder()); 
      field=new JTextField(); 
      field.setColumns(10); 
      add(field); 
      passwordField=new JPasswordField(); 
      passwordField.setColumns(10); 
      add(passwordField); 
      addMouseListener(new MouseClickAdapter()); 
     } 
     class MouseClickAdapter extends MouseAdapter { 
      public void mouseClicked(MouseEvent e) { 
       System.out.println("field="+ClickablePanell.this.field.getText()); 
       System.out.println("passwordField="+new String(ClickablePanell.this.passwordField.getPassword())); 
      } 
     } 
    } 

    public ButtonTest() { 
     add(new JLabel("click in border")); 
     add(new ClickablePanell()); 
     setExtendedState(JFrame.EXIT_ON_CLOSE); 
     setSize(300, 200); 
     setLayout(new FlowLayout()); 
     setVisible(true); 
    } 

    public static void main(String[] args) { 
     new ButtonTest(); 
    } 
} 
2

enter image description here

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
public class LayoutComponentsWithinButtonTest { 
    public static JButton makeButton() { 
    Icon icon = new Icon() { 
     @Override public void paintIcon(Component c, Graphics g, int x, int y) { 
     g.translate(x, y); 
     g.setColor(Color.ORANGE); 
     g.fillOval(4,4,55,55); 
     g.translate(-x, -y); 
     } 
     @Override public int getIconWidth() { 
     return 64; 
     } 
     @Override public int getIconHeight() { 
     return 64; 
     } 
    }; 
    JButton button = new JButton(); 
    button.addActionListener(new ActionListener() { 
     @Override public void actionPerformed(ActionEvent e) { 
     Toolkit.getDefaultToolkit().beep(); 
     } 
    }); 
    button.setLayout(new GridBagLayout()); 
    GridBagConstraints c = new GridBagConstraints(); 

    c.gridwidth = 1; 
    c.gridheight = 3; 
    c.gridy = 0; 

    c.gridx = 0; 
    c.weightx = 0.0; 
    c.insets = new Insets(5, 5, 5, 5); 
    c.anchor = GridBagConstraints.WEST; 
    button.add(new JLabel(icon), c); 

    c.gridheight = 1; 
    c.gridx = 1; 
    c.weightx = 1.0; 
    c.insets = new Insets(4, 2, 0, 2); 
    button.add(new JTextField(16), c); 

    c.gridy = 1; 
    button.add(new JPasswordField(16), c); 

    c.gridy = 2; 
    c.insets = new Insets(0, 2, 0, 2); 
    c.anchor = GridBagConstraints.NORTHWEST; 
    button.add(new JLabel("Domain:Xxx"), c); 

    return button; 
    } 
    public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     @Override public void run() { 
     createAndShowGUI(); 
     } 
    }); 
    } 
    public static void createAndShowGUI() { 
    JButton b = LayoutComponentsWithinButtonTest.makeButton(); 
    JPanel p = new JPanel(new BorderLayout()); 
    p.setBorder(BorderFactory.createEmptyBorder(32,32,32,32)); 
    p.add(b, BorderLayout.NORTH); 

    JFrame f = new JFrame(); 
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
    f.getContentPane().add(p); 
    f.getRootPane().setDefaultButton(b); 
    f.setSize(480, 200); 
    f.setLocationRelativeTo(null); 
    f.setVisible(true); 
    } 
}