Advertisement
Guest User

TelaRelativaTest

a guest
Mar 27th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.06 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.Dimension;
  3. import java.awt.EventQueue;
  4. import java.awt.Toolkit;
  5.  
  6. import javax.swing.JButton;
  7. import javax.swing.JFrame;
  8. import javax.swing.JLabel;
  9. import javax.swing.JPanel;
  10. import javax.swing.JScrollPane;
  11. import javax.swing.JTable;
  12. import javax.swing.JTextField;
  13. import javax.swing.border.EmptyBorder;
  14.  
  15. public class TelaRelativaTest extends JFrame {
  16.  
  17.     private JPanel contentPane;
  18.     private JTextField textField;
  19.     private JTable table;
  20.  
  21.     public static void main(String[] args) {
  22.         EventQueue.invokeLater(() -> new TelaRelativaTest().setVisible(true));
  23.     }
  24.  
  25.  
  26.     public TelaRelativaTest() {
  27.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  28.  
  29.         setPreferredSize(getRelativeDimension());
  30.         contentPane = new JPanel();
  31.         contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  32.         setContentPane(contentPane);
  33.         contentPane.setLayout(new BorderLayout(0, 0));
  34.        
  35.         JPanel panel = new JPanel();
  36.         contentPane.add(panel, BorderLayout.NORTH);
  37.        
  38.         JLabel lblNewLabel = new JLabel("New label");
  39.         panel.add(lblNewLabel);
  40.        
  41.         textField = new JTextField();
  42.         panel.add(textField);
  43.         textField.setColumns(8);
  44.        
  45.         JButton btnNewButton = new JButton("New button");
  46.         panel.add(btnNewButton);
  47.        
  48.         Object[][] data = {{null, null, null},{null, null, null},{null, null, null},{null, null, null}};
  49.         Object[] columns =  { "Column One", "Column Two", "Column Three"};
  50.        
  51.         table = new JTable(data, columns);
  52.  
  53.         JScrollPane scrollPane  = new JScrollPane(table);
  54.         contentPane.add(scrollPane, BorderLayout.CENTER);
  55.        
  56.         pack();
  57.         setLocationRelativeTo(null);
  58.    
  59.     }
  60.    
  61.     private Dimension getRelativeDimension() {
  62.         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();  
  63.  
  64.         int width = screenSize.width;  
  65.         int height = screenSize.height;  
  66.        
  67.         // valor que definira a proporcao do JFrame em
  68.         // relacao ao tamanho da tela do usuario
  69.         //valores maximos de 0 a 1 onde 1 é semelhante a fullscreen
  70.         float ratio = 0.5f;
  71.        
  72.         return new Dimension(Math.round(width*ratio), Math.round(height*ratio));
  73.     }
  74.    
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement