我知道这不是问这样的问题的地方,但我真的找不到任何问题的解决方案,需要一些帮助。根据我的理解,一切都应该正常工作,但是我创建的10x10矩阵没有显示,或者至少我看不到它。如果有什么地方可以寻找解决方案或者您自己有解决方案,请告诉我。
我希望顶部有3个按钮,中间有主要内容(矩阵),最后是底部的退出按钮。我正在关注我在oracle上发现的布局/教程,所以这可能是我的问题。我也理解我的一些代码很草率我一直在动作来回试图解决问题。
提前致谢。
layouts.java
public class layouts {
final static boolean shouldFill = true;
final static boolean shouldWeightX = true;
final static boolean RIGHT_TO_LEFT = false;
static JTextArea area = new JTextArea();
public int value;
public static int setter;
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);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
setter = 0;
System.out.println("set to zero");
}
});
button = new JButton("Reset to 1");
pane.add(button, c);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
setter = 1;
System.out.println("set to one");
}
});
button = new JButton("Reset All");
pane.add(button, c);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
setter = 2;
System.out.println("set to random");
}
});
array = new MyPanel();
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 40;
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 1;
pane.add(array, c);
button = new JButton("Quit");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 0;
c.weighty = 1.0;
c.anchor = GridBagConstraints.PAGE_END;
c.insets = new Insets(10,0,0,0);
c.gridx = 1;
c.gridwidth = 2;
c.gridy = 2;
pane.add(button, c);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
}
public int[][] getRandomMatrix() {
if(setter == 0)
value = 0;
if(setter == 1)
value = 1;
else
value = (int)(Math.random() * 2);
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] = value;
}
}
return randomMatrix;
}
private static void createAndShowGUI() {
// basic functions
JFrame frame = new JFrame("Module 5 Assignment 1");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// content pane
addComponentsToPane(frame.getContentPane());
// displays the window
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
MyPanel.java
package layouts;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class MyPanel extends JPanel {
public MyPanel() {
super();
}
}
答案 0 :(得分:0)
您需要将TextArea添加到基础面板。
array.add(area);