【问题标题】:JComboBox[] loop to show on JApplet not workingJComboBox[] 循环显示在 JApplet 上不起作用
【发布时间】:2014-05-05 14:17:06
【问题描述】:
public class NewAccountApplet extends JApplet implements ActionListener{
    JPanel jp1, jp2, jp3, jp4, jp5, jp6;
    GridLayout productLO = new GridLayout(10,4,10,10);
    int qty = 5;
    JComboBox<Object>[] selectQty;

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 = {"1", "2", "3", "4", "5"};
    JLabel[] items = new JLabel[6];
    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 = new JPanel(new GridBagLayout()); //set jp5 as a new jpanel with gridbaglayout
    jp6 = new JPanel(); 

    for(int i=0; (i<items.length); i++) { 
        items[i] = new JLabel(); //adds items[i] as JLabel
        items[i].setText(itemText[i]); //sets text of items as itemText[]
        jp5.add(items[i], firstCol);  //adds items to firstcol of jp5
        selectQty[i] = new JComboBox<Object>(); //JComboBox selectqty[i]
        selectQty[i].setPreferredSize(new Dimension(300, 20)); //sets the size
        jp5.add(selectQty[i], lastCol); //sadsdasd
        }

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

我有一些关于将 JComboBox 添加到循环中以在我的 JApplet 上显示的问题。 底部的 for 循环将我的 JComboBox (selectQty) 添加到屏幕上。但是我在 Eclipse 上收到一条错误消息,我将其编码为:items[i].setText(itemText[i]);。 它正确显示了我的 JPanel jp4。但是 JPanel jp5 没有出现.. 我不知道出了什么问题...

总而言之,代码可以编译(这里没有其他代码),但是japplet只显示jp4 jpanel,并且在线错误occrs:items[i].setText(itemText[i]);。

【问题讨论】:

标签: java swing loops jcombobox japplet


【解决方案1】:

itemText 有 5 个元素 {"1", "2", "3", "4", "5"}JLabel[] items = new JLabel[6] 有 6 个元素。

【讨论】:

  • 谢谢。我改变了它,它编译并运行没有错误。
  • 不要重新创建 jp5 = new JPanel(new GridBagLayout());但调用 jp5.setLayout(new GridBagLayout()) 代替。
  • 这给了我一个 java.lang.NullPointerException
  • 那么发布的代码不是您的实际代码。你打电话 jp5.setVisible(true); NPE 应该在那里发生。
  • 我已经发布了实际代码作为答案之一......你能看一下吗?
【解决方案2】:

您似乎正在重新创建jp5,但既没有删除旧实例也没有添加新实例...

// This suggests that jp5 has already been created
jp5.setVisible(true);
//...
// But you create a new instance here
jp5 = new JPanel(new GridBagLayout());
// Then add stuff to it...

尝试使用jp5.removeAll() 而不是jp5 = new JPanel(new GridBagLayout());

您的错误的可能原因可能是 IndexOutOfBoundsExceptionitemTextitems 具有不同数量的元素引起的

String[] itemText = {"1", "2", "3", "4", "5"};
JLabel[] items = new JLabel[6];
//...
for(int i=0; (i<items.length); i++) { 
    items[i] = new JLabel(); //adds items[i] as JLabel
    // Error here on the last element...
    items[i].setText(itemText[i]); //sets text of items as itemText[]

相反,请考虑使用类似...

String[] itemText = {"1", "2", "3", "4", "5"};
JLabel[] items = new JLabel[itemText.length];

记住,幻数是个坏主意

【讨论】:

    【解决方案3】:
    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);}
            }
    
    
        }
    

    【讨论】:

    • 只转储大量固定 (?) 代码无助于理解问题。描述问题所在,并仅包含所需的代码部分。
    猜你喜欢
    • 1970-01-01
    • 2011-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多