mKo*_*bel 5 java fonts swing resize jcombobox
在这两个岗位@iMohammad连接,
增加/减少内部使用的JButton textarea的字体大小和
上一个JButton的Java点击时更改字体样式 ......,我面临着从来到真是好笑问题JComboBox
通过传递setPrototypeDisplayValue
作为一个参数JComboBox's size
上屏幕
请问我如何JComboBox
动态调整大小,取决于Font
我在sscce中尝试的另一个JComponents的正确工作
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ComboBoxFontChange extends JFrame {
private static final long serialVersionUID = 1L;
private JComboBox cbox = new JComboBox();
private JTextField tfield = new JTextField("Change");
private JLabel label = new JLabel("Cash");
private JButton button = new JButton("++ Font");
private JTextField text;
private JPanel panel = new JPanel();
public ComboBoxFontChange() {
super("Combo Box Font change");
text = (JTextField) cbox.getEditor().getEditorComponent();
cbox.addItem("Change");
cbox.addItem("Cash");
cbox.addItem("Font");
tfield.setColumns(5);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Font font = cbox.getFont();
font = font.deriveFont((float) (font.getSize2D() * 1.10));
cbox.setFont(font);
// EDIT
cbox.setPrototypeDisplayValue(cbox.getSelectedItem().toString());
tfield.setFont(font);
button.setFont(font);
label.setFont(font);
//panel.revalidate();
//panel.repaint();
pack();
}
});
//panel.setLayout(new GridLayout(2, 2, 10, 10));
panel.add(cbox);
panel.add(label);
panel.add(tfield);
panel.add(button);
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(panel);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
ComboBoxFontChange frame = new ComboBoxFontChange();
frame.pack();
frame.setVisible(true);
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
我调试了你的SSCCE,传递给的值setPrototypeDisplayValue
是空字符串。将行更改为
cbox.setPrototypeDisplayValue(cbox.getSelectedItem());
Run Code Online (Sandbox Code Playgroud)
使一切按预期进行。删除对的调用setPrototypDisplayValue
也会使程序按预期运行。
编辑:
另一个问题是,原型显示值不会触发任何事件,因为您像以前一样将其设置为先前的值,并且仅当该值实际更改时才会触发事件。添加cbox.setPrototypeDisplayValue("");
beforecbox.setPrototypeDisplayValue(cbox.getSelectedItem().toString())
会使一切都按预期运行,即使在 JDK 1.6 上也是如此。我同意,鉴于字体已更改,应重新计算首选大小,但至少此更改是一种解决方法。