我想要繪製圖形刷新/重新填充當用戶選擇JComboBox中的各種選項中的每一個。我在網上找到的例子都使用JLabels,這對圖像文件來說可能很好,但對於paintComponent生成的自定義圖形不起作用。從JComboBox控件重繪
我試着在下面的~60行代碼中滾動我自己的解決方案。我正在使用一個矩形的簡單示例來重新調整大小。如果您編譯並運行下面的代碼,您將看到當用戶從JComboBox中選擇不同的選項時,它不會重新繪製。此外,我故意沒有對displayConstraints做任何事情,因爲如果某人有更好的方法,我不想強加一個解決方案。我的目標是將JComboBox顯示在頂部的自己的行中,並將繪製的圖形顯示在第一行下面的更大的第二行中。第二行將吸收所有調整大小的更改,而第一行在調整JFrame大小時將保持大致相同的大小。通過從JComboBox中選擇不同的選項,用戶將能夠使繪製的矩形相對於JFrame的當前大小變得更小或更大。
任何人都可以告訴我如何解決下面的代碼,以便它實現我的上述目標?
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class ComboBox extends JFrame implements ItemListener {
final String[] sizes = { "10%", "20%", "33%" };
JComboBox combobox = new JComboBox(sizes);
int selectedIndex;
public ComboBox() {
setLayout(new GridBagLayout());
combobox.setSelectedIndex(-1);
combobox.addItemListener(this);
GridBagConstraints comboBoxConstraints = new GridBagConstraints();
comboBoxConstraints.gridx = 0;
comboBoxConstraints.gridy = 0;
comboBoxConstraints.gridwidth = 1;
comboBoxConstraints.gridheight = 1;
comboBoxConstraints.fill = GridBagConstraints.NONE;
add(combobox,comboBoxConstraints);//This should be placed at top, in middle.
GridBagConstraints displayConstraints = new GridBagConstraints();
displayConstraints.gridx = 0;
displayConstraints.gridy = 1;
displayConstraints.gridwidth = 1;
displayConstraints.gridheight = 1;
displayConstraints.fill = GridBagConstraints.BOTH;
//I am aware that nothing is done with displayConstraints.
//I just want to indicate that the rectangle should go below the combobox,
//and that the rectangle should resize while the combobox should not.
//Other suggested approaches are welcome.
setSize(300, 300);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {new ComboBox();}
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
JComboBox combo = (JComboBox) e.getSource();
selectedIndex = combo.getSelectedIndex();
System.out.println("selectedIndex is: "+selectedIndex);
repaint();
}
}
protected void paintComponent(Graphics g){
int scaleFactor = 1;
if(selectedIndex==0){scaleFactor = 10;}
if(selectedIndex==1){scaleFactor = 5;}
if(selectedIndex==2){scaleFactor = 3;}
if(selectedIndex!=-1){
int xStart = (getWidth()/2)-(getWidth()/scaleFactor);
int yStart = (getHeight()/2)-(getHeight()/scaleFactor);
g.drawRect(xStart, yStart, (getWidth()/scaleFactor), (getHeight()/scaleFactor));
}
}
}
問題在編輯之前是好的 – CodeMed 2012-03-05 23:48:08
您可以恢復編輯,但現在看起來好了。 – trashgod 2012-03-06 02:18:24