Jlist未显示/出现在applet中

时间:2011-10-16 16:06:21

标签: java layout applet jlist japplet

出于某种原因,我的applet上不会显示Jlist

它只显示滑块的右侧,但仅在您单击各个元素时显示。

我试了this.validate()this.repaint()没有运气。谁能帮我吗?

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.");
    }
 }

2 个答案:

答案 0 :(得分:1)

每次绘制小程序时,您都会添加大量控件。这意味着您添加的每个新控件都将触发至少另一个绘制操作。

paint方法中的所有代码移出到构造函数或类似代码中。

paint用于自己绘制控件的视觉效果。即你拿走提供的Graphics物品然后在它上面做东西,直到你开心为止。你通常不会修改任何其他东西,因为一般来说绘画有点不可预测。

答案 1 :(得分:1)

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

这可能会让你开始:

Heating Home

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