我在Netbeans中创建了一个Jframe并使用初始化按钮,它显示了五张CD。我有一个添加按钮,但是当我输入CD(我的arraylist' s)时,它会显示错误。
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);
答案 0 :(得分:2)
我认为变量不在你的范围内
您可以在函数initializeButtonActionPerformed
中定义它,但希望在函数addButtonActionPerformed
答案 1 :(得分:2)
制作CD领域。无论是在顶部还是在其他领域。
public class GUI extends javax.swing.JFrame {
private List<String> cds = new ArrayList<>();
然后它应该工作。