gpt4 book ai didi

java - 在 JList 中选择值后出现 JButton

转载 作者:行者123 更新时间:2023-12-01 09:50:27 24 4
gpt4 key购买 nike

我有一个包含一些元素的 JList,并且我希望仅当用户在列表中选择一个值时才出现 JButton。这是我尝试过的:

public void itemStateChanged(ItemEvent event) {
int choice = avDevBox.getSelectedIndex();
if (choice = 0) {
list = new JList(listModel1);
list.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent ev1) {
if (!ev1.getValueIsAdjusting()) {
String outputext = list.getSelectedValue().toString();
itemDetails.setText(outputext);
}
}

});
add(list);
add(itemDetails);
add(but1);
}

有什么办法可以做到这一点吗?

提前致谢。

最佳答案

您可以在 GUI 上已有一个 JButton,但可以通过 myButton.setVisible(false) 将其设置为不可见。然后在 JList 的 ListSelectionListener 中调用 myButton.setVisible(true)。然后,您需要在保存现在可见的 JButton 的容器(通常是 JPanel)上调用 revalidate()repaint()

请注意,对于将来的问题,您将需要创建并发布更相关的代码,最好是 Minimal, Complete, and Verifiable Example Program ,因为这将使我们能够了解为什么您当前的代码不起作用,并有助于我们自信地回答。

MCVE 示例:

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

@SuppressWarnings("serial")
public class McveExample extends JPanel {
private static final String[] DATA = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
private static final int PREF_W = 400;
private static final int PREF_H = 250;
private JList<String> list = new JList<>(DATA);
private JButton myButton = new JButton();

public McveExample() {

myButton.setVisible(false); // **** make button invisible

list.setVisibleRowCount(4);
list.addListSelectionListener(new ListListener()); // listener that does the dirty work

JScrollPane scrollPane = new JScrollPane(list);

// add a JButton that resets myButton back to being invisible
add(new JButton(new ResetAction("Reset", KeyEvent.VK_R)));
add(scrollPane);
add(myButton);
}

// make sure things are big enough
@Override
public Dimension getPreferredSize() {
Dimension superSz = super.getPreferredSize();
if (isPreferredSizeSet()) {
return superSz;
}
int prefW = Math.max(superSz.width, PREF_W);
int prefH = Math.max(superSz.height, PREF_H);
return new Dimension(prefW, prefH);
}

private class ResetAction extends AbstractAction {
public ResetAction(String name, int mnemonic) {
super(name);
putValue(MNEMONIC_KEY, mnemonic);
}

@Override
public void actionPerformed(ActionEvent e) {
list.clearSelection();
myButton.setVisible(false);
McveExample.this.revalidate();
McveExample.this.repaint();
}
}

private class ListListener implements ListSelectionListener {
@Override
public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
JList<String> lst = (JList<String>) e.getSource();
String selection = lst.getSelectedValue();
if (selection != null) {
myButton.setText(selection);
myButton.setVisible(true);
McveExample.this.revalidate();
McveExample.this.repaint();
}
}
}
}

private static void createAndShowGui() {
McveExample mainPanel = new McveExample();

JFrame frame = new JFrame("McveExample");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}

关于java - 在 JList 中选择值后出现 JButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37640400/

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