📄 demojlistframe.java
字号:
/*
* DemoJListFrame.java
*
* Created on 2006年3月18日, 下午8:02
*/
package org.netbeans.swing.container;
import javax.swing.ListModel;
/**
*
* @author boyingking
*/
public class DemoJListFrame extends javax.swing.JFrame {
/** Creates new form DemoJListFrame */
public DemoJListFrame() {
initComponents();
}
/** 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.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jListCountryList = new javax.swing.JList();
jButtonAdd = new javax.swing.JButton();
jLabelActionMessage = new javax.swing.JLabel();
jTextFieldCountryName = new javax.swing.JTextField();
jButtonDelete = new javax.swing.JButton();
jLabelList = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("JList\u5e94\u7528\u793a\u4f8b");
jListCountryList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
jListCountryListValueChanged(evt);
}
});
jScrollPane1.setViewportView(jListCountryList);
jButtonAdd.setText("\u6dfb\u52a0");
jButtonAdd.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonAddActionPerformed(evt);
}
});
jLabelActionMessage.setFont(new java.awt.Font("宋体", 0, 24));
jLabelActionMessage.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabelActionMessage.setText("\u4f60\u8fd8\u6ca1\u6709\u8fdb\u884c\u4efb\u4f55\u64cd\u4f5c");
jButtonDelete.setText("\u5220\u9664");
jButtonDelete.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonDeleteActionPerformed(evt);
}
});
jLabelList.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabelList.setText("\u56fd\u5bb6\u5217\u8868");
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(30, 30, 30)
.add(jLabelList, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 68, Short.MAX_VALUE)
.add(54, 54, 54))
.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 84, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(38, 38, 38)))
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
.add(44, 44, 44)
.add(jButtonAdd)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 99, Short.MAX_VALUE)
.add(jButtonDelete)
.add(78, 78, 78))
.add(layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
.add(jTextFieldCountryName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 309, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(org.jdesktop.layout.GroupLayout.LEADING, jLabelActionMessage, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 301, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addContainerGap())))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(18, 18, 18)
.add(jLabelList, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 24, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(jLabelActionMessage, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 104, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(20, 20, 20)
.add(jTextFieldCountryName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 28, Short.MAX_VALUE)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jButtonDelete)
.add(jButtonAdd))
.add(23, 23, 23))
.add(layout.createSequentialGroup()
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 184, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap())))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void jButtonDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDeleteActionPerformed
// TODO add your handling code here:
String countryName=(String)this.jListCountryList.getSelectedValue();
if(countryName!=null)
{
this.imageVector.remove(countryName);
this.jListCountryList.setListData(imageVector);
this.jLabelActionMessage.setText("你将"+countryName+"从列表中删除");
}
else
{
this.jLabelActionMessage.setText("请先选择一个选项");
}
}//GEN-LAST:event_jButtonDeleteActionPerformed
private void jListCountryListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jListCountryListValueChanged
String choosedCountry=(String)this.jListCountryList.getSelectedValue();
this.jLabelActionMessage.setText("你从列表中选择"+choosedCountry+"选项");
}//GEN-LAST:event_jListCountryListValueChanged
private void jButtonAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAddActionPerformed
// TODO add your handling code here:
String countryName=this.jTextFieldCountryName.getText().trim();
if(countryName!=null&&!countryName.equals(""))
{
this.imageVector.add(countryName);
this.jLabelActionMessage.setText("你将"+countryName+"添加到列表中");
this.jListCountryList.setListData(this.imageVector);
}
else
{
this.jLabelActionMessage.setText("请先输入内容再添加");
}
}//GEN-LAST:event_jButtonAddActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new DemoJListFrame().setVisible(true);
}
});
}
private java.util.Vector imageVector = new java.util.Vector ();
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButtonAdd;
private javax.swing.JButton jButtonDelete;
private javax.swing.JLabel jLabelActionMessage;
private javax.swing.JLabel jLabelList;
private javax.swing.JList jListCountryList;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField jTextFieldCountryName;
// End of variables declaration//GEN-END:variables
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -