简体   繁体   中英

JPanel with Grid Layout and JButtons doesn't load / work

I'm having trouble getting a JPanel inside a BorderLayout to work. I defined the layout of the Panel as a Grid Layout, and then added a bunch of buttons I had made before hand to the JPanel. However, when I run the program, the JFrame loads but nothing within the frame loads. Here's the code:

import java.awt.*;
import javax.swing.*;

public class Phone extends JFrame {
private JTextField PhoneText;
private JPanel ButtonPanel;
private JButton bttn1;
private JButton bttn2;
private JButton bttn3;
private JButton bttn4;
private JButton bttn5;
private JButton bttn6;
private JButton bttn7;
private JButton bttn8;
private JButton bttn9;
private JButton bttn10;
private JButton bttn11;
private JButton bttn12;

public Phone(){
    setTitle("Phone - Agustin Ferreira");
    Container ContentPane = getContentPane();
    ContentPane.setLayout(new BorderLayout());
    setSize(300, 400);
    setVisible(true);
    setBackground(Color.DARK_GRAY);
    PhoneText = new JTextField("(317)188-8566");
    bttn1 = new JButton ("1");
    bttn2 = new JButton ("2");
    bttn3 = new JButton ("3");
    bttn4 = new JButton ("4");
    bttn5 = new JButton ("5");
    bttn6 = new JButton ("6");
    bttn7 = new JButton ("7");
    bttn8 = new JButton ("8");
    bttn9 = new JButton ("9");
    bttn10 = new JButton ("*");
    bttn11 = new JButton ("0");
    bttn12 = new JButton ("#");
    ButtonPanel = new JPanel(new GridLayout(4,3,0,0));

    ButtonPanel.add(bttn1);
    ButtonPanel.add(bttn2);
    ButtonPanel.add(bttn3);
    ButtonPanel.add(bttn4);
    ButtonPanel.add(bttn5);
    ButtonPanel.add(bttn6);
    ButtonPanel.add(bttn7);
    ButtonPanel.add(bttn8);
    ButtonPanel.add(bttn9);
    ButtonPanel.add(bttn10);
    ButtonPanel.add(bttn11);
    ButtonPanel.add(bttn12);
    ContentPane.add(PhoneText, BorderLayout.NORTH);
    ContentPane.add(ButtonPanel, BorderLayout.CENTER);
}

}

Additionally, I have another class that calls the Phone class. Here's the code for that, just in case:

package ProgrammingAssignment11;

import javax.swing.JFrame;

public class GUI_Driver {

public static void main(String[] args) {
    Phone Nokia;
    Nokia = new Phone();
    Nokia.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}

}

Any Help?

Much appreciated, M3tal T1ger

Your main problem:

  • You should only call setVisible(true) after adding components to your GUI. You don't do this and so the GUI gets drawn without its components.

Also:

  • You should avoid setting the sizes or preferred sizes of anything. Instead let the components and layout managers size themselves.
  • And don't forget to call pack() after adding all components and before making the GUI visible.
  • Learn and follow Java naming conventions, including giving all variables and methods names that begin with a lower case letter, and all classes with names that start with an upper-case letter.

For example:

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;

import javax.swing.*;

@SuppressWarnings("serial")
public class Phone2 extends JPanel {
   private static final String[][] BTN_TEXTS = {
      {"1", "2", "3"},
      {"4", "5", "6"},
      {"7", "8", "9"},
      {"*", "0", "#"}
   };
   private static final float BTN_POINTS = 48f;
   private static final float TEXT_POINTS = 24f;
   private static final int DISPLAY_COLUMNS = 12;

   private JButton[][] buttons = new JButton[BTN_TEXTS.length][BTN_TEXTS[0].length];
   private JTextField display = new JTextField(DISPLAY_COLUMNS);

   public Phone2() {
      display.setFocusable(false);
      display.setFont(display.getFont().deriveFont(TEXT_POINTS));
      GridLayout gridLayout = new GridLayout(BTN_TEXTS.length, BTN_TEXTS[0].length);
      JPanel btnPanel = new JPanel(gridLayout);
      for (int i = 0; i < BTN_TEXTS.length; i++) {
         for (int j = 0; j < BTN_TEXTS[i].length; j++) {
            String text = BTN_TEXTS[i][j];
            JButton btn = new JButton(new BtnAction(text));
            btn.setFont(btn.getFont().deriveFont(Font.BOLD, BTN_POINTS));
            btnPanel.add(btn);
            buttons[i][j] = btn;
         }
      }

      setLayout(new BorderLayout());
      add(display, BorderLayout.NORTH);
      add(btnPanel, BorderLayout.CENTER);
   }

   private class BtnAction extends AbstractAction {
      public BtnAction(String name) {
         super(name);
      }

      @Override
      public void actionPerformed(ActionEvent evt) {
         String text = evt.getActionCommand();
         display.setText(display.getText() + text);
      }
   }

   private static void createAndShowGui() {
      Phone2 mainPanel = new Phone2();

      JFrame frame = new JFrame("Phone");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM