gpt4 book ai didi

java - 从 JComboBox 控件重绘

转载 作者:行者123 更新时间:2023-12-04 05:55:16 25 4
gpt4 key购买 nike

当用户选择 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));
}
}
}

最佳答案

您不应该直接在 JFrame 中绘制,即使您这样做了,JFrame 也没有 PaintComponent 方法。使用 @Override注释自己看。相反,在 JPanel 或 JComponent 中绘制,在paintComponent 中进行绘制,并确保您使用覆盖注释正确覆盖了该方法。

例如:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ComboBoxTest extends JPanel implements ItemListener {
private static final int PREF_W = 300;
private static final int PREF_H = PREF_W;
final String[] sizes = { "10%", "20%", "33%" };
JComboBox combobox = new JComboBox(sizes);
int selectedIndex;
private double scaleFactor = 1;

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

@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}

public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
JComboBox combo = (JComboBox) e.getSource();
selectedIndex = combo.getSelectedIndex();
System.out.println("selectedIndex is: " + selectedIndex);
if (selectedIndex == -1) {
return;
}
String selectedItem = combo.getSelectedItem().toString().trim();
selectedItem = selectedItem.replace("%", "");
scaleFactor = Double.parseDouble(selectedItem) / 100.0;
repaint();
}
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int xStart = (getWidth() / 2) - (int)(getWidth() * scaleFactor);
int yStart = (getHeight() / 2) - (int)(getHeight() * scaleFactor);
g.drawRect(xStart, yStart, (int)(getWidth() * scaleFactor),
(int)(getHeight() * scaleFactor));
}

private static void createAndShowGui() {
JFrame frame = new JFrame("ComboBoxTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new ComboBoxTest());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

关于java - 从 JComboBox 控件重绘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9575306/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com