【发布时间】:2012-03-06 07:15:31
【问题描述】:
当用户选择 JComboBox 中的各种选项时,我试图让绘制的图形刷新/重新填充。我在网上找到的示例都使用了 JLabels,这对于图像文件可能很好,但不适用于 paintComponent 生成的自定义图形。
我尝试在下面大约 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));
}
}
}
【问题讨论】:
-
问题在编辑之前很好
-
您可以恢复编辑,但现在看起来可以了。
标签: java swing graphics plot jcombobox