gpt4 book ai didi

java - 为不同的自定义列表再次重用 JScrollPane 的问题

转载 作者:行者123 更新时间:2023-11-29 08:16:48 25 4
gpt4 key购买 nike

我正在尝试创建一个 JScrollPane 组件,它可以根据传递的字符串参数重复使用。我创建了以下代码,如果我使用 JFrame 将 JScrollPane 嵌入其中,它就可以工作,但是当我尝试重用代码来创建不同的 JScrollPane 时,根本不显示 scrollPane ..我在 java swing 项目中实现它..请让我知道在这方面应该做什么。非常感谢任何建议。

关于 JComboBox 我也有同样的问题,它不显示...

public class ListComponent extends JScrollPane {

private String[] selectedNames;
private String[] listNames;
private JButton submit, reset;
private JPanel subPanel = new JPanel();
private JList list;
private JScrollPane scroll;

public String getFirstSelectionInList() {
return selectedNames[0];
}

ListComponent() {

/*submit = new JButton("Submit");
reset = new JButton("Reset");*/
subPanel.setLayout(new FlowLayout());
/*subPanel.add(submit);
subPanel.add(reset);*/
}

ListComponent(String[] listNames) {
this();
/*JFrame frame = new JFrame("Creating a JList Component");*/

this.listNames = new String[listNames.length];
this.listNames = listNames;
//this.setLayout(new BorderLayout());
//this.setLayout(new ScrollPaneLayout());
JPanel panel = new JPanel();
list = new JList(listNames);
scroll = new JScrollPane(list);
scroll.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
/*panel.add(scroll);
panel.add(subPanel,BorderLayout.SOUTH);*/

/*frame.add(panel);
frame.pack();
frame.setVisible(true);*/

this.setVisible(true);
list.addListSelectionListener(listSelectionListener);
}

ListSelectionListener listSelectionListener = new ListSelectionListener() {

public void valueChanged(ListSelectionEvent listSelectionEvent) {

boolean adjust = listSelectionEvent.getValueIsAdjusting();
//System.out.println(", Adjusting? " + adjust);
if (!adjust) {
JList list = (JList) listSelectionEvent.getSource();
int selections[] = list.getSelectedIndices();
Object selectionValues[] = list.getSelectedValues();
int n = selections.length;
selectedNames = new String[n];
for (int i = 0; i < n; i++) {
if (i == 0) {
System.out.print(" Selections: ");
}
selectedNames[i] = (String) selectionValues[i];
System.out.print(selectedNames[i]
+ "/" + selectionValues[i]);
}
}
}
};

/** @return the selectedNames */
public String[] getSelectedNames() {
return selectedNames;
}

/** @param selectedNames the selectedNames to set */
public void setSelectedNames(String[] selectedNames) {
this.selectedNames = selectedNames;
}

public static void main(String args[]) {
String subject[] = {"Math", "Computer", "Phisics", "Chemestry"};
new ListComponent(subject);
}
}

最佳答案

@camickr 是正确的; JPanel是一个更好的“轻量级组件的通用容器”。您可以将其他组件添加到以下示例中使用的 BorderLayout 的其他区域。在 event dispatch thread 上构建 GUI 也是一个好习惯.

import java.awt.*;
import java.util.Arrays;
import javax.swing.*;
import javax.swing.event.*;

/** @see http://stackoverflow.com/questions/4176343 */
public class ListPanel extends JPanel {

private JList list;

public ListPanel(String[] data) {
super(new BorderLayout());
list = new JList(data);
list.addListSelectionListener(new SelectionHandler());
JScrollPane jsp = new JScrollPane(list);
this.add(jsp, BorderLayout.CENTER);
}

private class SelectionHandler implements ListSelectionListener {

@Override
public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
System.out.println(Arrays.toString(list.getSelectedValues()));
}
}
}

private void display() {
JFrame f = new JFrame("ListPanel");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
String[] data = {"Math", "Computer", "Physics", "Chemistry"};
new ListPanel(data).display();
}
});
}
}

关于java - 为不同的自定义列表再次重用 JScrollPane 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4176343/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com