Jlist 未显示/出现在小程序中

发布于 2024-12-10 11:46:49 字数 1663 浏览 1 评论 0原文

由于某种原因,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.");
    }
 }

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦回梦里 2024-12-17 11:46:49

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

这可能会帮助您开始:

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

Whereas the list & slider are added to the applet every time it is painted, the radiobutton is never added.

This might get you started:

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();
    }
}
甜中书 2024-12-17 11:46:49

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

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

paint 用于自己绘制控件的视觉效果。即,您获取提供的 Graphics 对象并对其进行操作,直到您满意为止。您通常不会修改任何其他内容,因为绘画通常有点不可预测。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文