Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. class ButtonTest extends JFrame {
  2. class ClickablePanell extends JPanel {
  3. JTextField field;
  4. JPasswordField passwordField;
  5. public ClickablePanell() {
  6. setBorder(BorderFactory.createRaisedBevelBorder());
  7. field=new JTextField();
  8. field.setColumns(10);
  9. add(field);
  10. passwordField=new JPasswordField();
  11. passwordField.setColumns(10);
  12. add(passwordField);
  13. addMouseListener(new MouseClickAdapter());
  14. }
  15. class MouseClickAdapter extends MouseAdapter {
  16. public void mouseClicked(MouseEvent e) {
  17. System.out.println("field="+ClickablePanell.this.field.getText());
  18. System.out.println("passwordField="+new String(ClickablePanell.this.passwordField.getPassword()));
  19. }
  20. }
  21. }
  22.  
  23. public ButtonTest() {
  24. add(new JLabel("click in border"));
  25. add(new ClickablePanell());
  26. setExtendedState(JFrame.EXIT_ON_CLOSE);
  27. setSize(300, 200);
  28. setLayout(new FlowLayout());
  29. setVisible(true);
  30. }
  31.  
  32. public static void main(String[] args) {
  33. new ButtonTest();
  34. }
  35. }
  36.  
  37. import java.awt.*;
  38. import java.awt.event.*;
  39. import javax.swing.*;
  40. public class LayoutComponentsWithinButtonTest {
  41. public static JButton makeButton() {
  42. Icon icon = new Icon() {
  43. @Override public void paintIcon(Component c, Graphics g, int x, int y) {
  44. g.translate(x, y);
  45. g.setColor(Color.ORANGE);
  46. g.fillOval(4,4,55,55);
  47. g.translate(-x, -y);
  48. }
  49. @Override public int getIconWidth() {
  50. return 64;
  51. }
  52. @Override public int getIconHeight() {
  53. return 64;
  54. }
  55. };
  56. JButton button = new JButton();
  57. button.addActionListener(new ActionListener() {
  58. @Override public void actionPerformed(ActionEvent e) {
  59. Toolkit.getDefaultToolkit().beep();
  60. }
  61. });
  62. button.setLayout(new GridBagLayout());
  63. GridBagConstraints c = new GridBagConstraints();
  64.  
  65. c.gridwidth = 1;
  66. c.gridheight = 3;
  67. c.gridy = 0;
  68.  
  69. c.gridx = 0;
  70. c.weightx = 0.0;
  71. c.insets = new Insets(5, 5, 5, 5);
  72. c.anchor = GridBagConstraints.WEST;
  73. button.add(new JLabel(icon), c);
  74.  
  75. c.gridheight = 1;
  76. c.gridx = 1;
  77. c.weightx = 1.0;
  78. c.insets = new Insets(4, 2, 0, 2);
  79. button.add(new JTextField(16), c);
  80.  
  81. c.gridy = 1;
  82. button.add(new JPasswordField(16), c);
  83.  
  84. c.gridy = 2;
  85. c.insets = new Insets(0, 2, 0, 2);
  86. c.anchor = GridBagConstraints.NORTHWEST;
  87. button.add(new JLabel("Domain:Xxx"), c);
  88.  
  89. return button;
  90. }
  91. public static void main(String[] args) {
  92. EventQueue.invokeLater(new Runnable() {
  93. @Override public void run() {
  94. createAndShowGUI();
  95. }
  96. });
  97. }
  98. public static void createAndShowGUI() {
  99. JButton b = LayoutComponentsWithinButtonTest.makeButton();
  100. JPanel p = new JPanel(new BorderLayout());
  101. p.setBorder(BorderFactory.createEmptyBorder(32,32,32,32));
  102. p.add(b, BorderLayout.NORTH);
  103.  
  104. JFrame f = new JFrame();
  105. f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  106. f.getContentPane().add(p);
  107. f.getRootPane().setDefaultButton(b);
  108. f.setSize(480, 200);
  109. f.setLocationRelativeTo(null);
  110. f.setVisible(true);
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement