将卡布局添加到JFrame Java Swing

时间:2017-04-10 07:43:26

标签: java swing jframe cardlayout

我是Java新手。我想将我的cardlayout添加到JFrame。我想这样做,因为我可以通过关闭JFrame单击退出按钮退出框架(窗口)。以下程序正在扩展JFrame。但我想声明JFrame并在其上添加卡片。我试过但没有发生。另外我想将card1 Jpanel设置为500(宽度),500(高度),但全屏显示。

import javax.swing.*;

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

public class TestCard extends JFrame {
    JButton b;
    JTextArea TA;
    JPanel cards;
    JPanel card1;
    JPanel card2;
    CardLayout card;
    final String card1Text = "One";
    final String card2Text = "Two";

    TestCard() {       
        myCard();       
    }

    public void myCard() {

        card = new CardLayout();
        cards = new JPanel(card);

        card1 = new JPanel(null);    
        card1.setBackground(new Color(255,0,0));
        card1.setPreferredSize(new Dimension(500, 500));

        cards.add(card1, card1Text);

            b = new JButton("Exit");
            b.setBounds(300, 500, 170, 50);
            b.setBackground(Color.yellow);
            card1.add(b);

            TA = new JTextArea();    
            TA.setBounds(300,200,500,250);
            TA.setBackground(Color.white);
            card1.add(TA);


            add(cards);

            card.show(cards, card1Text);

            b.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                }
            });
    }

    public static void main(String[] args) {
        TestCard cardLayoutTest = new TestCard();
        cardLayoutTest.setVisible(true);       
        cardLayoutTest.setExtendedState(JFrame.MAXIMIZED_BOTH);    
    }

}

1 个答案:

答案 0 :(得分:1)

  

但是我想声明JFrame并在其上添加卡

您需要了解的关于JFrame的第一件事是多层次的......

JFrame layers

当您向JFrame添加组件时,它们(通常)会添加到contentPane。请参阅How to Use Root Panes

因此,这是您需要克服的第一个问题,在引用CardLayout中的容器时,您不应该依赖JFrame的实例,而是使用JFrame#getContentPane < / p>

好的,首先删除extends JFrame,然后创建一个JFrame的实例...

import java.awt.CardLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestCard {

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

    public TestCard() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

好的,所以,这不是很令人兴奋,我们需要在它上面展示一些东西......

public class ACardPane extends JPanel {

    private JButton btn;
    private JTextArea textArea;

    public ACardPane() {
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridwidth = GridBagConstraints.REMAINDER;

        btn = new JButton("Exit");
        add(btn, gbc);

        gbc.weightx = 1;
        gbc.weighty = 1;
        gbc.fill = GridBagConstraints.BOTH;
        textArea = new JTextArea(20, 40);
        add(new JScrollPane(textArea), gbc);
    }

}

好的,现在我们需要将其实际添加到框架中....

我们需要将起始代码修改为类似的内容......

JFrame frame = new JFrame("Testing");

CardLayout cardLayout = new CardLayout();
frame.setLayout(cardLayout);
frame.add(new ACardPane(), "One");
cardLayout.show(frame.getContentPane(), "One");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);

frame.setVisible(true);

这将创建一个最大化的框架,文本区域占据尽可能多的空间。

如果您不想最大化,请将frame.setExtendedState(JFrame.MAXIMIZED_BOTH)更改为frame.pack()而不是

我建议看看