Frage

Very new to Java, but I am slowly picking my way through things. So please be kind. I understand most things I've tried so far, and built a version of the following that uses console output, but now I'm trying to make a GUI. I tried the netbeans GUI maker, but it created so much new code that when I tried to pick through it, I got lost. I'm much better at learning by piecing new things together myself, not having an IDE generate a ton of code and then attempt to find where I want to work.

I am trying to build an window that has a list with three choices on the left side, a button in the middle that confirms your choice, and an answer output on the right. Once the button is pressed, the input from the list is read and is converted into a corresponding answer. As of right now, all I get is "We recommend... null" after selecting an option in the list. The button appears to do nothing at the moment.

I have used tutorials, hacked up others' code from online, and referenced a few books, but I'm stuck.

Here is what I have:

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

}
War es hilfreich?

Lösung

The button appears to do nothing at the moment.

It does something. It calculates the value for your recommendOutput varable. But you never output this value.

try the following:

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

This should print the value to stdout

To put the value into your label try this instead:

output.setText(recommendOutput);

Andere Tipps

where do you set the text for the JLabel? It says "We recommend NULL" because recommenedOutput is null when the object is created. I dont see output.setText("We recommend "+value) anywhere. You probably need output.invalidate() also. Try putting setText(String text)/invalidate() in the RecommendListener.actionPerformed() method.

output.setText("We recommend A");
output.invalidate();
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top