【问题标题】:Arraylist not public in my program [closed]Arraylist 在我的程序中不公开[关闭]
【发布时间】:2014-05-27 14:50:57
【问题描述】:

我在 Netbeans 中制作了一个 Jframe,并使用了一个初始化按钮,它显示了五张 CD。我有一个添加按钮,但是当我输入 CDs(我的数组列表的名称)时,它会显示错误。

public class GUI extends javax.swing.JFrame {

public GUI() {
    initComponents();
addButton.setEnabled(false);
removeButton.setEnabled(false);
displayButton.setEnabled(false);

}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked") 
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    jToggleButton3 = new javax.swing.JToggleButton();
    jPanel1 = new javax.swing.JPanel();
    titleLabel = new javax.swing.JLabel();
    titleartistLabel = new javax.swing.JLabel();
    CDInput = new javax.swing.JTextField();
    displayButton = new javax.swing.JToggleButton();
    initializeButton = new javax.swing.JToggleButton();
    addButton = new javax.swing.JToggleButton();
    removeButton = new javax.swing.JButton();
    outputScroll = new javax.swing.JScrollPane();
    output = new javax.swing.JTextArea();

    jToggleButton3.setText("jToggleButton3");

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jPanel1.setBackground(new java.awt.Color(0, 0, 0));
    jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

    titleLabel.setFont(new java.awt.Font("Courier New", 0, 14)); // NOI18N
    titleLabel.setForeground(new java.awt.Color(102, 255, 51));
    titleLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    titleLabel.setText("CD Collection\n");

    titleartistLabel.setFont(new java.awt.Font("Courier New", 0, 11)); // NOI18N
    titleartistLabel.setForeground(new java.awt.Color(102, 255, 51));
    titleartistLabel.setText("Title - Artist");

    CDInput.setFont(new java.awt.Font("Courier New", 0, 11)); // NOI18N
    CDInput.setForeground(new java.awt.Color(51, 51, 51));
    CDInput.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            CDInputActionPerformed(evt);
        }
    });

    displayButton.setBackground(new java.awt.Color(0, 0, 0));
    displayButton.setFont(new java.awt.Font("Courier New", 0, 11)); // NOI18N
    displayButton.setForeground(new java.awt.Color(102, 255, 51));
    displayButton.setText("Display");
    displayButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            displayButtonActionPerformed(evt);
        }
    });

    initializeButton.setBackground(new java.awt.Color(0, 0, 0));
    initializeButton.setFont(new java.awt.Font("Courier New", 0, 11)); // NOI18N
    initializeButton.setForeground(new java.awt.Color(102, 255, 51));
    initializeButton.setText("Initialize");
    initializeButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            initializeButtonActionPerformed(evt);
        }
    });

    addButton.setBackground(new java.awt.Color(0, 0, 0));
    addButton.setFont(new java.awt.Font("Courier New", 0, 11)); // NOI18N
    addButton.setForeground(new java.awt.Color(102, 255, 51));
    addButton.setText("Add");
    addButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            addButtonActionPerformed(evt);
        }
    });

    removeButton.setBackground(new java.awt.Color(0, 0, 0));
    removeButton.setFont(new java.awt.Font("Courier New", 0, 11)); // NOI18N
    removeButton.setForeground(new java.awt.Color(102, 255, 51));
    removeButton.setText("Remove");

    output.setEditable(false);
    output.setColumns(20);
    output.setLineWrap(true);
    output.setRows(5);
    output.setWrapStyleWord(true);
    outputScroll.setViewportView(output);

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(outputScroll)
                .addComponent(titleLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(titleartistLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(displayButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGap(18, 18, 18)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(initializeButton)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 67, Short.MAX_VALUE)
                            .addComponent(addButton)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(removeButton))
                        .addComponent(CDInput))))
            .addContainerGap())
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(titleLabel)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(titleartistLabel)
                .addComponent(CDInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(18, 18, 18)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(displayButton)
                .addComponent(initializeButton)
                .addComponent(addButton)
                .addComponent(removeButton))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(outputScroll, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE)
            .addContainerGap())
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );

    pack();
}// </editor-fold>

private void CDInputActionPerformed(java.awt.event.ActionEvent evt) {                                        
    // TODO add your handling code here:
}                                       

private void displayButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              



}                                             

private void initializeButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                 
    ArrayList <String> CDs = new ArrayList();

    Collections.addAll(CDs, "Wildlife - La Dispute",
            "To the Beat of a Dead Horse - Touche Amore",
            "Meteora - Linkin Park",
            "Rooms of the House - La Dispute",
            "Somewhere at the Bottom of the River Between Vega and Altair - La Dispute");

    initializeButton.setEnabled(false);
    addButton.setEnabled(true);
    removeButton.setEnabled(true);
    displayButton.setEnabled(true);

    for(String s : CDs){
      output.append(s + System.getProperty("line.separator"));
    }   
}                                                

private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {
    String newCD = CDInput.getText();
    CDs.add(newCD);

【问题讨论】:

  • 请包含堆栈跟踪
  • 正如@dharr 所说,如果没有堆栈跟踪,我们根本无法帮助您,因为我们不知道您遇到了什么错误。
  • 您在initializeButtonActionPerformed 中创建CDs ArrayList。这意味着CDs 仅存在于该方法中;它不存在addButtonActionPerformed
  • @dharr:我认为他遇到了编译时错误。
  • 抱歉堆栈跟踪的事情......我是新来的,下次我会记住的

标签: java swing arraylist jframe


【解决方案1】:

我认为该变量不在您的范围内
您在函数initializeButtonActionPerformed 中定义它,但想在函数addButtonActionPerformed 中使用它

【讨论】:

    【解决方案2】:

    制作一片CD。无论是在顶部还是在其他领域。

    public class GUI extends javax.swing.JFrame {
    
        private List<String> cds = new ArrayList<>();
    

    那么它应该可以工作了。

    【讨论】:

    • 谢谢先生,成功了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-14
    • 2012-08-21
    • 2014-11-06
    • 1970-01-01
    • 2014-11-07
    相关资源
    最近更新 更多