Java符号表对话框

时间:2014-12-04 12:26:08

标签: java swing

我正在尝试创建一个Java JDialog,其中符号以表格方式显示。实际上类似于charmap for windows: enter image description here

但我遇到了一些问题:

  1. 我需要JDialog重新调整大小,所以我不能选择将表作为显示,因为它需要动态缩放。(澄清我的意思不仅仅是调整宽度每个单元格,但行/列中单元格的实际数量)

  2. 这些符号显然需要包含在某种听众友好的组件中。但是如果我选择JButton,由于像素中的字符宽度不同,一切看起来都不对。

  3. 由于我必须遍历符号才能创建JButtons,因此我只能创建没有名称的对象。那么我怎样才能找出按下哪个按钮?

  4. 这是我到目前为止所得到的:

    代码已于2014年12月5日更新

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

    ps:我使用here

    中推荐的columns_flow_layout

1 个答案:

答案 0 :(得分:0)

我会在JButton[][]中使用GridLayout。 要将它们添加到JPanel,您可以在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[][]*/)
     }
}