2
爲什麼標籤不顯示?選擇列表中的項目後在面板上設置標籤
package javaapplication4;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class settingPanel extends javax.swing.JPanel {
static JPanel con = new JPanel();
static JFrame frame = new JFrame();
JLabel jLabel1 = new JLabel();
public JEditorPane htmlvi = new JEditorPane();
/** Creates new form settingPanel */
public settingPanel() {
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.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jTabbedPane1 = new javax.swing.JTabbedPane();
jPanel1 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
jList1 = new javax.swing.JList();
jList1.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4",
"Item 5" };
public int getSize() {
return strings.length;
}
public Object getElementAt(int i) {
return strings[i];
}
});
jList1.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
jList1ValueChanged(evt);
}
});
jScrollPane1.setViewportView(jList1);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING).addGroup(
jPanel1Layout
.createSequentialGroup()
.addComponent(jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE, 70,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(325, Short.MAX_VALUE)));
jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING).addComponent(
jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 272,
Short.MAX_VALUE));
jTabbedPane1.addTab("tab1", jPanel1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING).addComponent(
jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400,
Short.MAX_VALUE));
layout.setVerticalGroup(layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING).addComponent(
jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300,
Short.MAX_VALUE));
}// </editor-fold>
private void addpanel() {
JPanel pnl = new JPanel();
pnl.setOpaque(false);
pnl.setLayout(new GridBagLayout());
pnl.add(new JButton());
pnl.setVisible(true);
jPanel1.setLocation(10, 30);
jPanel1.add(pnl);
}
private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) {
JLabel write = new JLabel();
// if(evt.getValueIsAdjusting()){
String read = "<html><h2>" + jList1.getSelectedValue().toString()
+ "<UL>USB";
addpanel();
jPanel1.add(write);
}
// Variables declaration - do not modify
private javax.swing.JList jList1;
public static javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTabbedPane jTabbedPane1;
// End of variables declaration
public static void main(String args[]) {
frame.add(new settingPanel());
// jPanel2.setLayout(new GridBagLayout());
// jPanel2.add(new JButton());
// jPanel2.setVisible(false);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(3);
}
}
+1一個不錯的和完整的答案!真的沒什麼好補充的! – GingerHead