所以我一直在寻找其他解决方案来解决这个问题,但似乎他们都没有帮助。有人能帮我吗?java - 在位置设置按钮和TextAreas
代码:
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Container cont = frame.getContentPane();
JPanel buttonpanel = new JPanel();
JButton[] button = new JButton[12];
JTextArea score1, score2;
score1 = new JTextArea(100, 200);
score2 = new JTextArea(100, 200);
frame.setSize(800, 600);
frame.setVisible(true);
score1.setLayout(null);
score1.setBackground(Color.BLUE);
score2.setLayout(null);
score2.setBackground(Color.RED);
score1.setLocation(0, 100);
score2.setLocation(700, 100);
frame.add(score1);
for (int i = 0; i < button.length/2; i++) {
button[i].setBounds(100 * (i + 1), 100, 100, 100);
button[i].setBackground(Color.GRAY);
buttonpanel.add(button[i]);
}
frame.add(buttonpanel);
frame.add(score2);
// frame.add(panel);
}
这给了我完全窗口蓝色。
此外,如果您尝试配置GUI,请显示一张图片,显示您想要实现的内容。 99%的时间,最好的解决方案是不要像你这样做。不要使用绝对定位,因为在99%的时间内制作GUI是一种可怕的方式。 –
虽然null布局和'setBounds()'对于Swing新手来说似乎是创建复杂GUI的最简单和最好的方式,但是更多的Swing GUI会创建使用它们时遇到的更严重的困难。它们不会在GUI大小调整时调整组件的大小,它们是增强或维护的皇室女巫,当它们放在滚动窗格中时它们会完全失败,在所有平台或屏幕分辨率与原始视图不同时,它们看起来会非常糟糕。 –