JPanels中的JButtons带有GridLayout JFrame [英] JButtons inside JPanels with a GridLayout JFrame

查看:95
本文介绍了JPanels中的JButtons带有GridLayout JFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java / Eclipse IDE。



我有一个JFrame设置为大小为NxN的GridLayout。用户将N作为程序开头的命令行给出。 NxN模式中的JButtons被添加到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(面板);
}
}

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



我需要/想要的是两个(或更多)JPanels,它们大致如上所述,我尝试过的所有布局管理器都不能与GridLayout布局JFrame一起使用。



欢迎任何有关更好解决方案的建议。



谢谢!

解决方案

这里尝试这个代码示例:

  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 =被按下;
私人JLabel positionLabel;
private JButton resetButton;
private static int gridSize = 4;

public LayoutExample()
{
super(布局示例);
}

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(按钮);
}
}
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();
}
});
}
}

输出:





Java/Eclipse IDE here.

I have one JFrame that is set to a GridLayout of size NxN. N is given by the user as a command line at the start of the program. JButtons in an NxN pattern are added to the window in JPanels, set in location by GridLayout (I think).

Does each JButton need its own JPanel to use GridLayout? I'm under the impression that you can set up just one JPanel for all the buttons and set the JPanel to a GridLayout for the JButtons. I want to add another JPanel to the left of the button array to display button clicks (JLabel) and a reset button within that same left JPanel.

Here is (a little of) my code, where N is given by the user, system is my background processes class, and ButtonEvent is the class for 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);

And this is what I have (N=4):

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

Here is (approximately) what I am looking for (N=4):

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

All I need/want is two (or more) JPanels that are set up roughly like above, and all the layout managers I've tried don't play nice with the GridLayout layout JFrame.

Any recommendations as to a better solution are invited.

Thanks!

解决方案

Here try this code example :

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();
            }
        });
    }
}

OUTPUT :

这篇关于JPanels中的JButtons带有GridLayout JFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆