4 Swing: Layoutmanager für drei Komponenten
4 Swing: Layoutmanager für drei KomponentenWelche Layoutmanager können auf Wunsch drei Komponenten horizontal oder auch vertikal anzeigen?
Gehen Sie davon aus, dass der LayoutManager die Komponenten mindestens in ihrer Wunschgröße darstellen kann.
Die Antwort finden Sie hinter der nächsten Frage (URL rechts unten klicken).
Niveau | 2 |
Schwierigkeitsgrad | mittel |
Zeit | 3 Minuten |
Antwort zu Frage 3: Layoutmanager
|
Der Quellcode des Applets
package Kurs2.Swing;import java.awt.BorderLayout;import java.awt.Container;import java.awt.FlowLayout;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.*;public class LayoutTest extends JApplet implements ActionListener {JButton[] b;static final int BUTTONS = 6;public LayoutTest() {b = new JButton[BUTTONS+1];JPanel jp = new JPanel();jp.setLayout( new BoxLayout(jp,BoxLayout.Y_AXIS));for (int i=1; i<= BUTTONS; i++) {b[i] = new JButton("Layoutmanager " + i );jp.add(b[i]);b[i].addActionListener(this);}Container myPane = getContentPane();myPane.add(jp);}public static void main(String[] args) {layout1();layout2();layout3();layout4();layout5();layout6();}public static void layout1() {JFrame f ;JPanel jp;f= new JFrame("Layout 1");jp = new JPanel();jp.setLayout(new FlowLayout());jp.add(new JButton("A"),BorderLayout.NORTH);jp.add(new JButton("B"),BorderLayout.WEST);jp.add(new JButton("CCCCCCCC"),BorderLayout.CENTER);JTextArea jta =new JTextArea(2,10);jta.append("JTextArea \nrow 2\nrow 3");jp.add(jta,BorderLayout.SOUTH);jp.add(new JButton("D"));f.add(jp);f.pack();f.setVisible(true);}public static void layout2() {JFrame f ;JPanel jp;f= new JFrame("Layout 2");jp = new JPanel();jp.setLayout( new BoxLayout(jp,BoxLayout.Y_AXIS));
jp.add(new JButton("A"));jp.add(new JButton("B"));jp.add(new JButton("CCCCCCCC"));JTextArea jta =new JTextArea(2,10);jta.append("JTextArea \nrow 2\nrow 3");jp.add(jta);jp.add(new JButton("D"));
f.add(jp);f.pack();f.setVisible(true);}
public static void layout3() {JFrame f ;JPanel jp;f= new JFrame("Layout 3");jp = new JPanel();jp.setLayout( new GridLayout(2,3));
jp.add(new JButton("A"));jp.add(new JButton("B"));jp.add(new JButton("CCCCCCCC"));JTextArea jta =new JTextArea(2,10);jta.append("JTextArea \nrow 2\nrow 3");jp.add(jta);jp.add(new JButton("D"),BorderLayout.CENTER);
f.add(jp);f.pack();f.setVisible(true);}public static void layout4() {JFrame f ;JPanel jp;f= new JFrame("Layout 4");jp = new JPanel();jp.setLayout( new BorderLayout());
jp.add(new JButton("A"),BorderLayout.NORTH);jp.add(new JButton("B"),BorderLayout.WEST);jp.add(new JButton("CCCCCCCC"),BorderLayout.CENTER);JTextArea jta =new JTextArea(2,10);jta.append("JTextArea \nrow 2\nrow 3");jp.add(jta,BorderLayout.EAST);jp.add(new JButton("D"),BorderLayout.SOUTH);
f.add(jp);f.pack();f.setVisible(true);}
public static void layout5() {JFrame f ;JPanel jp;f= new JFrame("Layout 5");jp = new JPanel();jp.setLayout( new BoxLayout(jp,BoxLayout.X_AXIS));jp.add(new JButton("A"));jp.add(new JButton("B"));jp.add(new JButton("CCCCCCCC"));JTextArea jta =new JTextArea(2,10);jta.append("JTextArea \nrow 2\nrow 3");jp.add(jta);jp.add(new JButton("D"));f.add(jp);f.pack();f.setVisible(true);}public static void layout6() {JFrame f ;JPanel jp;f= new JFrame("Layout 6");
jp = new JPanel();jp.setLayout( new BorderLayout());
jp.add(new JButton("A"),BorderLayout.NORTH);jp.add(new JButton("CCCCCCCC"),BorderLayout.CENTER);JTextArea jta =new JTextArea(2,10);jta.append("JTextArea \nrow 2\nrow 3");jp.add(jta,BorderLayout.EAST);jp.add(new JButton("D"),BorderLayout.SOUTH);
f.add(jp);f.pack();f.setVisible(true);}@Overridepublic void actionPerformed(ActionEvent e) {
Object source = e.getSource();if (source == b[1]) layout1();if (source == b[2]) layout2();if (source == b[3]) layout3();if (source == b[4]) layout4();if (source == b[5]) layout5();if (source == b[6]) layout6();}}
- 4236 views