SHARE
TWEET

Untitled

a guest Oct 3rd, 2017 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. jframe.add(panel, BorderLayout.CENTER);
  2.      panel.setAlignmentX(JComponent.CENTER_ALIGNMENT);
  3.    
  4. public static void main(String[] args) {
  5.  
  6.         EventQueue.invokeLater(new Runnable() {
  7.             public void run() {
  8.                 try {
  9.                     login frame = new login();
  10.                     frame.setVisible(true);
  11.                 } catch (Exception e) {
  12.                     e.printStackTrace();
  13.                 }
  14.             }
  15.         });
  16.     }
  17.  
  18.     public login() {
  19.  
  20.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    
  21.         setVisible(true);
  22.         setBounds(5, 5, 409, 267);
  23.         setLocationRelativeTo(null);
  24.         contentPane = new JPanel();
  25.         contentPane.setBackground(new Color(0, 128, 128));
  26.         contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  27.         setContentPane(contentPane);
  28.             contentPane.setLayout(null);
  29.  
  30.         panel = new JPanel();
  31.         panel.setBounds(57, 42, 292, 167);
  32.         panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Login", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 255)));
  33.         panel.setLayout(null);
  34.         contentPane.add(panel);
  35.  
  36.         textField = new JTextField();
  37.         textField.setBounds(71, 35, 192, 26);
  38.         panel.add(textField);
  39.         textField.setColumns(10);
  40.  
  41.         passwordField = new JPasswordField();
  42.         passwordField.setBounds(71, 72, 192, 26);
  43.         panel.add(passwordField);
  44.  
  45.         JLabel lblNewLabel = new JLabel("Username:");
  46.         lblNewLabel.setBounds(6, 41, 68, 14);
  47.         panel.add(lblNewLabel);
  48.  
  49.         JLabel lblNewLabel_1 = new JLabel("Password:");
  50.         lblNewLabel_1.setBounds(6, 78, 68, 14);
  51.         panel.add(lblNewLabel_1);
  52.  
  53.         JButton btnNewButton = new JButton("Login");
  54.         btnNewButton.setBounds(71, 120, 89, 23);
  55.         panel.add(btnNewButton);
  56.  
  57.  
  58.  
  59.         JButton btnNewButton_1 = new JButton("Cancel");
  60.         btnNewButton_1.setBounds(174, 120, 89, 23);
  61.         panel.add(btnNewButton_1);
  62.    
  63. import java.awt.Color;
  64. import java.awt.Dimension;
  65. import java.awt.EventQueue;
  66. import java.awt.Graphics;
  67. import java.awt.Graphics2D;
  68. import java.awt.GridBagConstraints;
  69. import java.awt.GridBagLayout;
  70. import javax.swing.JButton;
  71. import javax.swing.JFrame;
  72. import javax.swing.JLabel;
  73. import javax.swing.JPanel;
  74. import javax.swing.JTextField;
  75. import javax.swing.UIManager;
  76. import javax.swing.UnsupportedLookAndFeelException;
  77. import javax.swing.border.EmptyBorder;
  78. import javax.swing.border.TitledBorder;
  79.  
  80. public class CenterComponent {
  81.  
  82.     public static void main(String[] args) {
  83.         new CenterComponent();
  84.     }
  85.  
  86.     public CenterComponent() {
  87.         EventQueue.invokeLater(new Runnable() {
  88.             @Override
  89.             public void run() {
  90.                 try {
  91.                     UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  92.                 } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
  93.                     ex.printStackTrace();
  94.                 }
  95.  
  96.                 JFrame frame = new JFrame("Testing");
  97.                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  98.                 JPanel content = new JPanel(new GridBagLayout());
  99.                 content.setBackground(Color.GREEN);
  100.                 content.setBorder(new EmptyBorder(20, 20, 20, 20));
  101.                 frame.setContentPane(content);
  102.                 frame.add(new LoginPane());
  103.                 frame.pack();
  104.                 frame.setLocationRelativeTo(null);
  105.                 frame.setVisible(true);
  106.             }
  107.         });
  108.     }
  109.  
  110.     public class LoginPane extends JPanel {
  111.  
  112.         public LoginPane() {
  113.             setLayout(new GridBagLayout());
  114.             setBorder(new TitledBorder("Login"));
  115.  
  116.             GridBagConstraints gbc = new GridBagConstraints();
  117.             gbc.gridx = 0;
  118.             gbc.gridy = 0;
  119.             add(new JLabel("Username:"), gbc);
  120.             gbc.gridy++;
  121.             add(new JLabel("Password:"), gbc);
  122.  
  123.             gbc.gridx++;
  124.             gbc.gridy = 0;
  125.             gbc.gridwidth = 2;
  126.             gbc.fill = GridBagConstraints.HORIZONTAL;
  127.             gbc.weightx = 1;
  128.             add(new JTextField(10), gbc);
  129.             gbc.gridy++;
  130.             add(new JTextField(10), gbc);
  131.  
  132.             gbc.gridx = 1;
  133.             gbc.gridy++;
  134.             gbc.gridwidth = 1;
  135.             gbc.weightx = 0;
  136.             gbc.fill = GridBagConstraints.NONE;
  137.             add(new JButton("Login"), gbc);
  138.             gbc.gridx++;
  139.             add(new JButton("Cancel"), gbc);
  140.         }
  141.  
  142.     }
  143.  
  144. }
  145.    
  146. panel.setLayout(null);
  147.    
  148. panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
  149.    
  150. panel.setLayout(new GridLayout(3,1, 5, 5));
  151.    
  152. void init(){
  153.    jP.setBounds(ancho/2,alto/2, miPanel.getWidth(), miPanel.getHeight());
  154. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top