gpt4 book ai didi

java - 将监听器与列表和按钮一起使用

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

对 Java 很陌生,但我正在慢慢地寻找自己的方法。所以请善待。我理解到目前为止我尝试过的大多数事情,并构建了以下使用控制台输出的版本,但现在我正在尝试制作一个 GUI。我尝试了 netbeans GUI maker,但它创建了太多新代码,以至于当我试图挑选它时,我迷路了。我更擅长通过自己拼凑新事物来学习,而不是让 IDE 生成大量代码,然后尝试找到我想工作的地方。

我正在尝试构建一个窗口,该窗口的左侧有一个包含三个选项的列表,中间有一个确认您的选择的按钮,右侧有一个答案输出。按下按钮后,将读取列表中的输入并将其转换为相应的答案。截至目前,在列表中选择一个选项后,我得到的只是“We recommend...null”。该按钮目前似乎什么都不做。

我已经使用了教程,从网上破解了别人的代码,并引用了几本书,但我被卡住了。

这是我的:

package diffguidegui;

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


public class DiffGuideGUI extends JPanel implements ListSelectionListener {

private JList resultsTabList;
private DefaultListModel listModel;
private static final String recommendString = "Recommend a Option";
private JButton recommendButton;
private String recommendOutput;
final JLabel output = new JLabel("We recommend..." + recommendOutput);

//build list
public DiffGuideGUI () {
super(new BorderLayout());
listModel = new DefaultListModel();
listModel.addElement("A");
listModel.addElement("B");

//create the list and put it in the scroll pane
resultsTabList = new JList(listModel);
resultsTabList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
resultsTabList.setSelectedIndex(0);
//listener for user input
resultsTabList.addListSelectionListener(this);
resultsTabList.setVisibleRowCount(2);
JScrollPane listScrollPane = new JScrollPane(resultsTabList);

//build the button at the bottom to fire overall behavior
recommendButton = new JButton(recommendString);
recommendButton.setActionCommand(recommendString);
recommendButton.addActionListener(new RecommendListener());

//create a panel that uses Boxlayout for the button
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
buttonPane.add(recommendButton);

//create a panel that uses Boxlayout for the label
JPanel outputPane = new JPanel();
outputPane.setLayout(new BoxLayout(outputPane, BoxLayout.LINE_AXIS));
outputPane.add(output);

add(listScrollPane, BorderLayout.WEST);
add(buttonPane, BorderLayout.CENTER);
add(outputPane, BorderLayout.EAST);
}


//build listener class
class RecommendListener implements ActionListener {
public void actionPerformed(ActionEvent e) {

//build in logic for choice made here
String resultsTabChoice;
resultsTabChoice = (String)resultsTabList.getSelectedValue();

if( resultsTabChoice.equals("A")) {
recommendOutput = "One";}
else {recommendOutput = "Two";}
}
}


public void valueChanged(ListSelectionEvent e) {
if(e.getValueIsAdjusting() == false) {

if(resultsTabList.getSelectedIndex() == -1) {
recommendButton.setEnabled(false);
} else {
recommendButton.setEnabled(true);
}
}
}

//Create GUI and show it
private static void createAndShowGUI() {
JFrame frame = new JFrame("Recommend Window");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


//create and set up content pane
JComponent newContentPane = new DiffGuideGUI();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);

//display the window
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}

}

最佳答案

The button appears to do nothing at the moment.

一些事情。它计算您的 recommendOutput 变量的值。但是你永远不会输出这个值。

尝试以下操作:

 //build listener class
class RecommendListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
//build in logic for choice made here
String resultsTabChoice;
resultsTabChoice = (String)resultsTabList.getSelectedValue();

if( resultsTabChoice.equals("A")) {
recommendOutput = "One";}
else {recommendOutput = "Two";}
System.out.println(recommendOutput); // <-###################
}
}

这应该将值打印到标准输出

要将值放入您的标签中,请尝试以下操作:

output.setText(recommendOutput);

关于java - 将监听器与列表和按钮一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23685745/

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