运行Java Swing程序时,显示区域不会显示

时间:2015-10-11 08:30:30

标签: java eclipse

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.sampleforstack.MainActivity" >

    <!--This is your main layout. Add your content here -->
    <LinearLayout 
        android:id="@+id/lytMain"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/accent_material_dark">

    </LinearLayout>
    <!--This is your menu layout. Add smile buttons here -->
    <LinerLayout
        android:id="@+id/lytMenu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone">
    </LinerLayout>

</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

包含cp的组件displayArea永远不会添加到任何内容

public Ch5PA1() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    createIcon();
    createLabel();
    createTextArea();
    createCombo();
    createButton();
    createPanels();

    JComponent cp = (JComponent) getContentPane();
    cp.setLayout(new BorderLayout(0, 40));
    cp.setBorder(new EmptyBorder(20, 20, 20, 20));

    cp.add(p1, BorderLayout.WEST);
    cp.add(new JScrollPane(displayArea), BorderLayout.CENTER);
    cp.add(p2, BorderLayout.CENTER);
    // Not added to anything?
}

答案 1 :(得分:0)

原因

  • 两个项目已添加到BorderLayout.CENTER内容窗格的JFrame位置。一个是JScrollPane displayArea,另一个是p2
  • p2已设置为在方法displayArea中添加createPanels

解决方案

删除行cp.add(new JScrollPane(displayArea), BorderLayout.CENTER);或如果确实不需要p2,请删除p2引用并删除语句cp.add(p2, BorderLayout.CENTER);

输出

enter image description here

相关问题