2011-10-16 50 views
1

由於某種原因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."); 
    } 
} 

回答

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

每次繪製小程序時都會添加大量控件。這意味着您添加的每個新控件都將觸發至少另一次繪畫操作。

paint方法中的所有代碼移出到構造函數或類似方法中。

paint用於自己繪製控件的視覺效果。即你拿提供的Graphics對象,並做它的東西,直到你快樂。一般來說,你通常不會修改其他任何東西,因爲繪畫有點不可預測。