摇摆;我如何以这种格式定位组件?

时间:2014-03-16 21:38:28

标签: java swing

所以,我希望我的项目以下面的格式定位;我对定位确实不太自信,但我想更多地学习它。以下是代码:

package com.bleh.harry;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class Main 
{
    private JMenuBar menuBar;
    private JMenu fileMenu, windowMenu, helpMenu;
    private JMenuItem fileNew, fileOpen, fileSave, windowTheme, windowLayout, windowProperties, helpWelcome, helpHelp, helpAbout;
    private JTextArea mainTextArea;

    public 

    Main()
    {
        JPanel mainCard = new JPanel(new BorderLayout(8,8));
        JPanel mainTop = new JPanel(new FlowLayout(FlowLayout.CENTER));    
        menuBar = new JMenuBar();
        fileMenu = new JMenu("File");
        windowMenu = new JMenu("Window");
        helpMenu = new JMenu("Help");
        menuBar.add(fileMenu);
        menuBar.add(windowMenu);
        menuBar.add(helpMenu);

        final CardLayout layout = new CardLayout();                                           //ADDS CARDS TO CONTAINER
        final JPanel cards = new JPanel(layout);
        cards.add(mainCard, "2");

        mainCard.add(mainTop, BorderLayout.NORTH); 

        JFrame window = new JFrame("Pseudo code text editor");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.getContentPane().add(cards);
        window.setSize(1280, 720);
        window.setLocationRelativeTo(null);
        window.setVisible(true);
    }

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

enter image description here

2 个答案:

答案 0 :(得分:1)

要将组件放在一起,我建议使用BoxLayoutBoxLayout采用方向参数并相应地放置组件。最常用的两个选项是X_AXISY_AXISX_AXIS从左到右排列,Y_AXIS从上到下排列。你想要X_AXIS

要使用名为BoxLayout的{​​{1}}实例使用JFrame设置布局,请执行:

window

答案 1 :(得分:1)

您可以使用BorderLayout ...

BorderLayout

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class LayoutTest {

    private JMenuBar menuBar;
    private JMenu fileMenu, windowMenu, helpMenu;
    private JMenuItem fileNew, fileOpen, fileSave, windowTheme, windowLayout, windowProperties, helpWelcome, helpHelp, helpAbout;
    private JTextArea mainTextArea;

    public LayoutTest() {
        JPanel mainCard = new JPanel(new BorderLayout(8, 8));
        menuBar = new JMenuBar();
        fileMenu = new JMenu("File");
        windowMenu = new JMenu("Window");
        helpMenu = new JMenu("Help");
        menuBar.add(fileMenu);
        menuBar.add(windowMenu);
        menuBar.add(helpMenu);

        final CardLayout layout = new CardLayout(); 
        final JPanel cards = new JPanel(layout);
        cards.add(mainCard, "2");

        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.add("One", createPane());
        tabbedPane.add("Two", createPane());
        tabbedPane.add("Three", createPane());
        tabbedPane.add("Four", createPane());

        mainTextArea = new JTextArea(20, 40);

        mainCard.add(tabbedPane, BorderLayout.WEST);
        mainCard.add(new JScrollPane(mainTextArea));

        JFrame window = new JFrame("Pseudo code text editor");
        window.setJMenuBar(menuBar);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.getContentPane().add(cards);
        window.pack();
        window.setLocationRelativeTo(null);
        window.setVisible(true);
    }

    protected JPanel createPane() {

        return new JPanel() {

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 200);
            }

        };

    }

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

这里的问题是JTabbedPane想要的空间量取决于它的内容......

您甚至可以尝试使用GridBagLayout,这可能会让您获得更多控制权......

GridBagLayout

import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class LayoutTest {

    private JMenuBar menuBar;
    private JMenu fileMenu, windowMenu, helpMenu;
    private JMenuItem fileNew, fileOpen, fileSave, windowTheme, windowLayout, windowProperties, helpWelcome, helpHelp, helpAbout;
    private JTextArea mainTextArea;

    public LayoutTest() {
        JPanel mainCard = new JPanel(new GridBagLayout());
        menuBar = new JMenuBar();
        fileMenu = new JMenu("File");
        windowMenu = new JMenu("Window");
        helpMenu = new JMenu("Help");
        menuBar.add(fileMenu);
        menuBar.add(windowMenu);
        menuBar.add(helpMenu);

        final CardLayout layout = new CardLayout();                                           //ADDS CARDS TO CONTAINER
        final JPanel cards = new JPanel(layout);
        cards.add(mainCard, "2");

        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.add("One", createPane());
        tabbedPane.add("Two", createPane());
        tabbedPane.add("Three", createPane());
        tabbedPane.add("Four", createPane());

        mainTextArea = new JTextArea(20, 40);

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.weightx = 0.25;
        gbc.weighty = 1;

        mainCard.add(tabbedPane, gbc);
        gbc.weightx = 0.75;
        mainCard.add(new JScrollPane(mainTextArea), gbc);

        JFrame window = new JFrame("Pseudo code text editor");
        window.setJMenuBar(menuBar);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.getContentPane().add(cards);
        window.pack();
        window.setLocationRelativeTo(null);
        window.setVisible(true);
    }

    protected JPanel createPane() {

        return new JPanel() {

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 200);
            }

        };

    }

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

请记住,JMenuBar属于窗口;)