SHARE
TWEET

Untitled

a guest Jun 24th, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import javax.swing.event.*;
  4. import java.awt.event.*;
  5. public class HistoryEditor extends JFrame{
  6.  
  7.  
  8. public static Color randomColor(){
  9.     return new Color((float)Math.random(), (float)Math.random(), (float)Math.random());
  10. }
  11.  
  12. public HistoryEditor(){
  13.  
  14.     setLayout(new BorderLayout());
  15.  
  16.     final JPanel mainPanel = new JPanel();
  17.     mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
  18.     JPanel controlPanel = new JPanel(new FlowLayout());
  19.     final JTextField tf = new JTextField(20);
  20.     JButton addVertical = new JButton("Add Vertical");
  21.     JButton addHorizontal = new JButton("Add Horizontal");
  22.     JButton removeVertical = new JButton("Remove Vertical");
  23.  
  24.     addVertical.addActionListener(new ActionListener(){
  25.         public void actionPerformed(ActionEvent e){
  26.             JPanel newPanel = new JPanel();
  27.             newPanel.setPreferredSize( new Dimension(getWidth(), 50) );
  28.             newPanel.setMaximumSize( newPanel.getPreferredSize() );
  29.             newPanel.setMinimumSize( newPanel.getPreferredSize() );
  30.             newPanel.setBackground( HistoryEditor.randomColor() );
  31.             newPanel.setLayout( new BoxLayout( newPanel, BoxLayout.X_AXIS ) );
  32.  
  33.             JPanel eventPanel = new JPanel();
  34.             eventPanel.setBackground( HistoryEditor.randomColor() );
  35.             eventPanel.setMaximumSize( new Dimension(50, newPanel.getHeight() ) );
  36.             eventPanel.setMinimumSize( new Dimension(50, newPanel.getHeight() ) );
  37.             eventPanel.setPreferredSize( new Dimension(50, newPanel.getHeight() ) );
  38.             eventPanel.add( new JLabel( tf.getText() ) );
  39.             eventPanel.revalidate();
  40.             newPanel.add(eventPanel);
  41.             newPanel.revalidate();
  42.             mainPanel.add( newPanel );
  43.             mainPanel.revalidate();
  44.             mainPanel.repaint();
  45.             eventPanel.repaint();  
  46.  
  47.  
  48.         }
  49.     });
  50.  
  51.     addHorizontal.addActionListener(new ActionListener(){
  52.         public void actionPerformed(ActionEvent e){
  53.             try{
  54.                 int index = Integer.parseInt(tf.getText());
  55.                 JPanel selectedPanel = (JPanel)mainPanel.getComponent( index );
  56.  
  57.                 JPanel eventPanel = new JPanel();
  58.                 eventPanel.setBackground( HistoryEditor.randomColor() );
  59.                 eventPanel.setMaximumSize(new Dimension(50, selectedPanel.getHeight()));
  60.                 eventPanel.setMinimumSize(new Dimension(50, selectedPanel.getHeight()));
  61.                 eventPanel.add( new JLabel( tf.getText() ) );
  62.                 selectedPanel.add(eventPanel);
  63.                 selectedPanel.revalidate();
  64.                 selectedPanel.repaint();
  65.  
  66.             }catch(Exception ex){}
  67.         }
  68.     });
  69.  
  70.  
  71.     removeVertical.addActionListener(new ActionListener(){
  72.         public void actionPerformed(ActionEvent e){
  73.             try{
  74.                 int index = Integer.parseInt(tf.getText());
  75.                 mainPanel.remove(index);
  76.                 mainPanel.revalidate();
  77.                 mainPanel.repaint();
  78.  
  79.             }catch(Exception ex){}
  80.         }
  81.     });
  82.  
  83.  
  84.     controlPanel.add(tf);
  85.     controlPanel.add(addVertical);
  86.     controlPanel.add(addHorizontal);
  87.     controlPanel.add(removeVertical);  
  88.     add(mainPanel, BorderLayout.CENTER);
  89.     add(controlPanel, BorderLayout.SOUTH);
  90.     setSize(1000, 1000);
  91.     setVisible(true);
  92.     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  93.  
  94. }
  95.  
  96. public static void main(String[]args){
  97.     HistoryEditor he = new HistoryEditor();
  98. }
  99.  
  100. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top