Jlist 未显示/出现在小程序中
由于某种原因,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
而列表 &滑块每次绘制时都会添加到小程序中,但单选按钮永远不会添加。
这可能会帮助您开始:
Whereas the list & slider are added to the applet every time it is painted, the radiobutton is never added.
This might get you started:
每次绘制小程序时,您都会添加大量控件。这意味着您添加的每个新控件都将至少触发另一个绘制操作。
将
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 suppliedGraphics
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.