Java 在CardLayout中设置JTextFields和JButtons位置

Java 在CardLayout中设置JTextFields和JButtons位置,java,swing,layout,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";

这是我找到的一个关于卡片布局的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)但它不工作。有什么建议吗

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

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

使用不同的布局管理器可以实现垂直对齐。您可以对所有组件使用
GridBagLayout
,或者使用
GridBagLayout
,或者使用不同的布局管理器嵌套
JPanel
。可能性是无穷的。这取决于你想要什么样的外观

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

此外,除了仅定位组件外,布局管理器还通过尊重首选组件或不尊重它们来使用动态大小调整。您可以在中看到一些布局管理器的图片,它们尊重和不尊重首选尺寸

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);
            }
        });
    }
}


你到底想让它们如何分布?像
Jlabel-JTextField这样的行,然后是新行,页面末尾的按钮谢谢你的回答!你的意思是,我可以在我的多个窗口中使用你代码的第一部分(不在主窗口中),同时设置swingUtilities。。。就在右边一次?差不多。如果你想要这个精确的布局,我的构造器中的所有东西都可以放到你的构造器中。我修改了你的代码,看起来很有效!非常感谢您抽出时间:)最后一个问题!在void run()中,我似乎必须为我要创建的任何窗口设置
frame.add(new myConstructor())
,对吗?不,您的
createAndShowGUI
已经初始化了所有内容。你可以保持一切不变。