0

这是我在stackoverflow中的第一个问题。我在问如何在 JButton 中添加 JPasswordField,UAC 对话框就是一个例子。


(来源:microsoft.com

4

3 回答 3

3

这不是“JButton 中的 JPasswordField”——这只是“焦点指示”。

您应该将此表单(图标、用户名文本字段、密码文本字段等)添加到例如JPanel. 在这个 JPanel 中覆盖paintComponent 并绘制渐变和边框。

这就是你打算做的,我相信。

于 2012-05-31T22:33:49.290 回答
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();
    }
}
于 2012-05-31T22:58:23.957 回答
2

在此处输入图像描述

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);
  }
}
于 2012-06-01T08:20:05.427 回答