2015-04-08 100 views
1

所以我一直在寻找其他解决方案来解决这个问题,但似乎他们都没有帮助。有人能帮我吗?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); 
} 

这给了我完全窗口蓝色。

+1

此外,如果您尝试配置GUI,请显示一张图片,显示您想要实现的内容。 99%的时间,最好的解决方案是不要像你这样做。不要使用绝对定位,因为在99%的时间内制作GUI是一种可怕的方式。 –

+0

虽然null布局和'setBounds()'对于Swing新手来说似乎是创建复杂GUI的最简单和最好的方式,但是更多的Swing GUI会创建使用它们时遇到的更严重的困难。它们不会在GUI大小调整时调整组件的大小,它们是增强或维护的皇室女巫,当它们放在滚动窗格中时它们会完全失败,在所有平台或屏幕分辨率与原始视图不同时,它们看起来会非常糟糕。 –

回答

3

您看到全部为蓝色,因为您正在向JFrame的contentPane添加score1,一个全蓝色的JTextArea,一个使用BorderLayout的容器,这使得JTextArea填充contentPane,只剩下蓝色。由于使用了一个填充了空值的JButton数组而导致的NullPointerException,因此不会添加任何其他内容。一旦你解决了这个问题(你从来没有告诉过我们这个问题),由于同样的问题,你只会看到红色。

您无疑已阅读的最佳解决方案是使用布局管理器来创建GUI的最佳优势。虽然null布局和setBounds()对于Swing新手来说似乎是创建复杂GUI的最简单和最好的方法,但更多的Swing GUI会创建使用它们时遇到的更严重的困难。它们不会在GUI大小调整时调整组件的大小,它们是增强或维护的皇室女巫,当它们放在滚动窗格中时它们会完全失败,在所有平台或屏幕分辨率与原始视图不同时,它们看起来会非常糟糕。

其他问题:

  • 你对你的JFrame 调用setVisible(true)前添加任何东西它是倒退。您想在之后添加所有内容,使其在之后生效,以便它在显示时显示在JFrame中。
  • 您正在设置JTextArea的布局,因为您永远不想将这些布局用作其他组件的容器。
  • 也明白,这不是做你认为它在做什么:JTextArea(100, 200);。你没有创建一个100×200像素的JTextArea,而是100个×200 这是一个令人惊讶的巨大的 JTextArea。在使用不熟悉的组件时,您将需要阅读文档和API。
  • 您声明,"So I've been looking at other solutions to this problem, but it seems that none of them help at all."可能“其他解决方案”建议您使用布局管理器,而不是说他们根本没有“帮助”,您应该努力学习如何使用这些工具,因为其他人解决方案正确。
  • 是的,你正在尝试在尚未创建的JButton数组中使用JButton。理解一个引用类型的数组,例如JButton数组最初只填充空引用,类似于空的纸箱蛋。就像在你把鸡蛋放进去之前你不能使用纸盒中的任何鸡蛋一样,你不能在你的鸡蛋中放入JButtons,在for循环中,你通过数组迭代,在第一行创建的每个JButton的项目:button[i] = new JButton("Something");

你可以找到链接到秋千教程和这里其他Swing资源:Swing Info

例如,你能告诉我哪一个更容易调试,你的代码,或此代码:

import java.awt.BorderLayout; 
import java.awt.GridLayout; 
import javax.swing.*; 

public class Foo2 extends JPanel { 
    private static final String[] BUTTON_TEXTS = { "A", "B", "C", "D", "E", "F", 
     "G", "H", "I", "J", "K", "l" }; 
    private static final int GAP = 3; 
    private JTextArea textArea1 = new JTextArea(20, 30); 
    private JTextArea textArea2 = new JTextArea(20, 30); 

    public Foo2() { 
     JPanel textAreaGrid = new JPanel(new GridLayout(1, 0, GAP, GAP)); // gridlayout 1 row 
     textAreaGrid.add(new JScrollPane(textArea1)); 
     textAreaGrid.add(new JScrollPane(textArea2)); 

     JPanel buttonPanel = new JPanel(new GridLayout(2, 0, GAP, GAP)); // 2 rows 
     for (String btnText : BUTTON_TEXTS) { 
     buttonPanel.add(new JButton(btnText)); 
     } 

     setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP)); 
     setLayout(new BorderLayout(GAP, GAP)); // main GUI uses border layout 
     add(textAreaGrid, BorderLayout.CENTER); 
     add(buttonPanel, BorderLayout.PAGE_END); 
    } 

    private static void createAndShowGui() { 
     Foo2 mainPanel = new Foo2(); 

     JFrame frame = new JFrame("Foo2"); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     frame.getContentPane().add(mainPanel); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGui(); 
     } 
     }); 
    } 
} 

其中为显示:

enter image description here

+0

对不起,我对Java还不是很了解。这就像我第二次和我现在的第一次3年。所以我可以理解我的错误,并去解决很多事情(谢谢你)。所以我在我想让窗口类似于游戏“Mancala”的那个部分。它不是使用坑,而是按钮。我有侧坑或目标显示出来,但按钮没有显示在文本框之间。请提示? – Xephos

+0

Nvmd,我设法不使用SetLayout(null),因为它以某种方式阻止我组织?无论如何,现在如何把按钮和文本区域放在另一个textarea下面? – Xephos

+0

我修好了!谢谢你的帮助! – Xephos