Java非常新,但我正在慢慢挑选自己的方式。所以请善待。我理解到目前为止我尝试过的大多数事情,并构建了以下使用控制台输出的版本,但现在我正在尝试创建一个GUI。我尝试了netbeans GUI制作工具,但它创造了很多新代码,当我试图通过它时,我迷路了。通过自己拼凑新东西,没有IDE生成大量代码然后尝试找到我想要工作的地方,我学习得更好。
我正在尝试构建一个窗口,其左侧有三个选项列表,中间有一个确认您选择的按钮,右侧是答案输出。按下按钮后,将读取列表中的输入并将其转换为相应的答案。截至目前,在列表中选择一个选项后,我得到的只是“我们建议... 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();
}
});
}
}
答案 0 :(得分:0)
您在哪里设置JLabel的文本?它说"我们建议使用NULL"因为在创建对象时,recommendednedOutput为null。我没看到
output.setText("We recommend "+value)
任何地方。您可能还需要output.invalidate()
。尝试将setText(String text) / invalidate()放在RecommendListener.actionPerformed()方法中。
output.setText("We recommend A");
output.invalidate();
答案 1 :(得分:0)
此按钮似乎什么都不做。
它确实。它会计算您的recommendedOutput varable的值。但是你永远不会输出这个值。
尝试以下方法:
//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); // <-###################
}
}
这应该将值打印到stdout
要将值放入标签,请尝试改为:
output.setText(recommendOutput);