all layouts
Wed Feb 21 2024 08:49:55 GMT+0000 (Coordinated Universal Time)
Saved by @dsce
FileChooserDemo.java import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import java.io.*; public class FileChooserDemo { private JFrame frame; private JFileChooser cc; private JButton b1, b2; private JTextArea ta; public FileChooserDemo() { frame = new JFrame("A Sample Frame"); frame.setSize(700, 500); cc = new JFileChooser(); ta = new JTextArea(20, 75); frame.add(ta); b1 = new JButton("Open"); b1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae) { cc.showOpenDialog(frame); File f = cc.getSelectedFile(); String text =""; try(FileInputStream fis = new FileInputStream(f)) { int c = -1; while((c=fis.read()) != -1) text += (char)c; ta.setText(text); } catch(Exception e){ e.printStackTrace();} } }); frame.add(b1); b2 = new JButton("Save"); b2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae) { cc.showSaveDialog(frame); File f = cc.getSelectedFile(); String filename = f.getName(); String text = ta.getText(); byte[] b = text.getBytes(); try( FileOutputStream fos = new FileOutputStream(filename) ) { for(int i=0; i<text.length();i++) fos.write(b[i]); } catch(Exception e){ e.printStackTrace();} } }); frame.add(b2); frame.setLayout(new FlowLayout()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public static void main(String[] args) { new FileChooserDemo(); } } ColorChooserDemo.java import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.colorchooser.*; public class ColorChooserDemo { private JFrame frame; private JColorChooser cc; private JButton b; public ColorChooserDemo() { frame = new JFrame("A Sample Frame"); frame.setSize(700, 500); cc = new JColorChooser(); frame.add(cc); b = new JButton("Color Changer"); b.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae) { frame.getContentPane().setBackground(cc.getColor()); } }); frame.add(b); frame.setLayout(new FlowLayout()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public static void main(String[] args) { new ColorChooserDemo(); } } BorderLayoutDemo.java import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class BorderLayoutDemo { JFrame frame; public BorderLayoutDemo() { frame = new JFrame("Border Layout Demo"); frame.setSize(800, 500); frame.setLayout(new BorderLayout()); frame.add(new JButton("North"), BorderLayout.NORTH); JPanel panel = new JPanel(); panel.add(new JButton("Button 1")); panel.add(new JButton("Button 2")); frame.add(panel, BorderLayout.SOUTH); frame.add(new JLabel("Border Layout West"), BorderLayout.WEST); frame.add(new JTextField(10), BorderLayout.EAST); frame.add(new JButton("CENTER"), BorderLayout.CENTER); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public static void main(String[] args) { new BorderLayoutDemo(); } } GridLayoutDemo.java import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class GridLayoutDemo { JFrame frame; public GridLayoutDemo() { frame = new JFrame("Border Layout Demo"); frame.setSize(800, 500); frame.setLayout(new GridLayout(4, 4)); JButton b1 = new JButton("1");frame.add(b1); JButton b2 = new JButton("2");frame.add(b2); JButton b3 = new JButton("3");frame.add(b3); JButton b4 = new JButton("Add");frame.add(b4); JButton b5 = new JButton("4");frame.add(b5); JButton b6 = new JButton("5");frame.add(b6); JButton b7 = new JButton("6");frame.add(b7); JButton b8 = new JButton("Subtract");frame.add(b8); JButton b9 = new JButton("7");frame.add(b9); JButton b10 = new JButton("8");frame.add(b10); JButton b11 = new JButton("9");frame.add(b11); JButton b12 = new JButton("Multiply");frame.add(b12); JButton b13 = new JButton("Dot");frame.add(b13); JButton b14 = new JButton("0");frame.add(b14); JButton b15 = new JButton("Percentage");frame.add(b15); JButton b16 = new JButton("Divide");frame.add(b16); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public static void main(String[] args) { new GridLayoutDemo(); } } CardLayoutDemo.java import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class CardLayoutDemo { JFrame frame; public CardLayoutDemo() { frame = new JFrame("Border Layout Demo"); frame.setSize(800, 500); frame.setLayout(new FlowLayout()); CardLayout clo = new CardLayout(); JPanel p1 = new JPanel();p1.setBackground(Color.yellow); JPanel p2 = new JPanel();p2.setBackground(Color.yellow); JPanel p3 = new JPanel();p3.setBackground(Color.yellow); JPanel p4 = new JPanel();p4.setBackground(Color.yellow); JPanel main = new JPanel(); main.setLayout(clo); p1.setBackground(Color.yellow); JButton b1 = new JButton("Open"); p1.add(b1); JButton b2 = new JButton("Save"); p1.add(b2); JButton b3 = new JButton("Exit"); p2.add(b3); JButton b4 = new JButton("Dispose"); p2.add(b4); JCheckBox c1 = new JCheckBox("AWT"); p3.add(c1); JCheckBox c2 = new JCheckBox("Swing"); p3.add(c2); JCheckBox c3 = new JCheckBox("Java"); p4.add(c3); JCheckBox c4 = new JCheckBox("Python"); p4.add(c4); main.add(p1, "panel1"); main.add(p2, "panel2"); main.add(p3, "panel3"); main.add(p4, "panel4"); frame.add(main); JButton next = new JButton("Next"); next.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae) { clo.next(main);} }); frame.add(next); JButton previous = new JButton("Previous"); previous.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae) { clo.previous(main);} }); frame.add(previous); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public static void main(String[] args) { new CardLayoutDemo(); } } SwingDemo.java // Listener implemented as a separate class import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SwingDemo { private JFrame frame; private JButton b1, b2; public SwingDemo() { frame = new JFrame("A Sample Frame"); frame.setSize(700, 500); MyListener lis = new MyListener(frame); b1 = new JButton("Green"); b1.addActionListener(lis); frame.add(b1); b2 = new JButton("Yellow"); b2.addActionListener(lis); frame.add(b2); frame.setLayout(new FlowLayout()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public static void main(String[] args) { new SwingDemo(); } } class MyListener implements ActionListener { private JFrame fr; public MyListener(JFrame fr) { this.fr = fr; } public void actionPerformed(ActionEvent ae) { String s = ae.getActionCommand(); if(s.equals("Green")) fr.getContentPane().setBackground(Color.green); if(s.equals("Yellow")) fr.getContentPane().setBackground(Color.yellow); } } SwingDemo2.java // Listener implemented as an inner class import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SwingDemo2 { private JFrame frame; private JButton b1, b2; public SwingDemo2() { frame = new JFrame("A Sample Frame"); frame.setSize(700, 500); MyListener lis = this.new MyListener(); // note the use of this here b1 = new JButton("Green"); b1.addActionListener(lis); frame.add(b1); b2 = new JButton("Yellow"); b2.addActionListener(lis); frame.add(b2); frame.setLayout(new FlowLayout()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public static void main(String[] args) { new SwingDemo2(); } class MyListener implements ActionListener { public void actionPerformed(ActionEvent ae) { String s = ae.getActionCommand(); if(s.equals("Green")) frame.getContentPane().setBackground(Color.green); if(s.equals("Yellow")) frame.getContentPane().setBackground(Color.yellow); } } } SwingDemo3.java // Listener implemented as anonymoys classes import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SwingDemo3 { private JFrame frame; private JButton b1, b2; public SwingDemo3() { frame = new JFrame("A Sample Frame"); frame.setSize(700, 500); b1 = new JButton("Green"); b1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae) { frame.getContentPane().setBackground(Color.green); } }); frame.add(b1); b2 = new JButton("Yellow"); b2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae) { frame.getContentPane().setBackground(Color.yellow); } }); frame.add(b2); frame.setLayout(new FlowLayout()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public static void main(String[] args) { new SwingDemo3(); } }
Comments