简体   繁体   English

如何向JPanel添加方法?

[英]How to add a method to a JPanel?

I working on this assignment and I have a problem where I need to add this matrix to a JPanel. 我正在处理此分配,但是我有一个问题需要将这个矩阵添加到JPanel。 I made the matrix in another method because I thought it would be easier but can't find a solution. 我用另一种方法制作了矩阵,因为我认为这样会更容易,但是找不到解决方案。 I also used this layout by following a tutorial on Oracles website if it look familiar. 如果看起来很熟悉,我也会通过遵循Oracle网站上的教程来使用此布局。

The text also needs to be editable which is why I have buttons. 文本也需要可编辑,这就是为什么我有按钮。 I don't have there functions here either. 我这里也没有功能。

public static void addComponentsToPane(Container pane) {

    if (RIGHT_TO_LEFT) {
        pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
    }

    JPanel array;
    JButton button;
    pane.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();

    if (shouldFill) {
        c.fill = GridBagConstraints.HORIZONTAL;
    }

    button = new JButton("Reset to 0");
    pane.add(button, c);

    // WHERE IT NEEDS TO BE ADDED***********************
    array = new JPanel();
    pane.add(array, c);

}

private static void createAndShowGUI() {
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    addComponentsToPane(frame.getContentPane());
    frame.pack();
    frame.setVisible(true);
}

public int[][] getRandomMatrix() {
int[][] randomMatrix = new int[10][10];
    for (int r = 0; r < randomMatrix.length; r++) {
        for (int c = 0; c < randomMatrix[r].length; c++) {
        randomMatrix[r][c] = (int)(Math.random() * 2);
        }
    }
    return randomMatrix;
}

public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}

From the question you outline, it looks like you want to add an additional method to JPanel. 从您概述的问题来看,您似乎想向JPanel添加其他方法。

Ok, that would require you to create a custom instance of a JPanel like this: 好的,这将需要您创建一个JPanel的自定义实例,如下所示:

public class MyPanel extends JPanel {

    public MyPanel() {
        super();   
    }


    public int[][] getRandomMatrix() {
         int[][] randomMatrix = new int[10][10];
         for (int r = 0; r < randomMatrix.length; r++) {
             for (int c = 0; c < randomMatrix[r].length; c++) {
             randomMatrix[r][c] = (int)(Math.random() * 2);
         }
     }
    return randomMatrix;
   }
}

And instead of adding a pre-rolled JPanel to the frame, you would add an instance of 'MyPanel' to the frame instead. 而且,除了向框架添加预滚动的JPanel之外,您还可以向框架添加“ MyPanel”的实例。

public static void addComponentsToPane(Container pane) {

if (RIGHT_TO_LEFT) {
    pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}

MyPanel array;
JButton button;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();

if (shouldFill) {
    c.fill = GridBagConstraints.HORIZONTAL;
}

button = new JButton("Reset to 0");
pane.add(button, c);

array = new MyPanel();
pane.add(myPanel, c);

}

private static void createAndShowGUI() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}



public static void main(String[] args) {
   javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM