当前位置: 首页 > 知识库问答 >
问题:

带有GridLayout JFrame的JPanel内的JButtons

赏高格
2023-03-14

我有一个JFrame,它被设置为大小为NxN的GridLayout。N由用户在程序开始时作为命令行给出。NxN模式中的JButton被添加到JPanels中的窗口中,由GridLayout(我认为)在位置中设置。

每个JButton是否需要自己的JPanel来使用GridLayout?我的印象是,您可以为所有按钮设置一个JPanel,并将JPanel设置为JButtons的GridLayout。我想在按钮数组的左侧添加另一个JPanel,以显示按钮单击(JLabel)和同一个左侧JPanel中的重置按钮。

下面是我的代码(一小部分),其中N由用户给出,system是我的后台进程类,ButtonEvent是ActionListener/actionPerformed的类:

JFrame window = new JFrame("");
GridLayout layout = new GridLayout(N,N);
window.setLayout(layout);

for (int row = 0; row < N; row++){
    for (int col = 0; col < N; col++){
        JPanel panel = new JPanel();
        JButton b = new JButton ("("+row+","+col+")");
        window.add(b).setLocation(row, col);
        panel.add(b);
        b.addActionListener(new ButtonEvent(b, system, row, col));
        window.add(panel);
    }
}

window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.pack();
window.setVisible(true);

这就是我得到的(N=4):

http://i.imgur.com/nbQoM.png

以下是(大约)我正在寻找的(N=4):

http://i.imgur.com/SiVWO.png

我所需要/想要的只是两个(或更多)JPanel,它们的设置大致与上面的一样,我尝试过的所有布局管理器都不能很好地使用GridLayout JFrame。

共有3个答案

郎项禹
2023-03-14

为每个按钮(或按钮组)提供自己的面板的一个优点是嵌套面板可以具有不同的布局。在本例中,每个嵌套的ButtonPanel都具有默认的FlowLayout,因此当调整封闭容器的大小时,按钮的大小保持不变。

冀冯浩
2023-03-14

每个JButton是否需要自己的JPanel来使用GridLayout?

没有。在JFrame上添加和设置布局不要做他们似乎要做的事情。JFrame是顶级容器,最好在JPanels中组织您的内容。

您应该以这种形式组织面板:

----JPanel----------------------------|
| ---LeftPanel---  ---ButtonsPanel--- |
| |             |  |                | |
| |             |  |                | |
| |             |  | GridLayout(N,N)| |
| |             |  |                | |
| |             |  |                | |
| ---------------  ------------------ |
---------------------------------------

然后将JPanel添加到JFrame。还将面板分为不同的类别:

class BPanel extends JPanel {
    public BPanel() {
        GridLayout layout = new GridLayout(N,N, hgap, vgap);
        setLayout(layout);
        for (int row = 0; row < N; row++){
            for (int col = 0; col < N; col++){
                JButton b = new JButton ("("+row+","+col+")");
                add(b).setLocation(row, col);
                b.addActionListener(new ButtonEvent(b, system, row, col));
            }
        }
    }
}
class LeftPanel extends JPanel {
....
}

class MainFrame extends JFrame {
    public MainFrame() {
        JPanel p = new JPanel();
        p.add(new LeftPanel());
        p.add(newButtonsPanel());
        add(p);
    }
}
瞿文柏
2023-03-14

在这里尝试这个代码示例:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class LayoutExample extends JFrame
{
    private static final String INITIAL_TEXT = "Nothing Pressed";
    private static final String ADDED_TEXT = " was Pressed";
    private JLabel positionLabel;
    private JButton resetButton;
    private static int gridSize = 4;

    public LayoutExample()
    {
        super("Layout Example");
    }

    private void createAndDisplayGUI()
    {       
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new FlowLayout(FlowLayout.LEFT, 20, 20));
        contentPane.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 2));

        JPanel leftPanel = new JPanel();
        leftPanel.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 2));
        leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));    
        JPanel labelPanel = new JPanel();
        positionLabel = new JLabel(INITIAL_TEXT, JLabel.CENTER);
        JPanel buttonLeftPanel = new JPanel();
        resetButton = new JButton("Reset");
        resetButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                positionLabel.setText(INITIAL_TEXT);
            }
        });
        labelPanel.add(positionLabel);
        buttonLeftPanel.add(resetButton);
        leftPanel.add(labelPanel);
        leftPanel.add(buttonLeftPanel);

        contentPane.add(leftPanel);

        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridLayout(gridSize, gridSize, 10, 10));
        for (int i = 0; i < gridSize; i++)
        {
            for (int j = 0; j < gridSize; j++)
            {
                JButton button = new JButton("(" + i + ", " + j + ")");
                button.setActionCommand("(" + i + ", " + j + ")");
                button.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent ae)
                    {
                        JButton but = (JButton) ae.getSource();
                        positionLabel.setText(
                            but.getActionCommand() + ADDED_TEXT);                           
                    }
                });
                buttonPanel.add(button);
            }
        }
        contentPane.add(buttonPanel);

        setContentPane(contentPane);
        pack();
        setLocationByPlatform(true);
        setVisible(true);
    }

    public static void main(String[] args)
    {
        if (args.length > 0)
        {
            gridSize = Integer.parseInt(args[0]);
        }
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new LayoutExample().createAndDisplayGUI();
            }
        });
    }
}

输出:

 类似资料:
  • 你好,我实际上正在尝试从jPanel切换到我的JFrame中的另一个jPanel。我已经更改了我的代码,下面是我遵循以下内容的样子:

  • 我希望我的解释足够好,有人能帮助我:-) 编辑:随着camickr的改进而更新,垂直尺寸问题得到解决。

  • 我有一个,名为,布局设置为。在面板内部有三个,分别添加到、和。现在,我的要求是,如果单击Settings按钮,它将在上方显示一个透明的。这个透明的面板将包含一个不透明的,更小的,居中的面板,其中将包含设置的东西。 我知道我可以使用来实现这一点,但是查看教程,它说您只能使用绝对定位来放置不同深度的组件,我知道这是非常不鼓励的。 是否有其他方法可以做到这一点,而不使用绝对定位?

  • 我最近需要在一个JScrollPane的viewport视图中放置几个组件,其中包括一个JTextPane。 我将所有组件(两个JPanel和JTextPane)放在另一个JPanel中,这个JPanel有一个BorderLayout LayoutManager,并将该JPanel设置为ScrollPane的viewport视图。 我立即注意到: JTextPane不再根据JScrollPane的

  • 我有一个叫做CatalogPane的JPanel,它的大小是800 x 600,它位于一个叫做BookFrame的JFrame中的JTabbedPane中。因此,在CatalogPane中,我创建了一个名为bookDisplay的JPanel,它显示书籍及其详细信息的列表。我希望它的大小为780 x 900,滚动条的大小为20px,并且比框架高,这样就可以滚动。然后我创建了一个尺寸为800 x 4

  • 我有一个JFrame,里面装满了JPanel(下面的代码)。 我正在使用JPanel在里面画东西,例如,我可以在任何我喜欢的地方画线,但是当添加JLabel到它的时候,我不能把它移动到它卡住的任何地方 但问题是他们建议的解决方案对我不起作用。 很抱歉,如果我不清楚,我试图在创建Surface之前和之后在函数initUI()中添加上面的内容。 但是在那之后,框架显示几乎(1,1)大小和它的空(如果我