gpt4 book ai didi

java - JComboBox getSelectedIndex() 方法

转载 作者:行者123 更新时间:2023-11-29 10:18:18 27 4
gpt4 key购买 nike

我正在制作一个简单的文本编辑器,您可以在其中设置字体样式、字体大小、全部清除等。为了设置字体大小,我添加了 JComboBox 并实现了 ItemListener。这是我的主窗口类:

import javax.swing.*;

public class MainWindow extends JFrame{
Editor e = new Editor();

public MainWindow(){
super(".:My Text Editor:.");
getContentPane().add(e);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}

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

}

}

这是我的编辑类:

import javax.swing.*;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

public class Editor extends JPanel{
JPanel optionPanel = new JPanel();
JTextArea editArea = new JTextArea();
JButton boldBtn = new JButton("Bold");
JButton italicBtn = new JButton("Italic");
JButton plainBtn = new JButton("Plain");
JButton clearBtn = new JButton("Clear all");
String [] fontSizes = {"10","11","12","13","14","15","16","17","18","19","20"};
int fontSize;
JComboBox combo = new JComboBox(fontSizes);

public Editor(){
createUI();
addEvents();
}

public void createUI(){
optionPanel.add(boldBtn);
optionPanel.add(italicBtn);
optionPanel.add(plainBtn);
optionPanel.add(combo);
optionPanel.add(clearBtn);

setLayout(new BorderLayout());
add(optionPanel,BorderLayout.NORTH);
add(new JScrollPane(editArea),BorderLayout.CENTER);
setPreferredSize(new Dimension(640,480));
}

public void addEvents(){

boldBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
editArea.setFont(new Font("Sans Serif",Font.BOLD,fontSize));
}
});

italicBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
editArea.setFont(new Font("Sans Serif",Font.ITALIC,fontSize));

}
});

plainBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
editArea.setFont(new Font("Sans Serif",Font.PLAIN,fontSize));
}
});

combo.addItemListener(new ItemListener(){

public void itemStateChanged(ItemEvent e){
int ind = combo.getSelectedIndex();
System.out.println(ind);
fontSize = Integer.parseInt(fontSizes[ind]);
editArea.setFont(new Font("Sans Serif",Font.PLAIN,fontSize));
}
});

clearBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
editArea.setText("");
}
});
}

}

现在,奇怪的是当我把 System.out.println(ind);行只是为了查看 getSelectedIndex() 方法返回给我的索引。根据我点击的项目,它返回给我:

1
1
0
0
2
2
3
3

为什么会这样?不应该只返回 1 0 2 3 吗?提前致谢。

最佳答案

JCombobox 为您使用 ItemEvent.getStateChanged() 区分的 SELECTED 和 DESELECTED 触发 itemStateChanged 两次。因此,将您的代码包装在 if 中,如下所示:

public void itemStateChanged( ItemEvent event ) {
if( event.getStateChanged() == ItemEvent.SELECTED ) {
// code here
}
}

关于java - JComboBox getSelectedIndex() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12081696/

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