簡體   English   中英

無法將JFileChooser對齊到BorderLayout.NORTH的左側

[英]Can't align JFileChooser to the left of BorderLayout.NORTH

我有一個帶有JFileChooser和JTextArea的窗口。 JFileChooser在BorderLayout的北部。 JTextArea在BorderLayout的CENTER部分中。

我想將所有JFileChooser都向左對齊,但是它不會像我想要的那樣移動並保持居中狀態。 此外,我希望JFileChooser占用整個窗口的時間。

編輯

這是主要代碼

public class MainServer 
{
    public static void main(String[] args)
    {
        ServerBoard frame=new ServerBoard(1000, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
     }
}

這是窗口代碼

public class ServerBoard extends JFrame
{
    private JButton startserver;
    private JButton senddata;   
    private JButton sendgps;
    private JTextArea messagearea;

    public ServerBoard(int l, int h)
    {
        super("ServerBoard");
        this.initialize();
        this.setSize(l,h);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    public void initialize()
    {
        // Define a panel
        Container c=this.getContentPane();

        this.messagearea=new JTextArea(40,60);      

        c.add(this.createNorth(), BorderLayout.NORTH);
        c.add(messagearea, BorderLayout.CENTER);
    }

    public JPanel createNorth()
    {
        JPanel panelnorth=new JPanel();

        JToolBar toolbarnorth=new JToolBar();
        panelnorth.add(toolbarnorth);

        this.startserver=new JButton("START SERVER");
        startserver.addActionListener(new ServerBoardListener());
        toolbarnorth.add(startserver);

        this.senddata=new JButton("SEND DATA");
        senddata.addActionListener(new ServerBoardListener());
        toolbarnorth.add(senddata);

        this.sendgps=new JButton("SEND GPS FRAME");
        sendgps.addActionListener(new ServerBoardListener());
        toolbarnorth.add(sendgps);

        return panelnorth;  
    }
}

這是我的窗戶

在此處輸入圖片說明

我真的很想使用這個JFileChooser。 你能幫我嗎 ?

預先非常感謝您的回答。

只需嵌套您的JPanels。 創建一個使用BorderLayout的新JPanel(稱為northPanel),然后將其添加到BorderLayout.NORTH位置的主窗口中,然后將JFIleChooser添加到此BorderPanel JPanel的BorderLayout.WEST位置中。

選項2:為northPanel提供一個沿線軸定向的BoxLayout,添加JFileChooser,然后添加膠水。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM