gpt4 book ai didi

java - 选择列表中的项目后在面板上设置标签

转载 作者:行者123 更新时间:2023-11-30 07:25:41 26 4
gpt4 key购买 nike

为什么标签不显示?

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);
}
}

最佳答案

您将 JLabel 添加到 jPanel1,它使用 GroupLayout 来显示其组件。 GroupLayout 需要知道每个组件必须放置在其垂直和水平组中的什么位置,并且它不知道将新标签放在哪里,因为您没有告诉。

我非常怀疑您的最终目标是在每次选择更改时向您的 GUI 添加一个新标签。您可能应该在 GUI 初始化时添加一个带有默认文本的唯一标签,并在选择更改时更改此标签的文本。

关于java - 选择列表中的项目后在面板上设置标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10780024/

26 4 0