在CardLayout中设置JTextFields和JButtons位置

时间:2014-02-24 14:13:26

标签: java swing layout

这是我发现的关于卡片布局

的java模板
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class Main {

    private static final String CARD_JBUTTON =  "Card JButton";
    private static final String CARD_JTEXTFIELD = "Card JTextField";    
    private static final String CARD_JRADIOBUTTON = "Card JRadioButton";

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("Card Layout Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);

        // This JPanel is the base for CardLayout for other JPanels.
        final JPanel contentPane = new JPanel();
        contentPane.setLayout(new CardLayout(200, 200));

        /* Here we be making objects of the Window Series classes
         * so that, each one of them can be added to the JPanel 
         * having CardLayout. 
         */
        Window1 win1 = new Window1();
        contentPane.add(win1, CARD_JBUTTON);
        Window2 win2 = new Window2();
        contentPane.add(win2, CARD_JTEXTFIELD);
        Window3 win3 = new Window3();
        contentPane.add(win3, CARD_JRADIOBUTTON);

        /* We need two JButtons to go to the next Card
         * or come back to the previous Card, as and when
         * desired by the User.
         */
        JPanel buttonPanel = new JPanel(); 
        final JButton previousButton = new JButton("PREVIOUS");
        previousButton.setBackground(Color.BLACK);
        previousButton.setForeground(Color.WHITE);
        final JButton nextButton = new JButton("NEXT");
        nextButton.setBackground(Color.RED);
        nextButton.setForeground(Color.WHITE);

        buttonPanel.add(previousButton);
        buttonPanel.add(nextButton);

        /* Adding the ActionListeners to the JButton,
         * so that the user can see the next Card or
         * come back to the previous Card, as desired.
         */
        previousButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {   
                CardLayout cardLayout = (CardLayout) contentPane.getLayout();
                cardLayout.previous(contentPane);
            }
        });
        nextButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                CardLayout cardLayout = (CardLayout) contentPane.getLayout();
                cardLayout.next(contentPane);   
            }
        });

        // Adding the contentPane (JPanel) and buttonPanel to JFrame.
        frame.add(contentPane, BorderLayout.CENTER);
        frame.add(buttonPanel, BorderLayout.PAGE_END);

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
    }

这是我的Window1.java

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;



class Window1 extends JPanel
{
    /*
     * Here this is our first Card of CardLayout, which will
     * be added to the contentPane object of JPanel, which
     * has the LayoutManager set to CardLayout.
     * This card consists of Two JButtons.
     */  
    private ActionListener action;

    public Window1() 
    {
        init();
    }

    private void init() 
    {
        final JButton clickButton = new JButton("Click ME");
        final JButton dontClickButton = new JButton("DON\'T CLICK ME");     

        final JTextField title = new JTextField(12);

        action = new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                if (ae.getSource() == clickButton)
                {
                    String myString = title.getText();
                    System.out.println(myString);
                }
                else if (ae.getSource() == dontClickButton)
                {
                    JOptionPane.showMessageDialog(null, "I told you not to click me!"
                                                        , "Wrong Button", JOptionPane.PLAIN_MESSAGE);
                }
            }
        };

        clickButton.addActionListener(action);
        dontClickButton.addActionListener(action);

        add(clickButton);
        add(dontClickButton);
        add(title);

    }
}

现在我的问题是如何在Window1中设置文本字段和按钮的位置?

使用此代码,它们被设置在视图水平对齐的视图中心。

我尝试使用title.setLocation(5,5);,但它不起作用。有什么建议吗?

1 个答案:

答案 0 :(得分:2)

  

现在我的问题是如何在Window1中设置文本字段和按钮的位置?   行像Jlabel - JTextField然后新行,并在页面的末尾按钮

问题是你没有使用任何布局管理器。 JPanel的默认布局管理器为FlowLayout,它将完全符合您的体验(组件的水平布局)。

通过使用不同的布局管理器可以实现垂直对齐。您可以使用GridBagLayout all 组件或GridLayout,或者可以将JPanel嵌套到不同的布局管理器中。可能性是无止境。它只是归结为你想要的确切外观。

请参阅Laying out Components Within a Container以了解如何使用不同的布局管理器。我会给你一个例子,但不要让它阻止你看这些教程。您需要来学习它们。

除了组件布局管理器的定位之外,通过尊重组件的优选组件来使用动态大小调整并不尊重它们。您可以在某些布局管理器的this answer中看到一张图片,该图片包含但不尊重首选尺寸。

enter image description here

import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class LayoutManagers extends JPanel{

    public LayoutManagers() {
        JLabel label = new JLabel("Text Field");
        JTextField textField = new JTextField(20);
        JRadioButton rb1 = new JRadioButton("Radio 1");
        JRadioButton rb2 = new JRadioButton("Radio 2");
        JButton button = new JButton("Button");

        JPanel panel1 = new JPanel();
        panel1.add(label);
        panel1.add(textField);

        JPanel panel2 = new JPanel();
        panel2.add(rb1);
        panel2.add(rb2);

        JPanel panel3 = new JPanel(new FlowLayout(FlowLayout.TRAILING));
        panel3.add(button);

        JPanel panel4 = new JPanel(new GridLayout(3, 1));
        panel4.add(panel1);
        panel4.add(panel2);
        panel4.add(panel3);

        setLayout(new GridBagLayout());
        add(panel4);     
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new LayoutManagers());
                frame.pack();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLocationByPlatform(true);
                frame.setVisible(true);
            }
        });
    }
}