2014-02-18 38 views
0

我已经在这里很长一段时间了,但我似乎无法得到它。我试图生成一个JPanel,它具有上面的JTextArea和下面的两个JLabel,但是我的JLabel最终在我的JTextArea的左侧,而我不能让其他的出现。JPanels:一个带有JTextArea,另一个带有JLabel

这里是我的代码(对不起显示stuff-只是填料真的):

public JPanel contentPane() { 
    JPanel something = new JPanel(); 

    String information = "Please"; 

    info = new JTextArea(information, 4, 30); 
    info.setEditable(false); 
    info.setLineWrap(true); 
    info.setWrapStyleWord(true); 

    JPanel one = new JPanel(new BorderLayout()); 
    one.setBackground(Color.WHITE); 
    one.setLocation(10, 10); 
    one.setSize(50, 50); 
    one.add(info, BorderLayout.CENTER); 
    something.add(one, BorderLayout.NORTH); 

    JPanel two = new JPanel(new BorderLayout()); 
    two.setBackground(null); 
    two.setLocation(220, 10); 
    two.setSize(50, 50); 
    two.add(new JLabel("Please work"), BorderLayout.EAST); 
    two.add(new JLabel("Oh gosh, please"), BorderLayout.WEST); 
    something.add(two, BorderLayout.SOUTH); 

    something.setOpaque(true); 
    return something; 
} 

public static void GUI() { 
    JFrame frame = new JFrame("You Guessed It!"); 

    DisplayStudent panel = new DisplayStudent(); 
    frame.setContentPane(panel.contentPane()); 

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setSize(400, 150); 
    frame.setVisible(true); 
} 

请和谢谢你的人谁需要时间来帮助。

+0

您正在使用哪个版本的JDK? – masterX244

+0

@ masterX244 7.4我相信。 – Zanpo

回答

6

当你创建你东西,你不指定任何布局管理器,但后来尝试添加一个东西使用BorderLayout的常数 - 这是行不通的,因为默认布局管理器JPanel是FlowLayout。

试试这个,而不是;

JPanel something = new JPanel(new BorderLayout()); 
+0

或者,你可以使用GridLayout(),它可以让你在网格中放置任何元素:http://docs.oracle.com/javase/tutorial/uiswing/layout/grid.html – sparks

+0

GridLayout只要您希望面板中的所有组件具有完全相同的大小。当你开始混合大小和想要有不同的动态(例如,一些可能水平缩放,其他垂直,其他垂直,锚定北部,一些应该拉伸,等等等等),你需要改变GridBagLayout这是迄今为止最“通用的“我使用的布局管理器。 –

+0

@Markus Millfjord它的工作,但现在我的JLabels出现在中心,而不是在东部和西部恭敬地。我还有什么遗漏? – Zanpo

相关问题