java - JFrame打开很小

标签 java swing jframe

我的程序的 JFrame 打开非常小,我已经研究了几个类似的问题和修复,但到目前为止没有一个起作用......

我完全被这个问题难住了,通常当我使用 jframes 时它永远不会这样做,我是否忘记了一些简单的事情?

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pkgfinal.project;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 *
 * @author conor
 */
public class SolBoard extends javax.swing.JFrame {
    
    private final int[][] laysout = {
        {0,0,1,1,1,0,0},
        {0,0,1,1,1,0,0},
        {1,1,1,1,1,1,1},
        {1,1,1,1,1,1,1},
        {1,1,1,1,1,1,1},
        {0,0,1,1,1,0,0},
        {0,0,1,1,1,0,0,}
    };
    
    private final javax.swing.JButton[][] Board = new javax.swing.JButton[7][7];
     
    
    public SolBoard() {
        initComponents();
    }
    
       
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {
        for(int i=0;i<7;i++) {
            for(int j=0;j<7;j++){
                if(laysout[i][j]==1) {
                    Board[i][j] = new javax.swing.JButton();
                    Board[i][j].setText(i + "," + j);
                    Board[i][j].setBounds(j*60 + 10, i*60 + 10, 50, 50 );
                    
                    Board[i][j].addActionListener((ActionEvent e) -> {
                        javax.swing.JButton button = (javax.swing.JButton) e.getSource();
                        System.out.println(button.getActionCommand());
                    });
                    
                    getContentPane().add(Board[i][j]);
                }        
            }
        }
        getContentPane().add(new javax.swing.JButton("v.1.0"));
    }
    
    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Board.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        
        //</editor-fold>
    

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(() -> {
            
            new SolBoard().setVisible(true);
            
        });
    }
}

它应该打开到面板的大小(按钮数组)

最佳答案

尝试将此代码放入您的initComponents()中:

this.setVisible(true);
this.getContentPane().setSize(400, 400);

或者这个:

this.setPreferredSize(new Dimension(400, 300));
this.pack();
this.setLocationRelativeTo(null);
this.setVisible(true);

我认为在这种情况下顺序很重要

关于java - JFrame打开很小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56281212/

相关文章:

java - 如何为 BoxLayout 创建垂直 javax.swing.ComponentOrientation?

Java GUI 未显示

java - 如何将 JFrame 永久放置在另一个 JFrame 之上?

java - 在多个 JPanel 中绘图

java - 2 个 JFrames 有 2 个单独的关闭按钮?

java - java.awt.Color 对象的大小

java - Hadoop、MapReduce - 多输入/输出路径

java - HashMap 仅在选择某个键时打印值

java - JAXB 构造函数注入(inject)

java - 定位元素和调整大小的问题