如何向 JPanel 添加方法?
How to add a method to a JPanel?
我正在处理这项作业,但遇到问题需要将此矩阵添加到 JPanel。我用另一种方法制作了矩阵,因为我认为它会更容易但找不到解决方案。如果看起来很熟悉,我还按照 Oracle 网站上的教程使用了这种布局。
文本也需要是可编辑的,这就是为什么我有按钮。我这里也没有这个功能
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();
}
});
}
从您提出的问题来看,您似乎想要向 JPanel 添加一个额外的方法。
好的,这需要您像这样创建一个 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;
}
}
而不是将预滚动的 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();
}
});
}
我正在处理这项作业,但遇到问题需要将此矩阵添加到 JPanel。我用另一种方法制作了矩阵,因为我认为它会更容易但找不到解决方案。如果看起来很熟悉,我还按照 Oracle 网站上的教程使用了这种布局。
文本也需要是可编辑的,这就是为什么我有按钮。我这里也没有这个功能
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();
}
});
}
从您提出的问题来看,您似乎想要向 JPanel 添加一个额外的方法。
好的,这需要您像这样创建一个 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;
}
}
而不是将预滚动的 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();
}
});
}