import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; public class DecorationAuto extends JFrame implements ActionListener { JRadioButton rbLimousine = new JRadioButton("Limousine", true); JRadioButton rbCabrio = new JRadioButton("Cabrio", false); JRadioButton rbKombi = new JRadioButton("Kombi", false); JTextArea editor = new JTextArea(); JCheckBox chkSound = new JCheckBox("Soundanlage"); JCheckBox chkNavi = new JCheckBox("Navi/Touchanzeige"); JCheckBox chkLicht = new JCheckBox("Halogen/Nebelscheinwerfer"); JCheckBox chkAnhaengerkupplung = new JCheckBox("Anhängerkupplung"); JRadioButton rbBenzin = new JRadioButton("Benzin", true); JRadioButton rbDiesel = new JRadioButton("Diesel", false); JRadioButton rbHybrid = new JRadioButton("Hybrid", false); JRadioButton rbElektro = new JRadioButton("Elektro", false); // Konstruktor public DecorationAuto() { this.setSize( 780, 660); setLocation(400,10); setTitle( "DecorationAuto"); setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); setGUI(); calc(); } /** * Aufbau der GUI-Elemente. * Einfuegen der "desktopPane" in CENTER. */ public void setGUI() { // BorderLayout setzen this.getContentPane().setLayout( new GridBagLayout() ); //JPanel panelBn = new JPanel(); //panelBn.setLayout( new FlowLayout(FlowLayout.RIGHT)); ButtonGroup group1 = new ButtonGroup(); group1.add(rbLimousine); group1.add(rbCabrio); group1.add(rbKombi); JPanel panelPKW = new JPanel(); panelPKW.setLayout( new GridLayout(3,1) ); TitledBorder tb = new TitledBorder( LineBorder.createBlackLineBorder(), "Auswahl des Typs"); panelPKW.setBorder(tb); panelPKW.add(rbLimousine); panelPKW.add(rbCabrio); panelPKW.add(rbKombi); JLabel labelMain = new JLabel("DecorationAuto"); getContentPane().add(labelMain, new GridBagConstraints( 0, 0, 2, 1, 0.0, 0.0, // gridx, gridy, gridwidth, gridheight, weightx, weighty GridBagConstraints.WEST, // anchor GridBagConstraints.NONE, // HORIZONTAL, VERTICAL, BOTH new Insets(20,20,30,0), 0, 0)); // Top, left, bottom, right getContentPane().add(new JLabel("Hauptwahl:"), new GridBagConstraints( 0, 1, 1, 1, 50.0, 0.0, // gridx, gridy, gridwidth, gridheight, weightx, weighty GridBagConstraints.WEST, // anchor GridBagConstraints.HORIZONTAL, // HORIZONTAL, VERTICAL, BOTH new Insets(20,20,0,0), 0, 0)); // Top, left, bottom, right getContentPane().add(panelPKW, new GridBagConstraints( 0, 2, 1, 1, 0.0, 0.0, // gridx, gridy, gridwidth, gridheight, weightx, weighty GridBagConstraints.NORTHWEST, // anchor GridBagConstraints.NONE, // HORIZONTAL, VERTICAL, BOTH new Insets(20,20,0,20), 70, 0)); // Top, left, bottom, right JPanel panelZubehoer1 = new JPanel(); panelZubehoer1.setLayout( new GridBagLayout() ); //panelZubehoer.setBackground(Color.yellow); JPanel panelZubehoer2 = new JPanel(); panelZubehoer2.setLayout( new GridBagLayout() ); getContentPane().add(new JLabel("Standardauswahl:"), new GridBagConstraints( 1, 1, 1, 1, 50.0, 0.0, // gridx, gridy, gridwidth, gridheight, weightx, weighty GridBagConstraints.WEST, // anchor GridBagConstraints.HORIZONTAL, // HORIZONTAL, VERTICAL, BOTH new Insets(20,20,0,0), 0, 0)); // Top, left, bottom, right getContentPane().add(panelZubehoer1, new GridBagConstraints( 1, 2, 1, 2, 0.0, 0.0, // gridx, gridy, gridwidth, gridheight, weightx, weighty GridBagConstraints.NORTHWEST, // anchor GridBagConstraints.NONE, // HORIZONTAL, VERTICAL, BOTH new Insets(20,20,0,0), 0, 0)); // Top, left, bottom, right ButtonGroup group2 = new ButtonGroup(); group2.add(rbBenzin); group2.add(rbDiesel); group2.add(rbHybrid); group2.add(rbElektro); JPanel panelAntrieb= new JPanel(); panelAntrieb.setLayout( new GridLayout(4,1) ); TitledBorder tb2 = new TitledBorder( LineBorder.createBlackLineBorder(), "Antrieb"); panelAntrieb.setBorder(tb2); panelAntrieb.add(rbBenzin); panelAntrieb.add(rbDiesel); panelAntrieb.add(rbHybrid); panelAntrieb.add(rbElektro); panelZubehoer1.add(panelAntrieb, new GridBagConstraints( 0, 4, 1, 1, 0.0, 0.0, // gridx, gridy, gridwidth, gridheight, weightx, weighty GridBagConstraints.WEST, // anchor GridBagConstraints.NONE, // HORIZONTAL, VERTICAL, BOTH new Insets(0,0,0,0), 60, 0)); // Top, left, bottom, right getContentPane().add(new JLabel("Optionales Zubehör:"), new GridBagConstraints( 2, 1, 1, 1, 50.0, 0.0, // gridx, gridy, gridwidth, gridheight, weightx, weighty GridBagConstraints.WEST, // anchor GridBagConstraints.HORIZONTAL, // HORIZONTAL, VERTICAL, BOTH new Insets(20,20,0,0), 0, 0)); // Top, left, bottom, right getContentPane().add(panelZubehoer2, new GridBagConstraints( 2, 2, 1, 2, 0.0, 0.0, // gridx, gridy, gridwidth, gridheight, weightx, weighty GridBagConstraints.NORTHWEST, // anchor GridBagConstraints.NONE, // HORIZONTAL, VERTICAL, BOTH new Insets(20,20,0,0), 0, 0)); // Top, left, bottom, right panelZubehoer2.add(chkSound, new GridBagConstraints( 0, 0, 1, 1, 0.0, 0.0, // gridx, gridy, gridwidth, gridheight, weightx, weighty GridBagConstraints.WEST, // anchor GridBagConstraints.NONE, // HORIZONTAL, VERTICAL, BOTH new Insets(0,0,0,0), 0, 0)); // Top, left, bottom, right panelZubehoer2.add(chkNavi, new GridBagConstraints( 0, 1, 1, 1, 0.0, 0.0, // gridx, gridy, gridwidth, gridheight, weightx, weighty GridBagConstraints.WEST, // anchor GridBagConstraints.NONE, // HORIZONTAL, VERTICAL, BOTH new Insets(10,0,0,0), 0, 0)); // Top, left, bottom, right panelZubehoer2.add(chkLicht, new GridBagConstraints( 0, 2, 1, 1, 0.0, 0.0, // gridx, gridy, gridwidth, gridheight, weightx, weighty GridBagConstraints.WEST, // anchor GridBagConstraints.NONE, // HORIZONTAL, VERTICAL, BOTH new Insets(10,0,0,0), 0, 0)); // Top, left, bottom, right panelZubehoer2.add(chkAnhaengerkupplung, new GridBagConstraints( 0, 3, 1, 1, 0.0, 0.0, // gridx, gridy, gridwidth, gridheight, weightx, weighty GridBagConstraints.WEST, // anchor GridBagConstraints.NONE, // HORIZONTAL, VERTICAL, BOTH new Insets(10,0,0,0), 0, 0)); // Top, left, bottom, right getContentPane().add(new JLabel("Ergebnis"), new GridBagConstraints( 0, 4, 3, 1, 0.0, 0.0, // gridx, gridy, gridwidth, gridheight, weightx, weighty GridBagConstraints.NORTHWEST, // anchor GridBagConstraints.NONE, // HORIZONTAL, VERTICAL, BOTH new Insets(20,20,0,20), 0, 0)); // Top, left, bottom, right getContentPane().add(new JScrollPane(editor), new GridBagConstraints( 0, 5, 3, 1, 100.0, 100.0, // gridx, gridy, gridwidth, gridheight, weightx, weighty GridBagConstraints.NORTHWEST, // anchor GridBagConstraints.BOTH, // HORIZONTAL, VERTICAL, BOTH new Insets(5,20,20,20), 0, 0)); // Top, left, bottom, right setFonts(getContentPane(), 18); labelMain.setFont(new Font("Arial", Font.BOLD, 24)); labelMain.setForeground(Color.BLUE); tb.setTitleFont(new Font("Arial", Font.BOLD, 18)); tb2.setTitleFont(new Font("Arial", Font.BOLD, 18)); editor.setFont(new Font("Arial", Font.BOLD, 18)); rbLimousine.addActionListener(this); rbCabrio.addActionListener(this); rbKombi.addActionListener(this); chkSound.addActionListener(this); chkNavi.addActionListener(this); chkLicht.addActionListener(this); chkAnhaengerkupplung.addActionListener(this); rbBenzin.addActionListener(this); rbDiesel.addActionListener(this); rbHybrid.addActionListener(this); rbElektro.addActionListener(this); } // setGUI private void setFonts(Container cont, int size) { for (int i = 0; i < cont.getComponentCount(); i++) { Component c = cont.getComponent(i); if (c instanceof JPanel) setFonts((JPanel) c, size); else c.setFont(new Font("Arial", Font.BOLD, size)); } } // baut die Decorator-Hierachie auf private void calc() { // hier darf nur eine Variable deklariert werden // zum Beispiel: Auto a; editor.append("in calc\n"); if (rbLimousine.isSelected()) { // hier fehlt Code } // Den aktuellen Preis in den Editor eintragen (setText) // Die Beschreibung in den Editor eintragen (append) } // calc @Override public void actionPerformed(ActionEvent e) { calc(); } public static void main(String[] args) { DecorationAuto frame = new DecorationAuto(); frame.setVisible(true); } }