将 JButton 添加到布局为“GridLayout”的 JPanel

发布于 2024-11-18 18:56:29 字数 1862 浏览 0 评论 0原文

我正在开发一个 GUI,使用 java Swing 库。我想向 Jpanel 添加一些 JButton,该 Jpanel 使用 Gridlayout 作为其布局,但我有一个问题,面板的大小大约是显示器的整个大小,但是 JButton '大小可以不同,我可能会向面板添加一个 3*3 的 Jbutton 数组,而我可能会向面板添加一个 10 * 10 数组,问题是如果我添加一个 3*3 ,jbuttons 将大到占据整个显示屏,甚至当前 JPanel 顶部的 JPanel (名为 options ),我应该做什么才能为 JButtons 设置恒定大小所以即使它们是 2 个 Jbutton(现在占据整个显示器),它们的大小也不会改变( setSize 函数不起作用,我希望布局是 GridLayout ,而不是 null ),这是代码的一些部分:

public class Edit extends JFrame{   
public Edit ()
{
    width = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();
    height = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();
    newer = new JButton(new ImageIcon(b)) ;
    done = new JButton(new ImageIcon(f)) ;
    savior = new JButton(new ImageIcon(d)) ;
    undo = new JButton(new ImageIcon(h)) ;
    newer.setSize(200, 60);
    done.setSize(200, 60);
    savior.setSize(200, 60);    
    undo.setSize(200, 60);
    options = new JPanel();
    options.setSize(width , 100);
    options.setLayout(new GridLayout(1,5,(width- 1000)/6,20)) ;
    options.add(newer);
    options.add(done);
    options.add(savior);
    options.add(undo);
    options.setBackground(Color.magenta);
    options.add(selector);
    this.setExtendedState(this.MAXIMIZED_BOTH);
    this.setUndecorated(true);
    this.setVisible(true);
    view = new JPanel();
    regions = new JButton[3][3];
    view.setSize(width, height - 100) ; 
    view.setLayout(new GridLayout(3,3));
    for(int i=0;i<3;i++)
                for(int j=0;j<3;j++)
        {
                regions[i][j] = new JButton(); 
                    regions[i][j].setSize(80,80) ;
            view.add(regions[i][j]);
            }
    editPhase = new JPanel();
    editPhase.setLayout(null);
    editPhase.add(options,BorderLayout.NORTH);
    editPhase.add(view,BorderLayout.SOUTH);
    this.add(editPhase);
    }
}

提前致谢。

i am developing a GUI , using java Swing library .I want to add some JButtons to a Jpanel which is Using a Gridlayout as its layout , but i have a problem , the panel's size is about the whole size of the monitor , but the JButtons' size can be different , I may add a 3*3 array of Jbuttons to the panel , while I may add a 10 * 10 array to the panel , the problem is that if i add a 3*3 , the jbuttons would be as large as to occupy the whole display , even the JPanel on the top of the current JPanel (which is named options ), what should i do in order to set a constant size for JButtons so their size in not changed even if they are 2 Jbuttons ( which now takes the whole display) ( the setSize function wont work , and I want the layout to be a GridLayout , not null ) , here's some parts of the code :

public class Edit extends JFrame{   
public Edit ()
{
    width = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();
    height = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();
    newer = new JButton(new ImageIcon(b)) ;
    done = new JButton(new ImageIcon(f)) ;
    savior = new JButton(new ImageIcon(d)) ;
    undo = new JButton(new ImageIcon(h)) ;
    newer.setSize(200, 60);
    done.setSize(200, 60);
    savior.setSize(200, 60);    
    undo.setSize(200, 60);
    options = new JPanel();
    options.setSize(width , 100);
    options.setLayout(new GridLayout(1,5,(width- 1000)/6,20)) ;
    options.add(newer);
    options.add(done);
    options.add(savior);
    options.add(undo);
    options.setBackground(Color.magenta);
    options.add(selector);
    this.setExtendedState(this.MAXIMIZED_BOTH);
    this.setUndecorated(true);
    this.setVisible(true);
    view = new JPanel();
    regions = new JButton[3][3];
    view.setSize(width, height - 100) ; 
    view.setLayout(new GridLayout(3,3));
    for(int i=0;i<3;i++)
                for(int j=0;j<3;j++)
        {
                regions[i][j] = new JButton(); 
                    regions[i][j].setSize(80,80) ;
            view.add(regions[i][j]);
            }
    editPhase = new JPanel();
    editPhase.setLayout(null);
    editPhase.add(options,BorderLayout.NORTH);
    editPhase.add(view,BorderLayout.SOUTH);
    this.add(editPhase);
    }
}

thanks in advance .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独木成林 2024-11-25 18:56:29

这是使用评论中的想法的实现:

public class Main {

  public static void main(String[] args) {
    JFrame frame = new JFrame();

    int gridSize = 3; // try 4 or 5, etc. buttons are always 50x50

    JPanel panel = new JPanel(new GridLayout(gridSize, gridSize));
    panel.setPreferredSize(new Dimension(500, 500));

    for (int i = 0; i < gridSize; i++) {
      for (int j = 0; j < gridSize; j++) {
        JPanel buttonPanel = new JPanel(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.anchor = GridBagConstraints.CENTER;

        JButton button = new JButton();
        button.setPreferredSize(new Dimension(50, 50));
        buttonPanel.add(button, c);
        panel.add(buttonPanel);
      }
    }
    frame.setContentPane(panel);
    frame.pack();
    frame.setVisible(true);
  }
}

Here's an implementation using ideas from the comment:

public class Main {

  public static void main(String[] args) {
    JFrame frame = new JFrame();

    int gridSize = 3; // try 4 or 5, etc. buttons are always 50x50

    JPanel panel = new JPanel(new GridLayout(gridSize, gridSize));
    panel.setPreferredSize(new Dimension(500, 500));

    for (int i = 0; i < gridSize; i++) {
      for (int j = 0; j < gridSize; j++) {
        JPanel buttonPanel = new JPanel(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.anchor = GridBagConstraints.CENTER;

        JButton button = new JButton();
        button.setPreferredSize(new Dimension(50, 50));
        buttonPanel.add(button, c);
        panel.add(buttonPanel);
      }
    }
    frame.setContentPane(panel);
    frame.pack();
    frame.setVisible(true);
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文