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

public class NewAccountApplet extends JApplet implements ActionListener{
   /**
     *
     */
    private static final long serialVersionUID = 1L;
        JLabel titlePage;
        JLabel[] txt;
        JTextField[] jtf;
        JButton accept, decline;
        JPanel jp1, jp2, jp3, jp4, jp5, jp6;
        String[] accountlist = {"Select Account Type.", "Customer", "Admin"};
        JComboBox<Object> textAlignment = new JComboBox<Object>(accountlist);
        GridLayout productLO = new GridLayout(10,4,10,10);
        int qty = 5;
        JComboBox<Object>[] selectQty;


public void init(){
    setSize(400,400);

    JPanel content = (JPanel)getContentPane();
    GridBagConstraints firstCol = new GridBagConstraints();
    firstCol.weightx = 1.0;
    firstCol.anchor = GridBagConstraints.WEST;
    firstCol.insets = new Insets(5, 20, 5, 5);
    GridBagConstraints lastCol = new GridBagConstraints();
    lastCol.gridwidth = GridBagConstraints.REMAINDER;
    lastCol.weightx = 1.0;
    lastCol.fill = GridBagConstraints.HORIZONTAL;
    lastCol.insets = new Insets(5, 5, 5, 20);

    String[] labeltxt = {"Name","Account ID","Password","E-Mail","Phone","Address","","","Account Type"};
    titlePage = new JLabel("Create New Account");
    txt = new JLabel[9];
    jtf = new JTextField[9];
    accept = new JButton("Create");
    decline = new JButton("Decline");

    jp1 = new JPanel();
    jp2 = new JPanel(new GridBagLayout());
    jp3 = new JPanel();
    jp4 = new JPanel();
    jp5 = new JPanel();
    jp6 = new JPanel();

    for(int i=0; (i<9); i++) {
        txt[i] = new JLabel();
        txt[i].setText(labeltxt[i]);
        jp2.add(txt[i], firstCol);
        jtf[i] = new JTextField();
        jtf[i].setPreferredSize(new Dimension(300, 20));
        jp2.add(jtf[i], lastCol);

        }



    jp1.add(titlePage);
    jp3.add(accept);
    jp3.add(decline);
    content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
    content.add(jp1);
    content.add(jp2);
    content.add(jp3);
    String id = this.jtf[1].getText();
    String pw = this.jtf[2].getText();
    jtf[6].setText(id);
    jtf[7].setText(pw);

    jtf[6].setVisible(false);
    jtf[7].setVisible(false);
    jtf[8].setVisible(false);

    jp2.add(textAlignment, lastCol);

    decline.addActionListener(this);
    accept.addActionListener(this);

}

public void actionPerformed(ActionEvent e) {
    String id = jtf[1].getText();
    String pw = jtf[2].getText();
    String checkID = jtf[6].getText();
    String checkPW = jtf[7].getText();
    String accountType = "";
    String correctType = "Customer";
    String chosenType = (String) textAlignment.getSelectedItem();
    JPasswordField pField = new JPasswordField(10);
    JPanel pPanel = new JPanel();
    pPanel.add(new JLabel("Please Enter Password: "));
    pPanel.add(pField);

    if (e.getActionCommand().equals("Create") && (chosenType.equals("Customer"))){
        JOptionPane.showMessageDialog(null, "Thank you for Joining!");
        id = jtf[1].getText();
        pw = jtf[2].getText();
        titlePage.setText("Welcome to Final Sales!");
        accept.setText("Login");
        decline.setText("Cancel");
        txt[6].setText("UserName");
        txt[7].setText("Password");
        jtf[0].setText("");
        jtf[3].setText("");
        jtf[4].setText("");
        jtf[5].setText("");

        txt[0].setVisible(false);
        txt[1].setVisible(false);
        txt[2].setVisible(false);
        txt[3].setVisible(false);
        txt[4].setVisible(false);
        txt[5].setVisible(false);

        textAlignment.setVisible(false);
        txt[8].setVisible(false);

        jtf[0].setVisible(false);
        jtf[1].setVisible(false);
        jtf[2].setVisible(false);
        jtf[3].setVisible(false);
        jtf[4].setVisible(false);
        jtf[5].setVisible(false);
        jtf[6].setVisible(true);
        jtf[7].setVisible(true);

        }

    if (e.getActionCommand().equals("Create") && (chosenType.equals("Admin"))) {
        JOptionPane.showMessageDialog(null, pPanel);
        JOptionPane.showMessageDialog(null, "Wrong Admin Password");
    }

    if (e.getActionCommand().equals("Create") && (chosenType.equals("Select Account Type."))) {
        JOptionPane.showMessageDialog(null, "You have selected wrong account type.");
    }

    if (e.getActionCommand().equals("Decline"))
        System.exit(0);

    if (e.getActionCommand().equals("Login")) {
            if (id.equals(checkID) && pw.equals(checkPW)) {
            JOptionPane.showMessageDialog(null, "Authenticated");

            JPanel content = (JPanel)getContentPane();
            GridBagConstraints firstCol = new GridBagConstraints();
            firstCol.weightx = 1.0;
            firstCol.anchor = GridBagConstraints.WEST;
            firstCol.insets = new Insets(5, 20, 5, 5);
            GridBagConstraints lastCol = new GridBagConstraints();
            lastCol.gridwidth = GridBagConstraints.REMAINDER;
            lastCol.weightx = 1.0;
            lastCol.fill = GridBagConstraints.HORIZONTAL;
            lastCol.insets = new Insets(5, 5, 5, 20);

            selectQty = new JComboBox[qty];

            jp1.setVisible(false);
            jp2.setVisible(false);
            jp3.setVisible(false);
            jp4.setVisible(true);
            jp5.setVisible(true);
            jp6.setVisible(true);


            String[] itemText = {"White Snapback", "Silver Necklace", "Black T Shirt", "", "5"};
            JLabel[] items = new JLabel[5];
            JLabel purchasePage = new JLabel("Items for Purchase");
            jp4.add(purchasePage);
            content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
            content.add(jp4);

            jp4 = new JPanel();
            jp5.setLayout(new GridBagLayout());
            jp6 = new JPanel();

            for(int i=0; (i<items.length); i++) {
                items[i] = new JLabel();
                items[i].setText(itemText[i]);
                jp5.add(items[i], firstCol);
                selectQty[i] = new JComboBox<Object>();
                selectQty[i].setPreferredSize(new Dimension(300, 20));
                jp5.add(selectQty[i], lastCol);
            }







            }
            else JOptionPane.showMessageDialog(null, "Wrong account information");}

        if (e.getActionCommand().equals("Cancel")) {
            System.exit(0);}
        }


    }


我需要知道为什么我的jp5没有出现。
是因为gridbaglayout声明不正确吗?
我为jp5做过setVisible(true)和setLayout,但它仍然没有显示。
任何帮助将是巨大的!
谢谢。

最佳答案

您尚未将jp5添加到任何内容...

您可以通过在System.out.println("parent = " + jp5.getParent());方法中的任意位置添加actionPerformed来进行测试


小程序无法“退出”,在小程序中使用System.exit(0);毫无意义
这对于CardLayout是一个很好的用例
您应该考虑将每个“屏幕”分成自己的类/ JPanel,而不是将所有内容都填充到一个类中

10-07 23:27