BoxLayout zeigt nichts



  • Cioa Zäme

    Kann mir Jemand sagen warum der Panel mit BoxLayout nicht gezeigt wird?

    /*
     * Java class source file
     * Copyright© : Haag-Streit Diagnostics
     * Created on : 21.06.2016 by robbiar
     *
     * Revision of last commit : $Rev$
     * Author of last commit : $Author$
     * Date of last commit : $Date$
     * =============================================================================
     * =
     */
    
    package swing_eins;
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyListener;
    import java.util.EventObject;
    
    import javax.swing.BorderFactory;
    import javax.swing.BoxLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    /**
     *
     * @author robbiar
     */
    public class win_start
                           extends JFrame
                           implements ActionListener {
    
      private static final KeyListener keyListener = null;
      private final JTextField         edt_res;
    
      public static void main(final String[] args) {
        // TODO Auto-generated method stub
        final win_start fenster = new win_start();
        fenster.setSize(300, 300);
        fenster.setLocation(200, 200);
        fenster.setName("programm");
        fenster.pack();
        fenster.setVisible(true);
      }
    
      public win_start() {
        super("das programm");
    
        // Resultat anzeigen
        final JPanel pn_text = new JPanel();
        final JLabel lbl_name = new JLabel("Resultat");
        pn_text.add(lbl_name);
        edt_res = new JTextField(20);
        edt_res.setToolTipText("Resultat der Berechnung");
        edt_res.addFocusListener(null);
        edt_res.addKeyListener(keyListener);
        pn_text.add(edt_res);
        pn_text.setBorder(BorderFactory.createEtchedBorder());
        getContentPane().add(pn_text, BorderLayout.NORTH);
    
        // Buttons aufbauen
        final JPanel pn_buttons = new JPanel();
        final JPanel pn_eins = new JPanel();
        final JPanel pn_zwei = new JPanel();
        final JPanel pn_drei = new JPanel();
        final JPanel pn_vier = new JPanel();
        final JButton btn_nul = new JButton("0");
        // btn_nul.setLayout(null);
        // btn_nul.setBounds(1, 500, 60, 20);
        btn_nul.setBackground(Color.yellow);
        btn_nul.addActionListener(this);
        pn_eins.add(btn_nul);
        final JButton btn_eins = new JButton("1");
        btn_eins.addActionListener(this);
        // btn_eins.setLayout(null);
        // btn_eins.setBounds(1, 550, 60, 20);
        pn_eins.add(btn_eins);
        final JButton btn_zwei = new JButton("2");
        // btn_zwei.setLayout(null);
        btn_zwei.addActionListener(this);
        pn_eins.add(btn_zwei);
        final JButton btn_drei = new JButton("3");
        // btn_drei.setLayout(null);
        btn_drei.addActionListener(this);
        pn_zwei.add(btn_drei);
        final JButton btn_vier = new JButton("4");
        // btn_vier.setLayout(null);
        btn_vier.addActionListener(this);
        pn_zwei.add(btn_vier);
        final JButton btn_fuenf = new JButton("5");
        // btn_fuenf.setLayout(null);
        btn_fuenf.addActionListener(this);
        pn_zwei.add(btn_fuenf);
        final JButton btn_sechs = new JButton("6");
        // btn_sechs.setLayout(null);
        btn_sechs.addActionListener(this);
        pn_zwei.add(btn_sechs);
        final JButton btn_sieben = new JButton("7");
        // btn_sieben.setLayout(null);
        btn_sieben.addActionListener(this);
        pn_drei.add(btn_sieben);
        final JButton btn_acht = new JButton("8");
        // btn_acht.setLayout(null);
        btn_acht.addActionListener(this);
        pn_drei.add(btn_acht);
        final JButton btn_neun = new JButton("9");
        // btn_neun.setLayout(null);
        btn_neun.addActionListener(this);
        pn_drei.add(btn_neun);
        pn_buttons.setBorder(BorderFactory.createEtchedBorder());
        getContentPane().add(pn_buttons, BorderLayout.SOUTH);
        // edt_res.setText("eingabe");
        pn_eins.setLayout(new BoxLayout(pn_eins, BoxLayout.X_AXIS));
        pn_eins.setBackground(Color.green);
        pn_eins.setToolTipText("das einser Ort");
        getContentPane().add(pn_eins);
        getContentPane().add(pn_zwei, BoxLayout.X_AXIS);
        getContentPane().add(pn_drei, BoxLayout.X_AXIS);
        getContentPane().add(pn_vier, BoxLayout.X_AXIS);
      }
    
      @Override
      public void actionPerformed(final ActionEvent event) {
        final String cmd = event.getActionCommand();
        final Object was = event.getSource();
        try {
          String name = was.toString();
          final int pos = name.indexOf("[");
          name = name.substring(0, pos);
          if (name.equals("javax.swing.JTextField")) {
            // JTextField neu <? extends Object> = was.getClass();
            ((JTextField) was).setText("ereignis");
          }
          if (name.equals("javax.swing.JButton")) {
            String text = edt_res.getText();
            text += cmd;
            edt_res.setText(text);
          }
          // PLAF-Klasse auswählen
          String plaf = "unknown";
          plaf = cmd;
          // LAF umschalten
          UIManager.setLookAndFeel(plaf);
          SwingUtilities.updateComponentTreeUI(this);
        } catch (final UnsupportedLookAndFeelException e) {
          System.err.println(e.toString());
        } catch (final ClassNotFoundException e) {
          System.err.println(e.toString());
        } catch (final InstantiationException e) {
          System.err.println(e.toString());
        } catch (final IllegalAccessException e) {
          System.err.println(e.toString());
        }
      }
    
      public void actFocusListener(final KeyListener event) {
        final Object was = ((EventObject) event).getSource();
        String name = was.toString();
        final int pos = name.indexOf("[");
        name = name.substring(0, pos);
        if (was.equals("javax.swing.JTextField")) {
          // JTextField neu <? extends Object> = was.getClass();
          ((JTextField) was).setText("focus!");
        }
      }
    
    }
    

    Danke für eure Hilfe.

    Gruss Renato


Anmelden zum Antworten