Java符号表对话框
我正在尝试制作一个JavaJDialog,其中符号以表格的方式显示。事实上,类似于windows的charmap: 但我遇到了一些问题:Java符号表对话框,java,swing,Java,Swing,我正在尝试制作一个JavaJDialog,其中符号以表格的方式显示。事实上,类似于windows的charmap: 但我遇到了一些问题: 我需要重新调整JDialog的大小,所以我不能选择将表作为显示,因为它需要动态缩放。(澄清一下,我指的不仅仅是调整每个单元格的宽度,而是调整行/列中单元格的实际数量) 这些符号显然需要包装在某种侦听器友好的组件中。但如果我选择JButtons,一切看起来都错了,因为像素中的字符宽度不同 因为我必须通过循环符号来创建jbutton,所以我只能创建没有名称的对象
import java.awt.Dimension;
import javax.swing.JDialog;
import javax.swing.JScrollPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
public class SymbolDialog extends JDialog{
private char[] math = {8704, 8707, 8708, 8710, 8712, 8713, 8715, 8716, 8721, 8723, 8728, 8730, 8734, 8743, 8744, 8745, 8746, 8747, 8776, 8793, 8800, 8801, 8804, 8805, 8834, 8835, 8836, 8837, 8838, 8839};
private final String[] sTypes = {"Math","Logic", "Arrows"};
private JPanel pan = new JPanel();
public SymbolDialog(){
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
System.out.println("catdch 1");
} catch (InstantiationException e1) {
System.out.println("catdch 2");
} catch (IllegalAccessException e1) {
System.out.println("catdch 3");
} catch (UnsupportedLookAndFeelException e1) {
System.out.println("catdch 4");
}
SwingUtilities.updateComponentTreeUI(this);
pan.setPreferredSize(new Dimension(428, 70));
pan.setLayout(new ColumnsFlowLayout(0,0));
// Button Build
for (int i = 0; i < math.length; i++){
pan.add(new JButton(math[i]+""));
}
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{0, 0};
gridBagLayout.rowHeights = new int[]{0, 0, 0};
gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
getContentPane().setLayout(gridBagLayout);
JPanel panel = new JPanel();
panel.setBorder(new EmptyBorder(5,10,0,10));
GridBagConstraints gbc_panel = new GridBagConstraints();
gbc_panel.anchor = GridBagConstraints.WEST;
gbc_panel.fill = GridBagConstraints.VERTICAL;
gbc_panel.insets = new Insets(0, 0, 5, 0);
gbc_panel.gridx = 0;
gbc_panel.gridy = 0;
getContentPane().add(panel, gbc_panel);
GridBagLayout gbl_panel = new GridBagLayout();
gbl_panel.columnWidths = new int[]{0, 0, 0};
gbl_panel.rowHeights = new int[]{0, 0};
gbl_panel.columnWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
gbl_panel.rowWeights = new double[]{0.0, Double.MIN_VALUE};
panel.setLayout(gbl_panel);
JLabel lblCathegorie = new JLabel("Categories:");
GridBagConstraints gbc_lblCathegorie = new GridBagConstraints();
gbc_lblCathegorie.insets = new Insets(0, 0, 0, 5);
gbc_lblCathegorie.gridx = 0;
gbc_lblCathegorie.gridy = 0;
panel.add(lblCathegorie, gbc_lblCathegorie);
JComboBox<Object> comboBox = new JComboBox<Object>(sTypes);
GridBagConstraints gbc_comboBox = new GridBagConstraints();
gbc_comboBox.gridx = 1;
gbc_comboBox.gridy = 0;
panel.add(comboBox, gbc_comboBox);
GridBagConstraints gbc_scrollPane = new GridBagConstraints();
gbc_scrollPane.fill = GridBagConstraints.BOTH;
gbc_scrollPane.gridx = 0;
gbc_scrollPane.gridy = 1;
getContentPane().add(pan, gbc_scrollPane);
// Pack
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setResizable(true);
pack();
setTitle("Symbols");
setVisible(true);
}
public void actionPerformed(ActionEvent event) {
//TODO sysout which symbol was clicked
System.out.println(((JButton) event.getSource()).getText());
}
public static void main(String[] args){
new SymbolDialog();
}
}
导入java.awt.Dimension;
导入javax.swing.JDialog;
导入javax.swing.JScrollPane;
导入javax.swing.JPanel;
导入javax.swing.SwingUtilities;
导入javax.swing.UIManager;
导入javax.swing.UnsupportedLookAndFeelException;
导入java.awt.FlowLayout;
导入java.awt.event.ActionEvent;
导入javax.swing.JButton;
导入javax.swing.JComboBox;
导入javax.swing.JLabel;
导入javax.swing.border.EmptyBorder;
导入java.awt.Color;
导入java.awt.GridBagLayout;
导入java.awt.GridBagConstraints;
导入java.awt.Insets;
公共类SymbolDialog扩展了JDialog{
私有字符[]数学={8704、8707、8708、8710、8712、8713、8715、8716、8721、8723、8728、8730、8734、8743、8744、8745、8746、8747、8776、8793、8800、8801、8804、8805、8834、8835、8836、8837、8838、8839};
私有最终字符串[]sTypes={“数学”、“逻辑”、“箭头”};
private JPanel pan=new JPanel();
公共SymbolDialog(){
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(classnotfounde异常){
System.out.println(“catdch 1”);
}捕获(实例化异常e1){
System.out.println(“catdch 2”);
}捕获(IllegalacessException e1){
System.out.println(“catdch 3”);
}捕获(不受支持的LookandFeelException e1){
System.out.println(“catdch 4”);
}
SwingUtilities.updateComponentTreeUI(此);
平移设置首选尺寸(新尺寸(428,70));
平移设置布局(新柱布局(0,0));
//按钮构建
for(int i=0;i
注:我使用了
网格布局中的JButton[][
中推荐的列\u flow\u布局。
要将它们添加到JPanel
,可以在For循环中使用For,如下所示:
for(int row = 0; row < ITEMS-PER-ROW; row++){
for(int col = 0; col < ITEMS-PER-COL; col++){
JButton[row][col] = new JButton(/*SignToShowAs String[][]*/)
}
}
for(int row=0;row
我会使用JTable来显示字符。我使用了JLabel
的GridLayout
以及相关的工具提示。@StanislavL但是如果我要调整对话框的大小,我如何调整列数等?您可以尝试无间隙的布局。它应该可以解决在调整大小时重新排列列的问题。你是说类似于?使用根据帧大小调整大小的JTable
。