【问题标题】:validation using card layout使用卡片布局进行验证
【发布时间】:2013-02-08 20:48:40
【问题描述】:

有一个 J 框架,我在其中放置了 2 个面板,并且两个面板都提供了卡片布局

图像 J Frame 有 2 个面板,其中一个是

面板窗格>>我将在哪里调用 panel1、panel2、panel3,其中所有 3 个面板都有一些控件,如 JtextField、jComboBox 等。

按钮窗格>>在此面板中,当按下下一个时,面板有按钮下一步和返回(如果面板窗格显示面板 1 则)面板 1 控件得到验证

像这样

代码如下



    package Code;

/**
 * 
 * @author Rohini
 */

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class InvoicePage extends JFrame implements ActionListener{

    JLabel newInvoic, invoiceNol, invoiceNo;
    JButton clinfoNext, payentryNext, termentryNext;
    JButton clinfoBack, payentryBack, termentryBack;


    JPanel buttonPane, clinfoBPane, payentryBPane, termentryBPane;
    JPanel panelpane, client_info, invoice_entry, term_entry;

    Container c = this.getContentPane();

    CardLayout cardmain, cardbutton;

    Font head = new Font("Times New Roman",Font.BOLD,20);
    Font subheadb = new Font("Times New Roman",Font.BOLD,14);
    Font subheadp = new Font("Times New Roman",Font.PLAIN,14);

    public InvoicePage() throws HeadlessException {

        super("Thane Bharat Sahakari Bank : New Invoice");

    //  initialization of variables
        cardmain  = new CardLayout();
        cardbutton = new CardLayout();        

        newInvoic = new JLabel("New Invoice");
        invoiceNol = new JLabel("Invoice No");
        invoiceNo = new JLabel("DB Value of id");

        clinfoNext = new JButton(" Next > > ");
        payentryNext = new JButton(" Next > > ");
        termentryNext = new JButton(" Next > > ");
        clinfoBack = new JButton(" < < Back ");
        payentryBack = new JButton(" < < Back ");
        termentryBack = new JButton(" < < Back ");

        buttonPane = new JPanel(cardbutton);
        clinfoBPane = new JPanel(null);
        payentryBPane = new JPanel(null);
        termentryBPane = new JPanel(null);

        panelpane = new JPanel(cardmain);
        client_info = new clientInfo();
        invoice_entry = new discription();
        term_entry = new termentry();

    //  setting properties of variabels

        panelpane.add(client_info,"Client_info");
        panelpane.add(invoice_entry,"invoice_entry");
        panelpane.add(term_entry,"term_entry");

        buttonPane.add(clinfoBPane,"clinfoBpane");
        buttonPane.add(payentryBPane,"payentryBPane");
        buttonPane.add(termentryBPane,"termentryBPane");

        clinfoBPane.add(clinfoBack);
        clinfoBPane.add(clinfoNext);
        payentryBPane.add(payentryBack);
        payentryBPane.add(payentryNext);
        termentryBPane.add(termentryBack);
        termentryBPane.add(termentryNext);

        newInvoic.setFont(head);
        invoiceNol.setFont(subheadb);
        invoiceNo.setFont(subheadp);

        clinfoNext.addActionListener(this);
        payentryNext.addActionListener(this);
        termentryNext.addActionListener(this);
        clinfoBack.addActionListener(this);
        payentryBack.addActionListener(this);
        termentryBack.addActionListener(this);

    //  setting Bounds
        Bounds(0,0);

    //  Adding Components
        c.add(newInvoic);
        c.add(invoiceNol);
        c.add(invoiceNo);
        c.add(buttonPane);
        c.add(panelpane);


    //  Form properties
        cardbutton.show(buttonPane,"clinfoBpane");
        cardmain.show(panelpane,"Client_info") ;        
        c.setLayout(null);
        c.setBackground(Color.WHITE);
        //clinfoBPane.setBackground(Color.WHITE);


        this.setResizable(true);
        this.setVisible(true);
        setDefaultCloseOperation(3);
        System.out.println("");
    }
    public void Bounds( int i, int j ){
        if(i == 0){
            newInvoic.setBounds(250,0,150,30);
            invoiceNol.setBounds(400,30,100,25);
            invoiceNo.setBounds(500,30,100,25);

            buttonPane.setBounds(0,410,610,50);
            panelpane.setBounds(0,50,610,350);
            clinfoNext.setBounds(430,5,150,30);
            clinfoBack.setBounds(25,5,150,30); 
            this.setSize(625,505);
            this.setLocation(300,150);
        }
        else if(i == 1){
            newInvoic.setBounds(350,0,150,30);
            invoiceNol.setBounds(600,30,100,25);
            invoiceNo.setBounds(700,30,100,25);

            buttonPane.setBounds(0,440,830,50);
            panelpane.setBounds(0,50,815,390);
            if(j == 0){
                payentryNext.setBounds(640,5,150,30);
                payentryBack.setBounds(30,5,150,30);
            }
            else if(j == 1){
                termentryNext.setBounds(640,5,150,30);
                termentryBack.setBounds(30,5,150,30);
            }           
            this.setSize(830,525);
            this.setLocation(200,100);
        }
        else{

        }
    }
        public void actionPerformed(ActionEvent ae){

            if(ae.getSource() == clinfoNext){
                cardbutton.show(buttonPane,"payentryBPane");
                cardmain.show(panelpane,"invoice_entry") ;
                Bounds(1,0);

            }
            else if(ae.getSource() == clinfoBack){
                this.dispose();
                Mainfrm mf = new Mainfrm();
                mf.setVisible(true);
            }
            else if((ae.getSource() == payentryNext)){
                Bounds(1,1);
                cardbutton.show(buttonPane,"termentryBPane");
                cardmain.show(panelpane,"term_entry") ;
            }
            else if(ae.getSource() == payentryBack){
                Bounds(0,0);
                cardbutton.show(buttonPane,"clinfoBpane");
                cardmain.show(panelpane,"Client_info") ;
            }
            else if(ae.getSource() == termentryBack){
                cardbutton.show(buttonPane,"payentryBPane");
                cardmain.show(panelpane,"invoice_entry") ;
                Bounds(1,0);
            }
            else if(ae.getSource() == termentryNext){
                JOptionPane.showConfirmDialog(this,"Are you sure that the invoice is over","Confirmation", JOptionPane.YES_NO_CANCEL_OPTION, 0);

            }
            else{

            }
        }
    public static void main(String []avi){

         new InvoicePage();
    } 
}

【问题讨论】:

  • 查看Validating Input 并编辑您的问题以包含一个sscce,以显示您遇到的任何问题。
  • 先生,我知道如何验证控件,但我希望通过使用卡片布局来解决在单独类中创建的面板
  • 编辑您的问题以澄清这一点。这个example 添加了几个same 类的实例;您可以将其用作sscce 的基础。
  • 先生,我已经看到 [link]stackoverflow.com/questions/5654926/…),我知道如何更改卡片布局中的面板
  • 我有上面的代码,clientinfo、description、termentry,是我想在点击相应的下一步按钮时验证的外部面板类

标签: java swing validation cardlayout


【解决方案1】:

是的!我得到了答案

实际上,外部面板变量需要有一个公共范围

&

(见上面的代码)

JPanel panelpane, client_info, invoice_entry, term_entry;

我已经创建了 JPanel 的实例并使用外部面板启动它,即

   client_info = new clientInfo();
        invoice_entry = new discription();
        term_entry = new termentry();

而不是面板正在做的事情

 JPanel panelpane, client_info, invoice_entry, term_entry;

通过尝试这个问题得到解决

  JPanel panelpane;
        clientInfo client_info;
        discription invoice_entry;

通过这个我们可以给actionListner

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-21
    • 1970-01-01
    • 2021-08-08
    • 2019-12-24
    • 2011-12-14
    • 2017-11-10
    • 1970-01-01
    • 2010-12-13
    相关资源
    最近更新 更多