简体   繁体   English

为什么不显示此JTextArea?

[英]Why isn't this JTextArea displaying?

I know this isn't the place to be asking questions like this but I truly can not find any solutions to my problems and need some help. 我知道这里不是问这样的问题的地方,但我确实找不到解决我问题的方法,需要帮助。 Everything should be working fine from what I understand but my 10x10 matrix that I have created doesn't display, or at least I can't see it. 根据我的理解,一切都应该可以正常工作,但是我创建的10x10矩阵无法显示,或者至少看不到。 Let me know if there is some where to look for a solution or if you have one for yourself. 让我知道是否有哪里可以找到解决方案,或者您是否有自己的解决方案。

I'm looking to have 3 buttons on the top, the main content (matrix) in the middle, and finally the quit button on the bottom. 我希望顶部具有3个按钮,中间具有主要内容(矩阵),底部具有退出按钮。 I'm following a layout/tutorial that I found online from oracle so this could be my problem. 我正在遵循从oracle在线找到的布局/教程,所以这可能是我的问题。 I also understand some of my code is sloppy I've been moving pieces back and forth trying to figure things out. 我也理解我的一些代码很草率,我一直在来回移动试图解决问题。

Thanks in advance. 提前致谢。

layouts.java 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 MyPanel.java

package layouts;

import javax.swing.JPanel;

@SuppressWarnings("serial")
public class MyPanel extends JPanel {

public MyPanel() {
    super();   
}

}

您需要将TextArea添加到基础面板。

array.add(area);

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

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