JRadioButton.isSelected() Problem

plammy

Bekanntes Mitglied
Hallo 🙂
Ich benutze zum ersten mal und bin schon auf ein Problem gestoßen .. 🙁
Ich möchte, dass je nach ausgewählten JRadioButton Rechtecke in verschiedenen Farben gezeichnet werden... Es klappt aber immer nur die Farbe die Standartmäßig auf true gesetzt ist obwohl ich die Übreprüfung mache, was grade selected ist.. Wo kann denn der Fehler liegen?

Java:
public class EditPanel extends JPanel
{

 public  EditPanel()
  {
 protected void paintComponent(Graphics g) 
    {
        super.paintComponent(g);
        
        if(CreateFile.cf.onlyborderRect.isSelected()){
            borderrect(g);
        }
        if(CreateFile.cf.filledRect.isSelected()){
            filledrect(g);
        }


    public void borderrect (Graphics g){
         //baustein     
            Graphics2D g2d = (Graphics2D) g;
         
                g2d.setPaint(Color.LIGHT_GRAY);
                g2d.setStroke(new BasicStroke(2.0f));

                Rectangle2D rec = new Rectangle2D.Double(x, y,CreateFile.cf.bausteinW,CreateFile.cf.bausteinH);

                rectangles.add(rec);
                for (Rectangle2D r : rectangles) {
                    g2d.draw(r);
                }
            
    }
    
    public void filledrect (Graphics g) {
         
           Graphics2D g2d = (Graphics2D) g;
           
                g2d.setPaint(orangeRect);
 
                Rectangle2D rec = new Rectangle2D.Double(x, y,CreateFile.cf.bausteinW,CreateFile.cf.bausteinH);

                rectangles.add(rec);
                for (Rectangle2D r : rectangles) {
                    g2d.fill(r);
                }     
    }
    
    private List<Rectangle2D> rectangles = new ArrayList<>();


Java:
public class CreateFile extends JPanel implements ActionListener
{
 public CreateFile()
    {
        initComponents();     
    
    }
    

    public void initComponents() 
    {
editPanel = new EditPanel();
.........
 //ButtonGroup
        buttonGroup = new ButtonGroup();
        
        //RadioButtons
        onlyborderRect = new JRadioButton();
        filledRect = new JRadioButton();
        
buttonGroup.add(onlyborderRect);   
        onlyborderRect.setText("leer");
        onlyborderRect.setSelected(false);
        onlyborderRect.setForeground(Color.white);
 
        
        buttonGroup.add(filledRect);
        filledRect.setText("gefüllt");
        filledRect.setSelected(true);
        filledRect.setForeground(Color.white);
      
       
...........

    @Override
    public void actionPerformed(ActionEvent object) 
    {
  if (object.getSource() == buttonIndividualForms)
        {   
           cf.bausteinW = Integer.parseInt(bausteinWidth.getText());    //Daten auslesen
           cf.bausteinH = Integer.parseInt(bausteinHeight.getText());   //Daten auslesen
           repaint();
        } 
    }
..........
 
Uhm. Zuerst einmal fehlt ein ItemListener an den RadioButtons oder? Sodass du nach einem Klick darauf direkt neuzeichnen kannst.

Dann ist der Fehler vielleicht auch woanders, denn in deinen borderrect() und filledrect() Methoden werden immer neue Rechtecke einer Liste angefügt und all diese gezeichnet. Kann es nicht sein, dass hier die gefüllten Rechtecke die alten einfach übermalen? Vielleicht soltlen die alten Rechtecke vorher entfernt werden.
 
die sollten aber nicht entfernt werden.. also es sollen mehrere rechtecke gezeichnet werden können nacheinander und später auch verschobe werden können
 
hier ein vollständiges Testprogramm, welches zweifelsfrei belegt, dass aus deinen begrenzten Codeausschnitten nur auf korrektes Verhalten der RadioButtons geschlossen werden kann,
denn es geht doch sicher allein darum welches if erfüllt ist,
Spezialitäten wie unterschiedliches Verhalten pro Rectangle erwartest du sicher noch nicht von deinem Code

Java:
public class TestGUI  extends JFrame {

    public TestGUI()  {
        add(CreateFile.cf);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 400);
        setVisible(true);
    }

    public static void main(String[] args)  {
        new TestGUI();
    }

    static class CreateFile    extends JPanel    implements ActionListener   {
        static CreateFile cf = new CreateFile();

        JRadioButton onlyborderRect;
        JRadioButton filledRect;

        public CreateFile()   {
            initComponents();
        }

        public void initComponents()     {
            EditPanel editPanel = new EditPanel();
            // ButtonGroup
            ButtonGroup buttonGroup = new ButtonGroup();

            // RadioButtons
            onlyborderRect = new JRadioButton();
            filledRect = new JRadioButton();

            buttonGroup.add(onlyborderRect);
            onlyborderRect.setText("leer");
            onlyborderRect.setSelected(false);
            // onlyborderRect.setForeground(Color.white);
            add(onlyborderRect);
            onlyborderRect.addActionListener(this);

            buttonGroup.add(filledRect);
            filledRect.setText("gefüllt");
            filledRect.setSelected(true);
            // filledRect.setForeground(Color.white);
            add(filledRect);
            filledRect.addActionListener(this);
            add(editPanel);
        }

        public void actionPerformed(ActionEvent object)   {
            repaint();
        }
    }

    static class EditPanel
        extends JPanel
    {
        protected void paintComponent(Graphics g)    {
            super.paintComponent(g);

            if (CreateFile.cf.onlyborderRect.isSelected())      {
                System.out.println("onlyborderRect");
            }
            if (CreateFile.cf.filledRect.isSelected())    {
                System.out.println("filledRect");
            }
        }
    }
}

merke dir diesen Tag und dieses Vorgehen, denn ich persönlich werde von nun an von dir immer vollständige Testprogramme erwarten,
falls ich nerve kannst du natürlich bescheid sagen, dann störe ich nicht 😉
 
hmm...geht trotzdem nicht.. es wird immer noch filledRect ausgeführt, egal was ich markiert habe...

hier vollständiges Code.. (Teil 1 - Klasse CreateFile)

Java:
public class CreateFile extends JPanel implements ActionListener
{
    static CreateFile cf = new CreateFile();
   
    public CreateFile()
    {
        initComponents();        
    }
    
    public void initComponents() 
    {

        //Panels 
        tabbedPane = new JTabbedPane();
        
        editPanel = new EditPanel();
        editPanel.setPreferredSize(new Dimension(1200,1200));

        scrollPane=new JScrollPane();
        editPanel.setAutoscrolls(true);
        scrollPane.setPreferredSize(new Dimension(600,600));
      
        websitePanel = new JPanel();    
        layoutPanel = new JPanel();
        fixFormsPanel = new JPanel(); 
        individualFormsPanel = new JPanel();
   
        //Labels
        image_label = new JLabel();
        website = new JLabel();
        logo = new JLabel();
        header = new JLabel();
        content = new JLabel();
        footer = new JLabel();
        tile1 = new JLabel();
        tile2 = new JLabel();
        tile3 = new JLabel();
        baustein = new JLabel();
        hinweis = new JLabel();

        //ButtonGroup
        buttonGroup = new ButtonGroup();
        
        //RadioButtons
        onlyborderRect = new JRadioButton();
        filledRect = new JRadioButton();
        
        //Textfelder
        websiteTextField = new JTextField();
        headerWidth = new JTextField();
        headerHeight = new JTextField();
        contentWidth = new JTextField();
        contentHeight = new JTextField();
        footerWidth = new JTextField();
        footerHeight = new JTextField();
        tile1Width = new JTextField();
        tile1Height = new JTextField();
        tile2Width = new JTextField();
        tile2Height = new JTextField();
        tile3Width = new JTextField();
        tile3Height = new JTextField();  
        bausteinWidth = new JTextField();
        bausteinHeight = new JTextField();

        //Buttons
        buttonLogoUpload = new JButton();
        buttonCreateLayout = new JButton();       
        buttonFixForms = new JButton();
        buttonIndividualForms = new JButton();

        buttonGroup.add(onlyborderRect);   
        onlyborderRect.setText("leer");
        onlyborderRect.setForeground(Color.white);
        onlyborderRect.addActionListener(this);
        
        buttonGroup.add(filledRect);
        filledRect.setText("gefüllt");
        filledRect.setSelected(true);
        filledRect.setForeground(Color.white);
        filledRect.addActionListener(this);

        buttonCreateLayout.setText("erstellen");
        buttonCreateLayout.addActionListener(this);
               
        buttonFixForms.setText("erstellen");
        buttonFixForms.addActionListener(this);  
        
        buttonIndividualForms.setText("erstellen");
        buttonIndividualForms.addActionListener(this);
   
        buttonLogoUpload.setText("laden..");
        buttonLogoUpload.addActionListener(this);
        
        
        
        Border titled_border = BorderFactory.createRaisedSoftBevelBorder();
   
        layoutPanel.setBackground(darkGrayPanel);
         
        Border tb_layout = BorderFactory.createTitledBorder(titled_border, "Layout", TitledBorder.LEFT, TitledBorder.TOP,font_border, Color.white);
        layoutPanel.setBorder(tb_layout);

       GroupLayout layoutPanelLayout = new GroupLayout(layoutPanel);
       layoutPanel.setLayout(layoutPanelLayout);
        layoutPanelLayout.setHorizontalGroup(
            layoutPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
              .addGroup(GroupLayout.Alignment.TRAILING, layoutPanelLayout.createSequentialGroup()
                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layoutPanelLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                    .addComponent(buttonCreateLayout, GroupLayout.PREFERRED_SIZE, 86, 86)
                    .addGroup(layoutPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addGroup(layoutPanelLayout.createSequentialGroup()
                            .addComponent(header, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(headerWidth, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(headerHeight, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE))
                        .addGroup(layoutPanelLayout.createSequentialGroup()
                            .addComponent(content, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(contentWidth, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(contentHeight, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE))
                        .addGroup(layoutPanelLayout.createSequentialGroup()
                            .addComponent(footer, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(footerWidth, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(footerHeight, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE))))
                .addGap(14, 14, 14))
        );
        layoutPanelLayout.setVerticalGroup(
            layoutPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layoutPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layoutPanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(header, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
                    .addComponent(headerWidth, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
                    .addComponent(headerHeight, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layoutPanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(content, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
                    .addComponent(contentWidth, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
                    .addComponent(contentHeight, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layoutPanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(footer, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
                    .addComponent(footerWidth, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
                    .addComponent(footerHeight, GroupLayout.PREFERRED_SIZE, 25,GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                .addComponent(buttonCreateLayout, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        fixFormsPanel.setBackground(darkGrayPanel);
        Border tb_fix = BorderFactory.createTitledBorder(titled_border, "Feste Plätze", TitledBorder.LEFT, TitledBorder.TOP,font_border, Color.white);
        fixFormsPanel.setBorder(tb_fix);
        
        GroupLayout fixFormsPanelLayout = new GroupLayout(fixFormsPanel);
        fixFormsPanel.setLayout(fixFormsPanelLayout);
        fixFormsPanelLayout.setHorizontalGroup(
            fixFormsPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(GroupLayout.Alignment.TRAILING, fixFormsPanelLayout.createSequentialGroup()
                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(fixFormsPanelLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                    .addComponent(buttonFixForms, GroupLayout.PREFERRED_SIZE, 86, GroupLayout.PREFERRED_SIZE)
                    .addGroup(fixFormsPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addGroup(fixFormsPanelLayout.createSequentialGroup()
                            .addComponent(tile1, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(tile1Width, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(tile1Height, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE))
                        .addGroup(fixFormsPanelLayout.createSequentialGroup()
                            .addComponent(tile2, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(tile2Width, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(tile2Height, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE))
                        .addGroup(fixFormsPanelLayout.createSequentialGroup()
                            .addComponent(tile3, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(tile3Width, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(tile3Height, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE))))
                .addGap(14, 14, 14))
        );
        fixFormsPanelLayout.setVerticalGroup(
            fixFormsPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(fixFormsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(fixFormsPanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(tile1, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
                    .addComponent(tile1Width, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
                    .addComponent(tile1Height, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(fixFormsPanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(tile2, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
                    .addComponent(tile2Width, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
                    .addComponent(tile2Height, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(fixFormsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tile3, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
                    .addComponent(tile3Width, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
                    .addComponent(tile3Height, GroupLayout.PREFERRED_SIZE, 25,GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                .addComponent(buttonFixForms, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        individualFormsPanel.setBackground(darkGrayPanel);
       
        Border tb_individual = BorderFactory.createTitledBorder(titled_border, "Individuelle Plätze", TitledBorder.LEFT, TitledBorder.TOP,font_border, Color.white);
        individualFormsPanel.setBorder(tb_individual);
        
        GroupLayout individualFormsPanelLayout = new GroupLayout(individualFormsPanel);
        individualFormsPanel.setLayout(individualFormsPanelLayout);
        individualFormsPanelLayout.setHorizontalGroup(
            individualFormsPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
             .addGroup(individualFormsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(baustein, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(individualFormsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(onlyborderRect, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(filledRect, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(individualFormsPanelLayout.createSequentialGroup()
                        .addComponent(bausteinWidth, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bausteinHeight, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE))
                    .addComponent(buttonIndividualForms, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(14,14,14))
        );
        individualFormsPanelLayout.setVerticalGroup(
            individualFormsPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(individualFormsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(individualFormsPanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(baustein, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
                    .addComponent(bausteinWidth, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
                    .addComponent(bausteinHeight, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE))
                .addGap(16, 16, 16)
                .addComponent(onlyborderRect, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(filledRect, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(buttonIndividualForms)
                .addContainerGap(33, Short.MAX_VALUE))
        );


        editPanel.setBackground(lightGrayPanel);

        GroupLayout editPanelLayout = new GroupLayout(editPanel);
        editPanel.setLayout(editPanelLayout);
        editPanelLayout.setHorizontalGroup(
            editPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)   
                .addComponent(GUI.imageLabel)
                .addComponent(image_label) 
            .addGap(2,2,2)
          
        );
        editPanelLayout.setVerticalGroup(
            editPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)   
            .addComponent(GUI.imageLabel)
                .addComponent(image_label)       
            .addGap(2,2,2)
        );

        
       websitePanel.setBackground(darkGrayPanel);
      
        Border tb_wesite = BorderFactory.createTitledBorder(titled_border, "Webseite", TitledBorder.LEFT, TitledBorder.TOP,font_border, Color.white);
        websitePanel.setBorder(tb_wesite);
            
       GroupLayout websitePanelLayout = new GroupLayout(websitePanel);
        websitePanel.setLayout(websitePanelLayout);
        websitePanelLayout.setHorizontalGroup(
            websitePanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
             .addGroup(GroupLayout.Alignment.TRAILING, websitePanelLayout.createSequentialGroup()
                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(websitePanelLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)               
                    .addGroup(websitePanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)          
                        .addGroup(websitePanelLayout.createSequentialGroup()
                            .addComponent(logo, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(buttonLogoUpload, GroupLayout.PREFERRED_SIZE, 86, GroupLayout.PREFERRED_SIZE))
                        .addGroup(websitePanelLayout.createSequentialGroup()
                            .addComponent(website, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(websiteTextField, GroupLayout.PREFERRED_SIZE, 85, GroupLayout.PREFERRED_SIZE))))
                .addGap(14, 14, 14))
        );
        websitePanelLayout.setVerticalGroup(
            websitePanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(websitePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(websitePanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(website, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
                    .addComponent(websiteTextField, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(websitePanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(logo, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonLogoUpload, GroupLayout.PREFERRED_SIZE, 30,GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
               .addContainerGap())
        );


        scrollPane.setViewportView(editPanel);
        tabbedPane.addTab("neu", scrollPane);
        
        GroupLayout layout = new GroupLayout(this);
        this.setLayout(layout);
        this.setBackground(darkGrayPanel);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
              
                .addComponent(tabbedPane, GroupLayout.DEFAULT_SIZE,0, Short.MAX_VALUE)
                .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                    .addComponent(websitePanel, GroupLayout.DEFAULT_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(layoutPanel, GroupLayout.DEFAULT_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(fixFormsPanel, GroupLayout.DEFAULT_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(individualFormsPanel, GroupLayout.DEFAULT_SIZE,0, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20 , 20, 20)
                .addComponent(websitePanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(layoutPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(fixFormsPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(individualFormsPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
                
            .addComponent(tabbedPane,GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)            
        );  
    }
  

    @Override
    public void actionPerformed(ActionEvent object) 
    {
        if (object.getSource() == buttonLogoUpload)
        {  
            JFileChooser fc = new JFileChooser();            
            fc.setFileFilter(new FileNameExtensionFilter("*.jpg;*.gif","jpg", "gif"));
            
            int state = fc.showOpenDialog(editPanel);
            if ( state == JFileChooser.APPROVE_OPTION ){
                File file = fc.getSelectedFile();  
                BufferedImage bi;
                    try {
                    bi = ImageIO.read(file);
                            image_label.setIcon(new ImageIcon(bi));
                            
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }            
          }
             setVisible(true);
             repaint();       
        } 
        if (object.getSource() == buttonCreateLayout)
        {   
            cf.headerW = Integer.parseInt(headerWidth.getText());       //Daten auslesen
            cf.headerH = Integer.parseInt(headerHeight.getText());
            repaint();                                                  //Zeichnen
            cf.contentW = Integer.parseInt(contentWidth.getText());
            cf.contentH = Integer.parseInt(contentHeight.getText());
            repaint();
            cf.footerW = Integer.parseInt(footerWidth.getText());
            cf.footerH = Integer.parseInt(footerHeight.getText());
            repaint();     
        } 
        if (object.getSource() == buttonFixForms)
        {   
            cf.tile1W = Integer.parseInt(tile1Width.getText());
            cf.tile1H = Integer.parseInt(tile1Height.getText());
            repaint();
            cf.tile2W = Integer.parseInt(tile2Width.getText());
            cf.tile2H = Integer.parseInt(tile2Height.getText());
            repaint();
            cf.tile3W = Integer.parseInt(tile3Width.getText());
            cf.tile3H = Integer.parseInt(tile3Height.getText()); 
            repaint();
        } 
        if (object.getSource() == buttonIndividualForms)
        {    
           cf.bausteinW = Integer.parseInt(bausteinWidth.getText());    //Daten auslesen
           cf.bausteinH = Integer.parseInt(bausteinHeight.getText());   //Daten auslesen
           repaint();
        } 
        if (object.getSource() == onlyborderRect)
        {  
            repaint();
        }
        if (object.getSource() == filledRect)
        {  
            repaint();
        }
    }

   //variablen deklarieren
   //.... (wird leider sonst zu lang zum posten)
}
 
Teil 2 - Klasse EditPanel

Java:
public class EditPanel extends JPanel
{

    public  EditPanel()
    {
     
        /* ********************* Mouse Listener ***************************  */
        addMouseListener(new MouseAdapter() 
        {
            public void mousePressed(MouseEvent e) 
            {
                x1 = e.getX();
                y1 = e.getY();  
                repaint();
            }  
            
            public void mouseReleased(MouseEvent e) 
            {
                x2 = x1 + e.getX();
                y2 = x1 + e.getY(); 
                repaint(); 
                
            }      
        });

        /* ****************** Mouse Motion Listener ***********************  */
        addMouseMotionListener(new MouseMotionAdapter() 
        {
            public void mouseDragged(MouseEvent e) 
            {
              
               repaint(); 
      
            }
            });
       
    }

    protected void paintComponent(Graphics g) 
    {
        super.paintComponent(g);
        
        if(CreateFile.cf.onlyborderRect.isSelected()){
            borderrect(g);
        }
        if(CreateFile.cf.filledRect.isSelected()){
            filledrect(g);
        }
            int centerX = getWidth()/2;     
            int beginPosition = (getHeight()-(CreateFile.cf.headerH + CreateFile.cf.contentH+ CreateFile.cf.footerH+CreateFile.cf.tile1H+CreateFile.cf.tile3H))/2;
            
            //header
            headerRect = new Rectangle(centerX-CreateFile.cf.headerW/2,beginPosition,CreateFile.cf.headerW,CreateFile.cf.headerH);      
            g.setColor(grayRect);
            headerRect.translate(-CreateFile.cf.tile2W/2,CreateFile.cf.tile1H);
            g.fill3DRect(headerRect.x, headerRect.y, headerRect.width, headerRect.height,true);

            //content
            Rectangle contentRect = new Rectangle(centerX-CreateFile.cf.contentW/2,beginPosition + CreateFile.cf.headerH,CreateFile.cf.contentW,CreateFile.cf.contentH);
            g.setColor(grayRect);
            contentRect.translate(-CreateFile.cf.tile2W/2,CreateFile.cf.tile1H+CreateFile.cf.tile3H);   
            g.fill3DRect(contentRect.x, contentRect.y, contentRect.width, contentRect.height,true);


            //footer
            Rectangle footerRect = new Rectangle(centerX-CreateFile.cf.footerW/2,beginPosition+CreateFile.cf.headerH+CreateFile.cf.contentH,CreateFile.cf.footerW,CreateFile.cf.footerH);
            g.setColor(grayRect);
            footerRect.translate(-CreateFile.cf.tile2W/2,CreateFile.cf.tile1H+CreateFile.cf.tile3H);   
            g.fill3DRect(footerRect.x, footerRect.y, footerRect.width, footerRect.height,true);


            //tile 1
            Rectangle tile1Rect = new Rectangle(centerX-CreateFile.cf.headerW/2-CreateFile.cf.tile2W/2,beginPosition,CreateFile.cf.tile1W,CreateFile.cf.tile1H);
            g.setColor(orangeRect);  
            g.fill3DRect(tile1Rect.x, tile1Rect.y, tile1Rect.width, tile1Rect.height,true);


            //tile 2    
            Rectangle tile2Rect = new Rectangle(centerX+CreateFile.cf.headerW/2-CreateFile.cf.tile2W/2,beginPosition+CreateFile.cf.tile1H,CreateFile.cf.tile2W,CreateFile.cf.tile2H);
            g.setColor(orangeRect);  
            g.fill3DRect(tile2Rect.x, tile2Rect.y, tile2Rect.width, tile2Rect.height,true);


            //tile3      
            Rectangle tile3Rect = new Rectangle(centerX-CreateFile.cf.headerW/2-CreateFile.cf.tile2W/2,beginPosition+CreateFile.cf.tile1H+CreateFile.cf.headerH,CreateFile.cf.tile3W,CreateFile.cf.tile3H);
            g.setColor(orangeRect);  
            g.fill3DRect(tile3Rect.x, tile3Rect.y, tile3Rect.width, tile3Rect.height,true);


          
    }

    public void borderrect (Graphics g){
         //baustein     
            Graphics2D g2d = (Graphics2D) g;
         
                g2d.setPaint(Color.LIGHT_GRAY);
                g2d.setStroke(new BasicStroke(2.0f));

                Rectangle2D rec = new Rectangle2D.Double(x2, y2,CreateFile.cf.bausteinW,CreateFile.cf.bausteinH);

                rectangles.add(rec);
                for (Rectangle2D r : rectangles) {
                    g2d.draw(r);
                }
            
    }
    
    public void filledrect (Graphics g) {
         
           Graphics2D g2d = (Graphics2D) g;
           
                g2d.setPaint(orangeRect);
 
                Rectangle2D rec = new Rectangle2D.Double(x2, y2,CreateFile.cf.bausteinW,CreateFile.cf.bausteinH);

                rectangles.add(rec);
                for (Rectangle2D r : rectangles) {
                    g2d.fill(r);
                }     
    }
    
    private List<Rectangle2D> rectangles = new ArrayList<>();
    
    Image img;  
    int x, y;
    int x1,y1; //mousePressed
    int x2, y2; //mouseReleased
 

    static Rectangle headerRect ;
      
    //Colors
    Color grayRect = new Color(230,227,224);
    Color orangeRect = new Color(211,111,53);
    
}
 
GroupLayout ist immer schlimm wenn nicht woanders Java 1.6 vorausgesetzt wird, mich schließt du damit aus,
könnte ich evtl. noch durch einfaches add() ersetzen (eine Zeile, drei Buchstaben statt 1 Seite Code!..)

aber mit den fehlenden Variablen hätte ich nun unzähliges mühevoll Zeile für Zeile neu einzusetzen,
da streike ich

ein vollständiges Testprogramm ist es sowieso nicht ohne eine GUI drumherum und kürzen kann man auch,
ist es dir unmöglich, auf
Java:
        headerWidth = new JTextField();
        headerHeight = new JTextField();
        contentWidth = new JTextField();
        contentHeight = new JTextField();
        footerWidth = new JTextField();
        footerHeight = new JTextField();
        tile1Width = new JTextField();
        tile1Height = new JTextField();
        tile2Width = new JTextField();
        tile2Height = new JTextField();
        tile3Width = new JTextField();
        tile3Height = new JTextField();  
        bausteinWidth = new JTextField();
        bausteinHeight = new JTextField();
temporär zu verzichten und eine feste Breite von 50 einzubauen?

immerhin kann ich das auch selber raushauen, auch diese Arbeit hast du nun aber einmalig verspielt,
schaue an wie es aussehen kann und handle in Zukunft selber,
Programm funktioniert nach wie vor bestens, wobei dir das vielleicht nur begrenzt helfen kann,
solange es kein Programm ist welches du selber durch Kürzen komplett kennst und noch den Fehler enthält

Tipp: vielleicht geht es dir auch schon, prüfe mit System.out.println(), welches if erfüllt ist,
die anderen Zeichenvorgänge oder sonst was könnte das Endergebnis verblenden

Java:
public class TestGUI
    extends JFrame
{

    public TestGUI()
    {
        add(CreateFile.cf);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 400);
        setVisible(true);
    }

    public static void main(String[] args)
    {
        new TestGUI();
    }


    static class EditPanel
        extends JPanel
    {

        public EditPanel()
        {

            /* ********************* Mouse Listener *************************** */
            addMouseListener(new MouseAdapter()
                {
                    public void mousePressed(MouseEvent e)
                    {
                        x1 = e.getX();
                        y1 = e.getY();
                        repaint();
                    }

                    public void mouseReleased(MouseEvent e)
                    {
                        x2 = x1 + e.getX();
                        y2 = x1 + e.getY();
                        repaint();

                    }
                });

            /* ****************** Mouse Motion Listener *********************** */
            addMouseMotionListener(new MouseMotionAdapter()
                {
                    public void mouseDragged(MouseEvent e)
                    {

                        repaint();

                    }
                });

        }

        protected void paintComponent(Graphics g)
        {
            super.paintComponent(g);

            if (CreateFile.cf.onlyborderRect.isSelected())
            {
                borderrect(g);
            }
            if (CreateFile.cf.filledRect.isSelected())
            {
                filledrect(g);
            }
            // int centerX = getWidth()/2;
            // int beginPosition = (getHeight()-(CreateFile.cf.headerH +
            // CreateFile.cf.contentH+
            // CreateFile.cf.footerH+CreateFile.cf.tile1H+CreateFile.cf.tile3H))/2;
            //
            // //header
            // headerRect = new
            // Rectangle(centerX-CreateFile.cf.headerW/2,beginPosition,CreateFile.cf.headerW,CreateFile.cf.headerH);
            // g.setColor(grayRect);
            // headerRect.translate(-CreateFile.cf.tile2W/2,CreateFile.cf.tile1H);
            // g.fill3DRect(headerRect.x, headerRect.y, headerRect.width,
            // headerRect.height,true);
            //
            // //content
            // Rectangle contentRect = new
            // Rectangle(centerX-CreateFile.cf.contentW/2,beginPosition +
            // CreateFile.cf.headerH,CreateFile.cf.contentW,CreateFile.cf.contentH);
            // g.setColor(grayRect);
            // contentRect.translate(-CreateFile.cf.tile2W/2,CreateFile.cf.tile1H+CreateFile.cf.tile3H);
            // g.fill3DRect(contentRect.x, contentRect.y, contentRect.width,
            // contentRect.height,true);
            //
            // //footer
            // Rectangle footerRect = new
            // Rectangle(centerX-CreateFile.cf.footerW/2,beginPosition+CreateFile.cf.headerH+CreateFile.cf.contentH,CreateFile.cf.footerW,CreateFile.cf.footerH);
            // g.setColor(grayRect);
            // footerRect.translate(-CreateFile.cf.tile2W/2,CreateFile.cf.tile1H+CreateFile.cf.tile3H);
            // g.fill3DRect(footerRect.x, footerRect.y, footerRect.width,
            // footerRect.height,true);
            //
            // //tile 1
            // Rectangle tile1Rect = new
            // Rectangle(centerX-CreateFile.cf.headerW/2-CreateFile.cf.tile2W/2,beginPosition,CreateFile.cf.tile1W,CreateFile.cf.tile1H);
            // g.setColor(orangeRect);
            // g.fill3DRect(tile1Rect.x, tile1Rect.y, tile1Rect.width,
            // tile1Rect.height,true);
            //
            // //tile 2
            // Rectangle tile2Rect = new
            // Rectangle(centerX+CreateFile.cf.headerW/2-CreateFile.cf.tile2W/2,beginPosition+CreateFile.cf.tile1H,CreateFile.cf.tile2W,CreateFile.cf.tile2H);
            // g.setColor(orangeRect);
            // g.fill3DRect(tile2Rect.x, tile2Rect.y, tile2Rect.width,
            // tile2Rect.height,true);
            //
            // //tile3
            // Rectangle tile3Rect = new
            // Rectangle(centerX-CreateFile.cf.headerW/2-CreateFile.cf.tile2W/2,beginPosition+CreateFile.cf.tile1H+CreateFile.cf.headerH,CreateFile.cf.tile3W,CreateFile.cf.tile3H);
            // g.setColor(orangeRect);
            // g.fill3DRect(tile3Rect.x, tile3Rect.y, tile3Rect.width,
            // tile3Rect.height,true);


        }

        public void borderrect(Graphics g)
        {
            // baustein
            Graphics2D g2d = (Graphics2D)g;

            g2d.setPaint(Color.LIGHT_GRAY);
            g2d.setStroke(new BasicStroke(2.0f));

            Rectangle2D rec = new Rectangle2D.Double(x2, y2, 50, 50);

            rectangles.add(rec);
            for (Rectangle2D r : rectangles)
            {
                g2d.draw(r);
            }

        }

        public void filledrect(Graphics g)
        {

            Graphics2D g2d = (Graphics2D)g;

            g2d.setPaint(orangeRect);

            Rectangle2D rec = new Rectangle2D.Double(x2, y2, 50, 50);

            rectangles.add(rec);
            for (Rectangle2D r : rectangles)
            {
                g2d.fill(r);
            }
        }

        private List<Rectangle2D> rectangles = new ArrayList();

        Image img;
        int x, y;
        int x1, y1; // mousePressed
        int x2, y2; // mouseReleased


        static Rectangle headerRect;

        // Colors
        Color grayRect = new Color(230, 227, 224);
        Color orangeRect = new Color(211, 111, 53);

    }

    static class CreateFile
        extends JPanel
        implements ActionListener
    {
        static CreateFile cf = new CreateFile();


        JRadioButton onlyborderRect;
        JRadioButton filledRect;

        public CreateFile()
        {
            initComponents();
        }

        public void initComponents()
        {

            EditPanel editPanel = new EditPanel();
            editPanel.setPreferredSize(new Dimension(400, 400));

            JScrollPane scrollPane = new JScrollPane(editPanel);
            editPanel.setAutoscrolls(true);
            scrollPane.setPreferredSize(new Dimension(200, 200));


            // ButtonGroup
            ButtonGroup buttonGroup = new ButtonGroup();

            // RadioButtons
            onlyborderRect = new JRadioButton();
            filledRect = new JRadioButton();


            buttonGroup.add(onlyborderRect);
            onlyborderRect.setText("leer");
            onlyborderRect.setForeground(Color.white);
            onlyborderRect.addActionListener(this);

            buttonGroup.add(filledRect);
            filledRect.setText("gefüllt");
            filledRect.setSelected(true);
            filledRect.setForeground(Color.white);
            filledRect.addActionListener(this);


            add(onlyborderRect);
            add(filledRect);
            add(scrollPane);
        }


        public void actionPerformed(ActionEvent object)
        {
            if (object.getSource() == onlyborderRect)
            {
                repaint();
            }
            if (object.getSource() == filledRect)
            {
                repaint();
            }
        }

    }
}
 
mit isEnabled() tut sich etwas ehr aber auch nicht das gewünschte 🙁... mit isEnabled() zeichnet er zumidest das erste mal beim drücken der Button das richtige Rechteck....🙁
 
Tja, wo soll man da anfangen?
Ich schmeiss mal unsortiert ein paar Kommentare in die Runde:

1. Im ersten Post hat CreateFile noch keine statische Variable cf, obwohl diese in EditPanel verwendet wir. Später hat CreateFile sie doch.

2. Der Quälcode von EditPanel aus dem ersten Post ist vollkommener fuppes. Die paintComponent-Methode von EditPanel fängt im Konstruktor an! Da fehlen offenbar ein paar geschweifte Klammern. Wenn Du schon kein KSKB zustande bekommst, dan sorge bitte dafür, dass die einzelnen Schnipsel ok sind. Zur Zeit bin ich mir nicht sicher, dass das, was du uns hier an Code präsentierst, tatsächlich das ist, womit du arbeitest, und das ist ein 110% nogo.

3. Mir deucht, der tiefere Sinn deiner Klasse CreateFile ist die Speicherung von 4 Integer-Variablen. Ansonsten ist mir nicht klar, wofür du diese Klasse brauchst. Um Components irgendwo draufzupacken, reicht auch ein normales JPanel.

4. Der eventuell entscheidende Punkt: du fragst in der paintComponent von EditPanel den Status der JRadioButtons von CreateFile.cf ab. CreateFile.cf ist eine Instanz von CreateFile. Irgendwo musss es ein top-level-window geben wo die ganze GUI drinsteckt. Frage ist, ob in diesem container CreateFile.cf drinsteckt oder eine andere Instanz von CreateFile. Ich vermute, dass die JRadioButtons, deren Status du abfragst, nicht die sind, die du in der GUI änderst.

So wie du deine Klassen verschachtelt hast, halte ich es für fast unmöglich, deine Probleme rational zu diagnostizieren. Für den Anfang würde ich folgendes empfehlen:

Erweitere deine EditPanel-Klasse um die Methoden public void addRectangle(Rectangle2D rect) und public void setBordersVisible(boolean flag). Wenn der Benutzer auf einen der Buttons zum erzeugen eines Rechtecks klickt, konstruiere eins und füge es per addRectangle-Methode einem EditPanel hinzu. Eine Liste mit Rectangles in deiner EditPanel Klasse, die zur Speicherung deiner Rectangles verwendet werden kann, gibt es ja schon (gehört zwar thematisch eher hier hin, aber was solls). Und wenn der Benutzer auf einen der JRadioButtons klickt, erzeuge im Listener eine passende boolesche Variable und rufe damit die setBordersVisible-Methode auf.
Diese Kreuz-und-Quer-Bezüge zwischen den einzelnen Klassen über Variablen (nicht über Methoden!) finde ich extrem verwirrend.
 
Zuletzt bearbeitet:

Zurück
Oben