简体   繁体   中英

JList won't show up on JFrame

Everything works except for the fact that the list doesn't show up on the JFrame. I don't understand why I can't add things without declaring a new layout.

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

    import java.awt.*;
    import java.awt.event.*;

public class Gui3 extends JFrame {
    private JPanel mousepanel;
    private JLabel statusbar;
    private JList list;
    private static String[] colornames = {"black","blue","red","white"};
    private static Color[] colors = {Color.BLACK, Color.BLUE,Color.RED,Color.WHITE};

    public Gui3(){
        super("The title");

        list = new JList(colornames);
        list.setVisibleRowCount(4);
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        list.setFixedCellHeight(15);
        list.setFixedCellWidth(100);
        add(new JScrollPane(list));
        list.addListSelectionListener(
                new ListSelectionListener(){
                    public void valueChanged(ListSelectionEvent event){

The user should be able to select the background color from the list and then the program should change the background to that color. But is isn't...

mousepanel.setBackground(colors[list.getSelectedIndex()]);

                    }
                }
            );

        mousepanel = new JPanel();
        mousepanel.setBackground(Color.WHITE);
        add(mousepanel, BorderLayout.CENTER);

        statusbar = new JLabel("Default");
        add(statusbar, BorderLayout.SOUTH); 


        HandlerClass handler = new HandlerClass();
        mousepanel.addMouseListener(handler);
        mousepanel.addMouseMotionListener(handler);

    }   
    private class HandlerClass implements MouseListener, MouseMotionListener
        {
        @Override
        public void mouseClicked(MouseEvent event) {
            statusbar.setText(String.format("Clicked at %d, %d", event.getX(),event.getY()));
            DrawShapes shapes = new DrawShapes();
            shapes.setPosition(event.getX(), event.getY());
            add(shapes);

        }
        @Override
        public void mousePressed(MouseEvent event){
            statusbar.setText("You pressed down the mouse");
        }

        @Override
        public void mouseReleased(MouseEvent event){
            statusbar.setText("You released the button");
        }
        @Override
        public void mouseEntered(MouseEvent event){
            statusbar.setText("You entered the area");
            mousepanel.setBackground(Color.RED);
        }
        @Override
        public void mouseExited(MouseEvent event){
            statusbar.setText("The mouse has left the window");
            mousepanel.setBackground(Color.WHITE);
        }
        //These are mouse motion events
        @Override
        public void mouseDragged(MouseEvent event){
            statusbar.setText("You are dragging the mouse");
        }
        @Override
        public void mouseMoved(MouseEvent event){
            statusbar.setText("You are moving the mouse");
        }

    }
}

Because you use BorderLayout but don't set position for ScrollPane . So, mousePanel will lie on ScrollPane .

Try this

add(new JScrollPane(list), BorderLayout.WEST);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM