简体   繁体   English

带有网格布局和JButton的JPanel无法加载/工作

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

I'm having trouble getting a JPanel inside a BorderLayout to work. 我在让BorderPad内部的JPanel正常工作时遇到麻烦。 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. 我将面板的布局定义为网格布局,然后将我之前制作的一堆按钮添加到JPanel。 However, when I run the program, the JFrame loads but nothing within the frame loads. 但是,当我运行程序时,JFrame加载,但框架内没有加载。 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. 另外,我还有另一个类可以调用Phone类。 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 非常感谢,M3tal T1ger

Your main problem: 您的主要问题:

  • You should only call setVisible(true) after adding components to your GUI. 将组件添加到GUI 后,仅应调用setVisible(true) You don't do this and so the GUI gets drawn without its components. 您无需执行此操作,因此将绘制没有其组件的GUI。

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. 并且不要忘记在添加所有组件之后并且使GUI可见之前调用pack()
  • 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. 学习并遵循Java命名约定,包括为所有变量和方法提供以小写字母开头的名称,并为所有具有以大写字母开头的名称的类。

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();
         }
      });
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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