如何让JPanel适应其组件?

时间:2014-12-31 19:45:35

标签: java swing jframe window size

我有2个问题。

第一个问题:我必须将JFrame设置为不可调整大小,但是当我输入frame.setResizable(false)时会抛出错误;

第二个问题:我遇到了JFrame问题,完全不适合组件。我已将JFrame的尺寸设置为600x720,而电路板组件的尺寸设置为600x600。但是,当我扩展JFrame时,我可以看到有更多的组件需要显示。我如何更改此设置以适应组件的贴合性,同时还为另一个尺寸为100x120的组件留出空间?我对此的理解是JFrame设置了包含边框的大小,但是,我希望JFrame中的空间恰好是600x720像素而不包括边框。

代码如下。

游戏类

package snake;

//This class is used to run the game.
public class Game {

    /**
     * @author HyperBlue
     */

    //Declaring a static variable of type Board. This can be accessed from anywhere in the program. The fact that it is static means that it cannot be edited.
    public static Board board;

    public static void main(String[] args) {
        // TODO Auto-generated method stub

    //Creates an object board from the Board() construct
    board = new Board();

    }


}

董事会成员

package snake;

//Importing allows us to use pre-defined classes in Java, this contains its methods. We can also import entire packages which contain a number of classes in that package.
//This class allows us to assign/capture the width and height of an object.

import java.awt.Dimension;
//The Toolkit is an abstract class containing abstract and (possibly) non-abstract methods. Abstract classes cannot be instantiated (i.e. we cannot make an object from them). Abstract methods have no body (no code), for example we declare it as "public abstract boolean isChanged() ;", the semi colon shows it has no body (i.e. no {}).
import java.awt.Toolkit;

//ActionEvent gets information about an event (input) and its source. You can create an object from this.
import java.awt.event.ActionEvent;
//The ActionListener defines what should be done when a certain action is performed by the user.
import java.awt.event.ActionListener;



//This imports the JFrame class from the swing package.
import javax.swing.JFrame;
import javax.swing.Timer;

//This class is used to create the game board.
//The ActionListener is implemented because it is implementing an interface. What ActionListener does is it handles events; the ActionListener defines what should be done when a certain action is performed by the user.
public class Board implements ActionListener {

    //The JFrame is the window in which everything will be placed into, this will provide the framed window (what is visible to us) in which the game will run in. We are creating a variable frame of type JFrame.
    public JFrame frame;
    //Creating a variable drawBoard of type DrawBoard. This allows us to add the component of drawBoard to the Board.
    public DrawBoard drawBoard;
    //Defining a new Timer called ticker. This is using the form  new Timer(int delay in milliseconds, ActionListener listener).  What the timer does is it allows threads to schedule the execution of instructions. In this case to constantly refresh the drawBoard component at regular intervals. This will give the appearance of motion. What "this" does is it is in reference to the current instance, 
    public Timer ticker = new Timer(20, this);


    //This is a constructor for the class Board. This will allow us to create an object.
    public Board() {
        //Making an instance of dimension dim and assigning it to the size of the screen.
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        //Declaring instance of the JFrame 'frame'. This JFrame is called to declare a title for this frame - "Snake".
        frame = new JFrame("Snake");
        //JFrame is initially set to invisible, so we use the setVisible method (setting it to true) to make the JFrame 'frame' visible.
        frame.setVisible(true);
        frame.setPreferredSize(new Dimension(600, 720));
        frame.getContentPane().add(drawBoard = new DrawBoard());
        frame.pack(); 
        //What this does is it places the JFrame 'frame' into the middle of the user's screen, this diminishes the issue of not all screens being the same resolution and size. This is done by setting the (x, y) position of the JPanel. For example, the x position is gained by dividing the size of the monitor by 2 and negating the size of the JPanel by 2 from that value, this places it in the middle of the screen's x axis. This is true for the y-axis too. 
        frame.setLocation((dim.width / 2) - (frame.getWidth() / 2), (dim.height / 2) - (frame.getHeight() / 2));
        //Sets the operation which will happen when the user closes the JFrame 'frame', the EXIT_ON_CLOSE exits the application using the System exit method. This means that when the JFrame is closed, the application will be exited (closed).
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //Starts the timer, this starts is sending action events to its listeners.
        ticker.start();
    }


    //Overriding the actionPerformed method from the ActionListener class.
    @Override
    //This is the actionPerformed method. We parse it the ActionEvent e, what this is is an object which gives information about the event and its source. This allows us to perform an action based upon a specific event (e.g. a keyboard key pressed).
    public void actionPerformed(ActionEvent e) {
        //This repaints this component for every tick
        //drawBoard.repaint();
    }
}

DrawBoard类

package snake;

//Allows use of default sRGB colours.
import java.awt.Color;
//Graphics is an abstract class that allows us to draw onto components.
import java.awt.Graphics;

import javax.swing.JPanel;

//Warnings will not be thrown (are suppressed).
@SuppressWarnings("serial")

//This class is used to create the board component in which the snake can move on.
//What extending does is it allows us to inherit the methods and attributes (properties) of another class. In this case, the DrawBoard class (subclass - inherits state and behaviour from all of its ancestors) inherits properties from the JPanel class (superclass - gives properties to its subclasses).
public class DrawBoard extends JPanel{

    //Declaring the colour 'yellow' as the hex colour code (turned to decimal using a hex calculator so Java can use it) which was chosen in the design stage.
    public static Color yellow = new Color(13816442);

    //We are overriding the protected method in order to define our own body (and properties) for the paintComponent method. Overriding this allows us to define how we will paint the component DrawBoard. Protected means that it can only be accessed by things within the same package.
    @Override
    //A component is an object which has a graphical representation that can interact with the user (e.g. buttons).
    //What this does is it paints the component using the graphics class, defined as instance g.
    protected void paintComponent(Graphics g) {
        //'Super.' refers to the method calling its super class, which in this case is JPanel. Doing this allows me to use in built 'drawings' such as rectangle and oval, which can be drawn by calling their methods.
        super.paintComponent(g);
        //Setting the colour in which graphics objects are made to the colour defined in the colour 'yellow'
        g.setColor(yellow);
        //Filling in a rectangle which starts at the point (0, 120) - [this is from the top left of the screen, with (0, 120) referring to 120 pixels down] and has a width and height of (600, 600), in other words provides a background of colour 'yellow' defined.
        g.fillRect(0, 120, 600, 600);
    }

}

1 个答案:

答案 0 :(得分:4)

关于:

  

如何让JPanel与其组件紧密配合?

让布局管理员为您完成这项工作。

建议:

  • 请勿设置尺寸或首选尺寸。
  • 而是让组件的首选大小和布局管理器为您调整大小。
  • 如果您确实需要积极设置某些尺寸,请覆盖getPreferredSize()并返回适当的尺寸,但这样做时请注意不要破坏构成组件的首选尺寸。这必须小心。
  • Re " I have set the dimensions for the JFrame to 600x720 and the board component as being 600x600. However, when I extend the JFrame I can see that there is more of the component to be revealed." - 您忘记了JFrame顶部栏的大小,这可能会根据外观和风格改变大小。同样,不要设置JFrame的大小,这将是一个没有实际意义的点。
  • 要将JFrame居中,只需在打包后调用frame.setLocationRelativeTo(null);即可。
  • 避免过度使用评论,因为这会使您的代码几乎无法读取。
  • 如果您遇到问题并需要帮助解决错误,例如您在此处提及的问题:"I have to set the JFrame as non resizable, however, an error is thrown up when I enter in frame.setResizable(false);",则会显示有问题的代码和错误消息。
  • 在添加所有组件并打包JFrame之前,请勿调用frame.setVisible(true);

例如

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.*;
import javax.swing.*;

public class Game {
   public static Board board;

   public static void main(String[] args) {
      board = new Board();
   }
}

class Board implements ActionListener {
   public JFrame frame;
   public DrawBoard drawBoard;
   public Timer ticker = new Timer(20, this);

   public Board() {
      frame = new JFrame("Snake");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      // frame.setPreferredSize(new Dimension(600, 720));
      frame.getContentPane().add(drawBoard = new DrawBoard(), BorderLayout.CENTER);
      frame.getContentPane().add(new BottomComponent(), BorderLayout.PAGE_END);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
      ticker.start();
   }

   @Override
   public void actionPerformed(ActionEvent e) {
   }
}

class DrawBoard extends JPanel {
   private static final int PREF_W = 600;
   private static final int PREF_H = PREF_W;
   public static Color yellow = new Color(13816442);

   public DrawBoard() {
      setBorder(BorderFactory.createTitledBorder("Draw Board"));
   }

   @Override
   public Dimension getPreferredSize() {
      if (isPreferredSizeSet()) {
         return super.getPreferredSize();
      }
      return new Dimension(PREF_W, PREF_H);
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      g.setColor(yellow);
      g.fillRect(0, 120, 600, 600);
   }
}

class BottomComponent extends JPanel {
   private static final int PREF_W = 600;
   private static final int PREF_H = 120;

   public BottomComponent() {
      setBorder(BorderFactory.createTitledBorder("Bottom Component"));
   }

   @Override
   public Dimension getPreferredSize() {
      if (isPreferredSizeSet()) {
         return super.getPreferredSize();
      }
      return new Dimension(PREF_W, PREF_H);
   }


}