Autor Beitrag
LINUS19
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 156
Erhaltene Danke: 1

Windows 10, 7
Java(Eclipse)
BeitragVerfasst: Sa 18.11.17 00:58 
Hallo,
Ich bekomme es nicht hin JButtons auf einem Panel zu positionieren, ich kann die Buttons hinzufügen,dann landen sie nur nicht da wo ich sie hinhaben möchte.

LG
LINUS19
Frühlingsrolle
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Sa 18.11.17 07:25 
- Nachträglich durch die Entwickler-Ecke gelöscht -
LINUS19 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 156
Erhaltene Danke: 1

Windows 10, 7
Java(Eclipse)
BeitragVerfasst: Sa 18.11.17 11:15 
Hallo,

Hier der Quellcode:

ausblenden volle Höhe Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class EinPanel extends JPanel{ 

  final int Breite=800,  Höhe=600; // Spielfeld Breite und Höhe

  public EinPanel() {
         setPreferredSize(new Dimension(Breite+400,Höhe));
         setLayout(new FlowLayout());
         add(new Button("Abbruch"));
         add(new Button("OK"));
  }
}

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Fenster extends JFrame {

    private EinPanel eins = new EinPanel();

    public Fenster() {
   setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
   getContentPane().add(eins);       
   pack();
        getRootPane();setVisible(true);         
        eins.setFocusable(true);  
    }
    public static void main(String[] args) {
  new Fenster();  
    }
}

Hatte auch schon sowas wie setBounds() versucht.

LG
LINUS19

Moderiert von user profile iconChristian S.: Code-Tags hinzugefügt
papa69
ontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic starofftopic star
Beiträge: 79
Erhaltene Danke: 23

Win 10, Ubuntu
C#, Java, C
BeitragVerfasst: Sa 18.11.17 11:52 
Das Flow-Layout ordnet deine Buttons ... IMMER nebeneinander, soweit der Platz ausreicht.

(Soll es denn ein Flow-Layout sein?)

_________________
Daniel Bauer
... fatal ist nur, wenn sich das Licht am Ende des Tunnels als entgegenkommender ICE entpuppt ...
LINUS19 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 156
Erhaltene Danke: 1

Windows 10, 7
Java(Eclipse)
BeitragVerfasst: Sa 18.11.17 17:53 
Hallo,
Es soll eigentlich kein FlowLayout sei, es hat nur damit funktioniert.
Ich möchte nur die Größe ändern können und den Button an einer beliebigen Stelle platzieren.

LG
LINUS19
papa69
ontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic starofftopic star
Beiträge: 79
Erhaltene Danke: 23

Win 10, Ubuntu
C#, Java, C
BeitragVerfasst: Sa 18.11.17 22:20 
Dann musst du dein Layout null setzen -->Nulllayout!

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
import javax.swing.*;
import java.awt.*;
public class button extends JFrame{

 private JButton button;
 public button(){
      JPanel panel = new JPanel();
      button = new JButton("Button");
      //Null-Layout  
      panel.setLayout(null);
      
      //Hier deine Position(x, y) + Größe(w, h) setzen
      //button.setBounds(x,y,width,height);
      button.setBounds(100,20,100,20);
      panel.add(button);

      getContentPane().add(panel);
      
      setSize(400,400);
      setVisible(true);

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


So funktioniert auch dein setBounds() und du kannst den Button setzen, wo du ihn benötigst.

Viel Glück!

Moderiert von user profile iconTh69: Beitragsformatierung überarbeitet (zitierten Beitrag zusammengefügt).
Moderiert von user profile iconTh69: Code-Tags hinzugefügt

_________________
Daniel Bauer
... fatal ist nur, wenn sich das Licht am Ende des Tunnels als entgegenkommender ICE entpuppt ...