开发者

BoxLayout program not working

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class File
{
 private JFrame frame1;
 private JPane开发者_StackOverflow中文版l panel1;
 private JPanel panel2;
 private JLabel labelWeight;
 private JLabel labelHeight;




    File()
    {
     frame1 = new JFrame();
     panel1 = new JPanel();
     panel2 = new JPanel();
     labelWeight = new JLabel("Weight :");
     labelHeight = new JLabel("Height :");


    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

     panel1.setLayout(new FlowLayout());
     panel1.add(labelWeight);

     panel2.setLayout(new FlowLayout());
     panel2.add(labelHeight);

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

     frame1.setLayout(new BoxLayout(frame1,BoxLayout.X_AXIS));
     panel1.setAlignmentY(0);
     panel2.setAlignmentY(0);

     frame1.add(panel1);
     frame1.add(panel2);


     frame1.setSize(400, 200);
     frame1.setDefaultCloseOperation(frame1.EXIT_ON_CLOSE);
     frame1.setVisible(true);
    }


    public static void main (String args[])
    {
     new File();
    }

}   

It gives BoxLayout Sharing error at runtime


Generally, LayoutManagers are set on a JPanel. I guess JFrame implements this method to forward it to the content pane of the frame. I would suggest you try:

Container contentPane = frame1.getContentPane();
contentPane.setLayout(new BoxLayout(contentPane,BoxLayout.X_AXIS)); 

If you still have problems take a look at the Swing tutorial on How to Use Box Layout for working examples.


Swing components should be created in the Event Dispatch Thread. Try this in your main():

javax.swing.SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        new File();
    }
});

But your problem may be the same as this question.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜