在 gui 中更改面板 [英] Changing panels in gui

查看:18
本文介绍了在 gui 中更改面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是每次按下按钮时更改 GUI 的右侧.第一个按钮显示一个 JLabel,第二个按钮显示一个 JTextField.面板中的预期结果变化.结果是当我按下按钮时什么也没有发生.

What I am trying to do is change the right side of my GUI each time I press a button. First button shows a JLabel second button a JTextField. Expected outcome change in panels. Outcome is that when I press the buttons nothing happens.

package javaapplication37;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.*;


public class Gui extends JFrame {
    JTextField f1;
    JPanel b, p1, p2;
    JPanel p3;
    JLabel l1;
    JButton b2, b1;
    String a;
    public Gui() {
        a="Input here";
        setSize(600,600);
        l1=new JLabel("8a petuxei");
        b = new JPanel();
        p3 = new JPanel();
        p1 = new JPanel();
        p2 = new JPanel();
        b.setLayout(new GridLayout(2, 1));
        b1 = new JButton("Eleos");
        b2 = new JButton("elpizw");
        b.add(b1);
        b.add(b2);
        b.setSize(150,600);
        p1.setSize(450,600);
        add(b);
        add(p1);
        ActionListener pou = new Listener(p1);
        b1.addActionListener(pou);
        p2.add(l1);
        f1=new JTextField(a);
        a=f1.getText();
        p3.add(f1);

    }

    public class Listener implements ActionListener {

        JPanel k;

        public Listener(JPanel k) {
            this.k = k;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            k.remove(getContentPane());
            k.add(p2);
        }

    }
    public class l implements ActionListener {

        JPanel k;

        public l(JPanel k) {
            this.k = k;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            k.remove(getContentPane());
            k.add(p3);
        }

    }  
}

推荐答案

您需要使用 CardLayout.如果你需要帮助问我.

You need to be using a CardLayout. If you need help ask me.

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class Gui extends JFrame implements ActionListener {

    private JPanel menu = new JPanel();
    private CardLayout contentLayout = new CardLayout();
    private JPanel content = new JPanel(contentLayout);

    private java.util.List<Card> cardList = new ArrayList<>();

    public Gui() {

        int i;
        for (i = 0; i < 10; i++) {
            Card card;
            if(i % 2 == 0) card = new TextAreaCard("Card " + i, "this is the content for card #" + i);
            else card = new LabelCard("Card " + i, "content for Label Card #" + i);

            JButton btn = new JButton(card.name);
            menu.add(btn);
            btn.addActionListener(this);
            content.add(card, card.name);
            cardList.add(card);
        }

        menu.setLayout(new GridLayout(i, 1));
        add(menu, BorderLayout.WEST);
        add(content, BorderLayout.CENTER);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        contentLayout.show(content, e.getActionCommand());
    }

    class Card extends JPanel{
        final String name;

        public Card(String name){
            this.name = name;
        }
    }

    class TextAreaCard extends Card implements ActionListener {

        JTextArea textArea = new JTextArea();
        JButton btn = new JButton("OK");

        public TextAreaCard(String name, String text) {
            super(name);
            textArea.setText(text);
            setLayout(new BorderLayout());
            add(textArea, BorderLayout.CENTER);
            add(btn, BorderLayout.SOUTH);
            btn.addActionListener(this);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(this, textArea.getText(), "click OK", JOptionPane.NO_OPTION);
        }
    }

    class LabelCard extends Card{
        JLabel label = new JLabel();

        public LabelCard(String name, String text) {
            super(name);
            label.setText(text);
            add(label);
        }
    }

    public static void main(String [] args){
        Gui gui = new Gui();
        gui.setSize(600, 500);
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setVisible(true);
    }
}

这篇关于在 gui 中更改面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆