【发布时间】:2012-01-30 13:17:35
【问题描述】:
与@iMohammad 这两个帖子有关,
Increasing/Decreasing Font Size inside textArea using JButton 和
Changing Font Style when Clicking on a 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);
}
});
}
}
【问题讨论】:
标签: java swing fonts resize jcombobox