Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.GridBagLayout;
- import java.awt.GridLayout;
- import javax.swing.*;
- @SuppressWarnings("serial")
- public class Gui002 extends JPanel {
- private static final int TEXT_FIELD_COUNT = 3;
- private static final Color BG = Color.BLACK;
- private static final Dimension PREF_SIZE = new Dimension(500, 350);
- private JTextField[] fields = new JTextField[TEXT_FIELD_COUNT];
- public Gui002() {
- // make background black
- setBackground(BG);
- // create JPanel to hold 3 JTextFields in a single column
- JPanel innerPanel = new JPanel(new GridLayout(0, 1, 10, 0));
- // allow background color to show through this JPanel
- innerPanel.setOpaque(false);
- // create and add our 3 JTextFields
- for (int i = 0; i < fields.length; i++) {
- fields[i] = new JTextField(10);
- innerPanel.add(fields[i]);
- }
- setPreferredSize(PREF_SIZE); // not best way to do this
- // set layout of enclosing JPanel so that inner panel is centered
- setLayout(new GridBagLayout());
- add(innerPanel);
- }
- @Override
- protected void paintComponent(Graphics g) {
- super.paintComponent(g);
- // .... only painting code goes here
- }
- private static void createAndShowGui() {
- Gui002 mainPanel = new Gui002();
- JFrame frame = new JFrame("GUI");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.getContentPane().add(mainPanel); // add components first
- frame.pack();
- frame.setLocationRelativeTo(null);
- frame.setVisible(true); // set visible **after** components added
- }
- public static void main(String[] args) {
- SwingUtilities.invokeLater(() -> createAndShowGui());
- }
- }
Add Comment
Please, Sign In to add comment