FlowLayout layout = new FlowLayout(); final JPanel panel = new JPanel(); panel.setLayout(layout);
package com.java.myapp; import java.awt.EventQueue; import java.awt.FlowLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class MyForm extends JFrame { /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { MyForm frame = new MyForm(); frame.setVisible(true); } }); } /** * Create the frame. */ public MyForm() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 457, 173); setTitle("ThaiCreate.Com GUI Tutorial"); FlowLayout layout = new FlowLayout(); final JPanel panel = new JPanel(); panel.setLayout(layout); panel.add(new JButton("Button 1")); panel.add(new JButton("Button 2")); panel.add(new JButton("Button 3")); panel.add(new JButton("Button 4")); panel.add(new JButton("Button 5")); panel.add(new JButton("Button 6")); panel.add(new JButton("Button 7")); panel.add(new JButton("Button 8")); panel.add(new JButton("Button 9")); getContentPane().add(panel); } }
package com.java.myapp; import java.awt.BorderLayout; import java.awt.ComponentOrientation; import java.awt.EventQueue; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class MyForm extends JFrame { /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { MyForm frame = new MyForm(); frame.setVisible(true); } }); } /** * Create the frame. */ public MyForm() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 457, 173); setTitle("ThaiCreate.Com GUI Tutorial"); FlowLayout flowlyt = new FlowLayout(); final JPanel panel = new JPanel(); panel.setLayout(flowlyt); flowlyt.setAlignment(FlowLayout.TRAILING); JPanel controls = new JPanel(); controls.setLayout(new FlowLayout()); panel.add(new JButton("Button 1")); panel.add(new JButton("Button 2")); panel.add(new JButton("Button 3")); panel.add(new JButton("Button 4")); panel.add(new JButton("Button 5")); // Left to Right JButton btnLeftToRight = new JButton("Left to Right"); btnLeftToRight.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT); panel.validate(); panel.repaint(); } }); controls.add(btnLeftToRight); // Right to Left JButton btnRightToLeft = new JButton("Right to Left"); btnRightToLeft.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { panel.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); panel.validate(); panel.repaint(); } }); controls.add(btnRightToLeft); getContentPane().add(panel, BorderLayout.CENTER); getContentPane().add(controls, BorderLayout.SOUTH); ; } }
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท