1

For some reason the Jlist will not show up on my applet.

It shows up just right of the slider but only when you click on the individual elements.

I tried this.validate() and this.repaint() with no luck. Can anyone help me out?

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import java.awt.Graphics;
import javax.swing.JList;
import javax.swing.JRadioButton;
import javax.swing.JSlider;
import javax.swing.ListSelectionModel;

public class HeatingHome extends JApplet implements ActionListener
{
        // declare variables here

    JRadioButton switchIt = new JRadioButton();
    JSlider tempControl = new JSlider(JSlider.VERTICAL, 10, 15, 11);


    String[] theRooms = {"Porch", "Kitchen", "Living Room", "Hall", "Bedroom 1", "Bathroom", "Bedroom 2"};   
    JList roomsList = new JList(theRooms);


    public void init()
    {

        setSize(1000,600);


    }

    public void paint(Graphics g)
    {
        super.paint(g);


        roomsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        roomsList.setBounds(700, 200, 150, 150);
        roomsList.setVisible(true);
        roomsList.setEnabled(true);
        add(roomsList);


               //tempControl.addChangeListener(e);
       tempControl.setMajorTickSpacing(10);
       tempControl.setPaintLabels(true);
       tempControl.setMinorTickSpacing(1);
       tempControl.setPaintTicks(true);
       tempControl.setBounds(600, 200, 100, 200);
       tempControl.setEnabled(true);
       add(tempControl);



    }



    @Override
    public void actionPerformed(ActionEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
 }
4

2 回答 2

1

You're adding plenty of controls every time the applet is painted. Which means every new control you add will trigger at least another paint operation.

Move all the code from the paint method out into a constructor or similar.

paint is for painting the control's visuals yourself. I.e. you take the supplied Graphics object and do stuff on it until you're happy. You're usually not modifying anything else as painting is a bit unpredictable in general.

于 2011-10-16T16:09:47.143 回答
1

每次绘制时都会将列表和滑块添加到小程序中,而不会添加单选按钮。

这可能会让你开始:

供暖之家

// <applet code='HeatingHome' width=400 height=200></applet>
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

public class HeatingHome extends JApplet
{
    // declare variables here
    JRadioButton switchIt = new JRadioButton();
    JSlider tempControl = new JSlider(JSlider.VERTICAL, 10, 15, 11);

    String[] theRooms = {"Porch", "Kitchen", "Living Room", "Hall", "Bedroom 1", "Bathroom", "Bedroom 2"};
    JList roomsList = new JList(theRooms);

    public void init()
    {
        // applet size is set by the HTML
        //setSize(1000,600);
        setLayout(new BorderLayout());

        JPanel gui = new JPanel(new BorderLayout(5,5));
        gui.setBackground(Color.ORANGE);
        gui.setBorder(new EmptyBorder(20,20,20,20));

        roomsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        // Use LAYOUTS!!!
        //roomsList.setBounds(700, 200, 150, 150);
        // Becomes visible when added to something!
        //roomsList.setVisible(true);
        // Enabled by default!
        //roomsList.setEnabled(true);
        gui.add(roomsList, BorderLayout.EAST);

        //tempControl.addChangeListener(e);
        tempControl.setMajorTickSpacing(10);
        tempControl.setPaintLabels(true);
        tempControl.setMinorTickSpacing(1);
        tempControl.setPaintTicks(true);
        //tempControl.setBounds(600, 200, 100, 200);
        //tempControl.setEnabled(true);
        //add(tempControl);
        gui.add(tempControl, BorderLayout.WEST);

        gui.add(switchIt, BorderLayout.NORTH);

        add(gui);
        validate();
    }
}
于 2011-10-16T16:19:04.873 回答