Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何更改滚动面板中按钮的大小_Java_Eclipse_Swing - Fatal编程技术网

Java 如何更改滚动面板中按钮的大小

Java 如何更改滚动面板中按钮的大小,java,eclipse,swing,Java,Eclipse,Swing,我想更改按钮的大小。我想为每个按钮设置一个方形视图,但不幸的是,它提供了一个矩形视图。只有当我将行数设置为20或25时,我才能获得方形外观。现在,我的GUI如下所示: 我尝试将其从buttons[I][j].setMaximumSize(新维度(20,20))更改为buttons,其中buttons是数组的名称。我也尝试过按钮[I][j]。设置大小,但仍然没有效果。我从:bPanel.setLayout(newgridlayout(x,y))进行设置,我认为这是问题的主要原因。有人能告诉我如何将

我想更改按钮的大小。我想为每个按钮设置一个方形视图,但不幸的是,它提供了一个矩形视图。只有当我将行数设置为20或25时,我才能获得方形外观。现在,我的GUI如下所示:

我尝试将其从buttons[I][j].setMaximumSize(新维度(20,20))更改为buttons,其中buttons是数组的名称。我也尝试过按钮[I][j]。设置大小,但仍然没有效果。我从:bPanel.setLayout(newgridlayout(x,y))进行设置,我认为这是问题的主要原因。有人能告诉我如何将其设置为布局管理器吗

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

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

import javax.swing.JComboBox;
import javax.swing.event.*;
import javax.swing.JFrame;

import sun.util.calendar.Gregorian;

public class Final_GUI extends JFrame implements ActionListener {
JLabel label;
ButtonGroup cbg;
JRadioButton radio_1;
JRadioButton radio_2;
JRadioButton radio_3;
JCheckBox checkbox_1;
JCheckBox checkbox_2;
JCheckBox checkbox_3;
JScrollPane scrollpane_1;
JComboBox combobox_1;
JList list_1;
JScrollPane sp_list_1;
JComboBox combobox_2;
JButton Orange;
JButton Exit;
JLabel for_text;
int x=200;
int y=100;
int check [][]= new int [x][y];

JFrame frame = new JFrame();
   JButton[][] buttons = new JButton[x][y];
    JPanel mPanel = new JPanel();
    JPanel bPanel = new JPanel();
    JPanel cPanel = new JPanel();
    JTextArea scoreKeeper = new JTextArea();


    int[][] intArray = new int[x][y];

public Final_GUI() {

     butGen();
        score();


    Final_GUILayout customLayout = new Final_GUILayout();

    getContentPane().setFont(new Font("Helvetica", Font.PLAIN, 12));
    getContentPane().setLayout(customLayout);

    label = new JLabel("Shortest Path Finding Algorithm");
    getContentPane().add(label);

    cbg = new ButtonGroup();
    radio_1 = new JRadioButton("radio_1", false);
    cbg.add(radio_1);
    getContentPane().add(radio_1);

    radio_2 = new JRadioButton("radio_2", false);
    cbg.add(radio_2);
    getContentPane().add(radio_2);

    radio_3 = new JRadioButton("radio_3", false);
    cbg.add(radio_3);
    getContentPane().add(radio_3);

    checkbox_1 = new JCheckBox("checkbox_1");

    getContentPane().add(checkbox_1);

    checkbox_2 = new JCheckBox("checkbox_2");
    getContentPane().add(checkbox_2);

    checkbox_3 = new JCheckBox("checkbox_3");
    getContentPane().add(checkbox_3);


    bPanel.setLayout(new GridLayout(x,y));


    mPanel.setLayout(new BorderLayout());

   mPanel.add(bPanel, BorderLayout.CENTER);

   scrollpane_1 = new JScrollPane(mPanel);



    scrollpane_1.setViewportView(mPanel);
    getContentPane().add(scrollpane_1);

    combobox_1 = new JComboBox();
    combobox_1.addItem("Size1");
    combobox_1.addItem("Size2");
    getContentPane().add(combobox_1);

    DefaultListModel listModel_list_1 = new DefaultListModel();
    listModel_list_1.addElement("Black");
    listModel_list_1.addElement("Green");
    list_1 = new JList(listModel_list_1);
    sp_list_1 = new JScrollPane(list_1);
    getContentPane().add(sp_list_1);

    combobox_2 = new JComboBox();
    combobox_2.addItem("Additional Data");
    combobox_2.addItem("Additional Data2");
    getContentPane().add(combobox_2);

    Orange = new JButton("Orange");
    getContentPane().add(Orange);

    Exit = new JButton("Exit");

    getContentPane().add(Exit);

    for_text = new JLabel("Just For some text");
    getContentPane().add(for_text);

    setSize(getPreferredSize());

    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });
}

private void butGen()
{
    for(int i=0;i<x;i++)
    {
        for(int j=0;j<y;j++)
        {
            buttons[i][j] = new JButton(String.valueOf(i)+"x"+String.valueOf(j));
           buttons[i][j].setActionCommand("button" +i +"_" +j);
            buttons[i][j].addActionListener(this);

          buttons[i][j].setMaximumSize(new Dimension( 20, 20));

            bPanel.add(buttons[i][j]);
        }
    }
}   

public void actionPerformed(ActionEvent e)
{

    if(e.getActionCommand().contains("button"))
    {

        String str = e.getActionCommand().replaceAll("button", "");
        System.out.println(str);
        String[] v = str.split("_");
        int i = Integer.parseInt(v[0]);
        int j = Integer.parseInt(v[1]);

        intArray[i][j]++;

        if(check[i][j]!=1){
            buttons[i][j].setBackground(Color.black);
            check[i][j]=1;
        }
        else{
            buttons[i][j].setBackground(null);
            check[i][j]=0;
        }


    System.out.println(e.getActionCommand() +"  " +(i) +"  " +(j));

    score();
    if(checkbox_1.isSelected())
    {
        buttons[i][j].setBackground(Color.GREEN);
    }

}
    }
private void score()
{
    for(int i=0;i<x;i++)
        for(int j=0;j<y;j++)
    buttons[i][j].setText("");

    }

public static void main(String args[]) {
    Final_GUI window = new Final_GUI();

    window.setTitle("SHORTEST PATH FINDING ALGORITHM");
    window.setBackground(Color.black);
   // window.setSize(800, 300);
    window.setResizable(true);
    window.resize(200, 500);
    window.pack();
    window.show();
}
}

GridLayout
为每个组件提供相等的空间,使每个组件填满每个单元格

请查看以了解更多详细信息

如果你想要一个<代码> LayOutMeals,它为你提供了最好的控制,但是它将(除非你告诉它否则)使用组件的首选项/最小/最大大小提示,你应该考虑像<代码> GridBagLayout < /代码>而不是


查看更多详细信息

GridLayout
为每个组件提供相等的空间,使每个组件填满每个单元格

请查看以了解更多详细信息

如果你想要一个<代码> LayOutMeals,它为你提供了最好的控制,但是它将(除非你告诉它否则)使用组件的首选项/最小/最大大小提示,你应该考虑像<代码> GridBagLayout < /代码>而不是


查看更多详细信息

GridLayout
为每个组件提供相等的空间,使每个组件填满每个单元格

请查看以了解更多详细信息

如果你想要一个<代码> LayOutMeals,它为你提供了最好的控制,但是它将(除非你告诉它否则)使用组件的首选项/最小/最大大小提示,你应该考虑像<代码> GridBagLayout < /代码>而不是


查看更多详细信息

GridLayout
为每个组件提供相等的空间,使每个组件填满每个单元格

请查看以了解更多详细信息

如果你想要一个<代码> LayOutMeals,它为你提供了最好的控制,但是它将(除非你告诉它否则)使用组件的首选项/最小/最大大小提示,你应该考虑像<代码> GridBagLayout < /代码>而不是


查看更多详细信息

使用GridBagLayout可以设置内部组件的大小

另一种方法是创建一个从JButton类继承的自定义按钮,并覆盖方法SetSize(int-width,int-height)和设置width=height

您可以这样做:

public class SquareButton extends JButton{

    @Override
    public void setSize(int width, int height) {

        super.setSize(width, width);
    }   
}
这样,您就定义了一个新的
JButton
类,它继承了
JButton
中的所有方法,但是这个SquareButton类的所有实例都具有相同的宽度和高度

也许你不需要这样做,但我认为它是有效的。另一方面,使用这个新类,您无法为按钮设置其他大小关系


另一种方法是在new
SquareButton
类中创建一个新方法来设置所需的大小(或关系宽度/高度),而不是使用GridBagLayout覆盖
SetSize(int-width,int-height)
,您可以设置内部组件的大小

另一种方法是创建一个从JButton类继承的自定义按钮,并覆盖方法SetSize(int-width,int-height)和设置width=height

您可以这样做:

public class SquareButton extends JButton{

    @Override
    public void setSize(int width, int height) {

        super.setSize(width, width);
    }   
}
这样,您就定义了一个新的
JButton
类,它继承了
JButton
中的所有方法,但是这个SquareButton类的所有实例都具有相同的宽度和高度

也许你不需要这样做,但我认为它是有效的。另一方面,使用这个新类,您无法为按钮设置其他大小关系


另一种方法是在new
SquareButton
类中创建一个新方法来设置所需的大小(或关系宽度/高度),而不是使用GridBagLayout覆盖
SetSize(int-width,int-height)
,您可以设置内部组件的大小

另一种方法是创建一个从JButton类继承的自定义按钮,并覆盖方法SetSize(int-width,int-height)和设置width=height

您可以这样做:

public class SquareButton extends JButton{

    @Override
    public void setSize(int width, int height) {

        super.setSize(width, width);
    }   
}
这样,您就定义了一个新的
JButton
类,它继承了
JButton
中的所有方法,但是这个SquareButton类的所有实例都具有相同的宽度和高度

也许你不需要这样做,但我认为它是有效的。另一方面,使用这个新类,您无法为按钮设置其他大小关系


另一种方法是在new
SquareButton
类中创建一个新方法来设置所需的大小(或关系宽度/高度),而不是使用GridBagLayout覆盖
SetSize(int-width,int-height)
,您可以设置内部组件的大小

另一种方法是创建一个从JButton类继承的自定义按钮,并覆盖方法SetSize(int-width,int-height)和设置width=height

您可以这样做:

public class SquareButton extends JButton{

    @Override
    public void setSize(int width, int height) {

        super.setSize(width, width);
    }   
}
这样,您就定义了一个新的
JButton
类,它继承了
JButton
中的所有方法,但是这个SquareButton类的所有实例都具有相同的宽度和高度

也许你不需要这样做,但我认为它是有效的。另一方面,使用这个新类,您无法为您的