为什么我的按钮占用了整个JFrame? [英] Why Are My Buttons Taking Up The Whole JFrame?

查看:135
本文介绍了为什么我的按钮占用了整个JFrame?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码以前工作正常,但是现在一个JButton占用了整个JFrame,我们将不胜感激!

My code was working before, but now one JButton takes up the entire JFrame, any help would be appreciated!

我正在使用一个名为fl的FlowLayout和一个名为Ken的类

I am using a FlowLayout called fl and a class called Ken

我有十个按钮(oneButton,twoButton,ThreeButton等.)

I have ten buttons called (oneButton, twoButton, threeButton, etc..)

我的代码:

import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.FlowLayout;


import javax.swing.JButton;
import javax.swing.JFrame;



public class Ken {

public static void frame(){
    JFrame frame = new JFrame("Pow");
    frame.setSize(500, 600);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setResizable(false);
    FlowLayout fl = new FlowLayout(0, 50, 40);


    JButton oneButton = new JButton("         1         ");
    oneButton.setPreferredSize(new Dimension(100, 90));
    frame.add(oneButton);
    JButton twoButton = new JButton("         2         ");
    twoButton.setPreferredSize(new Dimension(100, 90));
    frame.add(twoButton);
    JButton threeButton = new JButton("         3         ");
    threeButton.setPreferredSize(new Dimension(100, 90));
    frame.add(threeButton);
    JButton fourButton = new JButton("         4         ");
    fourButton.setPreferredSize(new Dimension(100, 90));
    frame.add(fourButton);
    JButton fiveButton = new JButton("         5         ");
    fiveButton.setPreferredSize(new Dimension(100, 90));
    frame.add(fiveButton);
    JButton sixButton = new JButton("          6         ");
    sixButton.setPreferredSize(new Dimension(100, 90));
    frame.add(sixButton);
    JButton sevenButton = new JButton("         7         ");
    sevenButton.setPreferredSize(new Dimension(100, 90));
    frame.add(sevenButton);
    JButton eightButton = new JButton("         8         ");
    eightButton.setPreferredSize(new Dimension(100, 90));
    frame.add(eightButton);
    JButton nineButton = new JButton("         9         ");
    nineButton.setPreferredSize(new Dimension(100, 90));
    frame.add(nineButton);
    JButton zeroButton = new JButton("                                                                       
                                  0                               ");
    zeroButton.setPreferredSize(new Dimension(400, 90));
    frame.add(zeroButton);
    frame.setComponentOrientation(
            ComponentOrientation.LEFT_TO_RIGHT);
}



public static void main(String[] args){
    frame();
}
}

有什么可以尝试的吗?

推荐答案

我正在使用一个名为fl的FlowLayout.

I am using a FlowLayout called fl ...

实际上您并未使用FlowLayout,因为您从未在容器上调用setLayout(...).相反,框架的contentPane使用contentPanes的默认布局BorderLayout,这将导致以默认方式添加的最后一个组件填充容器的BorderLayout.CENTER位置.

You are not in fact using FlowLayout as you never call setLayout(...) on a container. Instead your Frame's contentPane is using the default layout for contentPanes, BorderLayout, which will cause the last component added in a default way to fill up the container's BorderLayout.CENTER location.

一种解决方案是使用布局:

So one solution is to use your layout:

FlowLayout fl = new FlowLayout(0, 50, 40);    
frame.getContentPane().setLayout(fl);

但是,话虽如此,您最好不要设置组件的首选大小,而应使用嵌套容器的组合,每个容器都使用自己的布局,以实现令人愉悦且复杂的GUI,从而更容易实现保持和扩展.

But having said that, you will probably be better off not setting the preferred sizes of your components but instead using a combination of nested containers each using its own layout in order to achieve a pleasing and complex GUI that will be much easier to maintain and extend.

或者,您可以使用更复杂的布局,例如GridBagLayout.例如:

Alternatively, you could play with more complex layouts such as the GridBagLayout. For example:

import java.awt.Component;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.*;

public class Ken2 {
   private static final String[][] NUMBERS = {
      {"1", "2", "3"},
      {"4", "5", "6"},
      {"7", "8", "9"},
      {"0"}
   };
   private static final float BUTTON_FONT_PTS = 45f;
   private static final int INSETS = 20;
   private static final Insets BUTTON_INSETS = new Insets(INSETS, INSETS, 
           INSETS, INSETS);
   private static final int IPAD = 20;
   private JPanel mainPanel = new JPanel();

   public Ken2() {
      mainPanel.setLayout(new GridBagLayout());
      for (int row = 0; row < NUMBERS.length; row++) {
         addRowToPanel(row, NUMBERS[row]);
      }
   }

   private void addRowToPanel(int row, String[] numbersRow) {
      int columns = numbersRow.length;
      for (int col = 0; col < numbersRow.length; col++) {
         addNumberButton(row, col, columns, numbersRow[col]);
      }
   }

   private void addNumberButton(int row, int col, int columns, 
           String numberText) {
      JButton button = new JButton(numberText);
      button.setFont(button.getFont().deriveFont(Font.BOLD, BUTTON_FONT_PTS));
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.gridx = col;
      gbc.gridy = row;
      gbc.gridheight = 1;
      gbc.gridwidth = 3 / columns;
      gbc.weightx = 1.0;
      gbc.weighty = 1.0;
      gbc.anchor = GridBagConstraints.CENTER;
      gbc.fill = GridBagConstraints.BOTH;
      gbc.insets = BUTTON_INSETS;
      gbc.ipadx = IPAD;
      gbc.ipady = IPAD;

      mainPanel.add(button, gbc);
   }

   private static void createAndShowGui() {
      Ken2 ken = new Ken2();

      JFrame frame = new JFrame("Ken2");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(ken.getMainPanel());
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   private Component getMainPanel() {
      return mainPanel;
   }

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

这篇关于为什么我的按钮占用了整个JFrame?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆