Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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();
- }
- }
- 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);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement