开发者

Draw line shape with length and send length value to JTable

I created three classes: one is Gui class it includes JTable and JButton.

public class Gui {
    public void Gui() {
        JFrame frame = new JFrame();
        String data[][] = {};
        String column[] = {"Bar Mark", "Length"};
        JTable table = new JTable(data, column);
        table.setBounds(0, 0, 500, 500);
        JScrollPane pane = new JScrollPane(table);
        frame.getContentPane().add(BorderLayout.NORTH, pane);
        JButton button = new JButton("Create Rebar");
        frame.getContentPane().add(BorderLayout.SOUTH, button);
        frame.setSize(500, 500);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                CreateRebar createRebar = new CreateRebar();
                createRebar.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
                JDialog bDialog = new JDialog();
                bDialog.setTitle("Create Rebar");
                bDialog.setPreferredSize(new Dimension(500, 300));
                bDialog.getContentPane().setLayout(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.insets = new Insets(5, 5, 5, 5);
                gbc.fill = GridBagConstraints.BOTH;
                gbc.weightx = 0.1;
                gbc.weighty = 0.1;
                bDialog.add(createRebar, gbc);
                bDialog.setModal(true);
                bDialog.pack();
                bDialog.setLocationRelativeTo(null);
                bDialog.setVisible(true);
            }
        });
    }

    public static void main(String[] args) {
        Gui gui = new Gui();
        gui.Gui();
    }
}

CreateRebar is another class and it collects data to send to JTable.

public class CreateRebar extends JPanel {
    private ShapeTwenty shapeTwenty = new ShapeTwenty();
    String[] diaSizes = {"20", "32"};
    JComboBox cmbDia = new JComboBox(diaSizes);
    JTextField txtMark = new JTextField(3);
    JLabel lblMark = new JLabel("Add Bar Mark");
    JLabel lblLength = new JLabel("Select Shape");
    JButton addBar = new JButton("Add Rebar");
    public CreateRebar() {
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.weightx = 0.1;
        gbc.weighty = 0.1;
        gbc.insets = new Insets(5, 5, 5, 5);
        add(lblMark, gbc);
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.weightx = 0.1;
        gbc.weighty = 0.1;
        gbc.insets = new Insets(5, 5, 5, 5);
        add(lblLength, gbc);
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.weightx = 0.1;
        gbc.weighty = 0.1;
        gbc.insets = new Insets(5, 5, 5, 5);
        add(txtMark, gbc);
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.weightx = 0.1;
        gbc.weighty = 0.1;
        gbc.insets = new Insets(5, 5, 5, 5);
        add(cmbDia, gbc);
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.weightx = 0.1;
        gbc.weighty = 0.1;
        gbc.insets = new Insets(5, 5, 5, 5);
        add(addBar, gbc);
        cmbDia.addActionListener(new ActionListener() {
           
            public void actionPerformed(ActionEvent e) {
                String type = (String) cmbDia.getSelectedItem();
                if (type.equals("20")) {
                    shapeTwenty.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
                    JDialog sDialog = new JDialog();
  开发者_如何学编程                  sDialog.setTitle("Shape 20");
                    sDialog.setPreferredSize(new Dimension(230, 150));
                    sDialog.getContentPane().setLayout(new GridBagLayout());
                    GridBagConstraints gbc = new GridBagConstraints();
                    gbc.insets = new Insets(5, 5, 5, 5);
                    gbc.fill = GridBagConstraints.BOTH;
                    gbc.weightx = 0.1;
                    gbc.weighty = 0.1;
                    sDialog.add(shapeTwenty, gbc);
                    sDialog.setModal(true);
                    sDialog.pack();
                    sDialog.setLocationRelativeTo(null);
                    sDialog.setVisible(true);
                } else if (type.equals("32")) {
                }
            }
        });
        addBar.addActionListener(new ActionListener() {
            
            public void actionPerformed(ActionEvent e) {
                ShapeTwenty shapeTwenty = new ShapeTwenty();
                String obj = shapeTwenty.getTxtLength().getText();
                if (txtMark.getText()
                        .isEmpty()) {
                    JOptionPane.showMessageDialog(null, "Add Bar Mark");
                } else {
                    String mark = (String) cmbDia.getSelectedItem();
                    if (mark.equals("20")) {
                        String barMark = txtMark.getText();
                        Object[] row = {barMark, obj};
                        AppUI.addRebar(row);
                    } else if (mark.equals("32")) {
                    }
                }
            }
        });
    }
}

ShapeTwenty is another class and it includes line and JTextField.

public class ShapeTwenty extends JPanel {
    private JTextField txtLength = new JTextField(3);

    public ShapeTwenty() {
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        setBorder(BorderFactory.createTitledBorder("20"));
        this.add(txtLength);
        add(txtLength, gbc);
        txtLength.setText("A");
        txtLength.setHorizontalAlignment(SwingConstants.CENTER);
        txtLength.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Window w = SwingUtilities.getWindowAncestor(ShapeTwenty.this);
                w.setVisible(false);
            }
        });
    }

    public JTextField getTxtLength() {
        return txtLength;
    }

    public void setTxtLength(JTextField txtLength) {
        this.txtLength = txtLength;
    }

    @Override
    public Dimension getPreferredSize() {
        return (new Dimension(300, 200));
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setRenderingHints(rh);
        g2.setColor(Color.black);
        g2.setStroke(new BasicStroke(2));
        g2.drawLine(20, 65, 170, 65);
    }
}

Draw line shape with length and send length value to JTable

Draw line shape with length and send length value to JTable

Draw line shape with length and send length value to JTable

Draw line shape with length and send length value to JTable

When Project is running Gui class is open and click on Create Rebar Button, Create Rebar open. After adding Bar mark as "01" and select Shape 20 in cmbDia, ShapeTwenty open but "txtLength" JTextField is not visible. I supposed to send Bar mark and length to JTable in Gui class. But due to invisible JTextField my attempt is unsuccessful.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜