如何将 Jlabel 和 Jtext 放在同一框架中?
如果我最后添加文本,则仅显示文本,这是我的代码:
public MatrixFrame(String framname, int width, int height) {
width =7;
height = 6;
JFrame fram = new JFrame(framname);
fram.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fram.setSize(500,500);
JTextArea text = new JTextArea("Here come Text");
valMatrixPanel = new ValMatrixPanel(height,width,Color.GRAY, Color.black);
JPanel pan = valMatrixPanel.getPan(); // pan is 6*7 panels lock the picture
fram.add(pan);
fram.add(text);
fram.setVisible(true);
}
}
最佳答案
解决这个问题的关键是理解如何使用布局管理器,因为这就是 Swing 决定在哪里放置内容以及如何调整内容大小的方式。首先,为了快速简单地修复,将所有组件放入 JPanel 中,默认情况下使用 FlowLayout,然后将 JPanel 添加到 JFrame 中。 不要设置 JPanel 或 JFrame 的大小,添加所有内容后在 JFrame 上调用 pack()
,然后最后调用 setVisible(true)
。
更好的长期答案:阅读您可以在其他 Swing 教程中找到的布局管理器教程: here 。
关于java - 将面板和文本框放在同一个框架中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27584069/