简体   繁体   English

添加到JFrame的JPanel没有任何作用

[英]JPanel added to JFrame does nothing

I have the following code: 我有以下代码:

public static void main(String[] args){
    Table testtable= new Table();
    testtable.setVisible(true);

and: 和:

public class ChessPanel extends JPanel {
@Override
public void paintComponent(Graphics g){

    // intitlialize background-color
    super.paintComponent(g);
    int squareWidth = this.getWidth()/8;
    int squareHeight = this.getHeight()/8;
    this.setBackground(Color.WHITE);
    for(int i = 0; i<8; i++) {
        for(int j = 0; j<8; j++) {
            if(((i+j)%2) == 1) {
                g.setColor(new Color(128, 64, 0));
                g.fillRect(squareWidth*i, squareHeight*j, squareWidth, squareHeight);
            }
        }
    }

}

} }

and: 和:

public class Table extends javax.swing.JFrame {

/**
 * Creates new form Table
 */
public Table() {
    initComponents();

    ChessPanel jpc = new ChessPanel();
    getContentPane().add(jpc);
    pack();
    setVisible(true);

}

when I add the JPanel to the JFrame nothing happens. 当我将JPanel添加到JFrame时,什么也没有发生。 It is supposed to draw a chessboard. 应该画一个棋盘。 I simply missed something I guess, but I can't find the solution. 我只是想念一些东西,但找不到解决方案。

I have tried multiple ways of adding my JPanel to the frame, but nothing seems to draw the expected chessboard. 我尝试了多种将JPanel添加到框架的方法,但是似乎没有画出预期的棋盘。

Thanks in advance,, 提前致谢,,

Looks ok to me. 对我来说还不错。 I only added an override of getPreferredSize: 我只添加了getPreferredSize的重写:

样品

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class ChessPanel extends JPanel {
    @Override
    public void paintComponent(Graphics g) {

        // intitlialize background-color
        super.paintComponent(g);
        int squareWidth = this.getWidth() / 8;
        int squareHeight = this.getHeight() / 8;
        this.setBackground(Color.WHITE);
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                if ((i + j) % 2 == 1) {
                    g.setColor(new Color(128, 64, 0));
                    g.fillRect(squareWidth * i, squareHeight * j, squareWidth, squareHeight);
                }
            }
        }
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(600, 600);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                ChessPanel jpc = new ChessPanel();
                frame.getContentPane().add(jpc);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

If you want to add JPanel along with other components then 如果要与其他组件一起添加JPanel,则

ChessPanel jpc = new ChessPanel();
getContentPane().add(jpc);
validate(); //Add this line
pack();
setVisible(true);

Or, if you want to set JPanel's content as a whole in JFrame's contentPane then 或者,如果您想在JFrame的contentPane中将JPanel的内容设置为一个整体,则

ChessPanel jpc = new ChessPanel();
this.setContentPane(jpc);
validate();

would do the job. 会做的工作。

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

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