Advertisement
jovanovski

ИА Лаб3

Oct 31st, 2012
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.05 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Component;
  3. import java.awt.Container;
  4. import java.awt.Dimension;
  5. import java.awt.FlowLayout;
  6. import java.awt.GridBagConstraints;
  7. import java.awt.GridBagLayout;
  8. import java.awt.GridLayout;
  9. import java.awt.Image;
  10. import java.awt.Insets;
  11.  
  12. import javax.swing.*;
  13. import javax.swing.event.ListSelectionEvent;
  14. import javax.swing.event.ListSelectionListener;
  15.  
  16.  
  17. class Forma extends JFrame implements ListSelectionListener{
  18.     JPanel panel1;
  19.     JPanel panel2;
  20.     JList lista;
  21.     JLabel slika;
  22.     JTextField brand;
  23.     JTextField model;
  24.     JTextField cpu;
  25.     JTextField ram;
  26.     JTextField price;
  27.     Computer tmp = new Computer("","","","","","");
  28.     Computer [] computers = tmp.getComputers();
  29.     private static final Insets insets= new Insets(0,0,0,0);
  30.     public Forma(){
  31.         super("Computers");
  32.         setResizable(false);
  33.         setLayout(new GridLayout(1,2));
  34.         setSize(400, 500);
  35.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  36.         addPanel1();
  37.         addPanel2();
  38.     }
  39.    
  40.     public void addPanel1(){
  41.         panel1 = new JPanel();
  42.         panel1.setLayout(new GridLayout(1,1));
  43.         panel1.setBorder(BorderFactory.createTitledBorder("Computer list:"));
  44.         lista = new JList(computers);
  45.         lista.addListSelectionListener(this);
  46.         panel1.add(lista);
  47.         getContentPane().add(panel1);
  48.     }
  49.    
  50.     public void addPanel2(){
  51.         panel2 = new JPanel();
  52.         panel2.setLayout(new FlowLayout());
  53. //      panel2.setLayout(new GridBagLayout());
  54.         slika = new JLabel();
  55.         slika.setPreferredSize(new Dimension(200,170));
  56.  
  57.         panel2.add(slika);
  58.         JPanel content = new JPanel();
  59.         content.setSize(200, 300);
  60.         content.setLayout(new GridLayout(10, 1));
  61.        
  62.         brand = new JTextField();
  63.         brand.setEditable(false);
  64.         model = new JTextField();
  65.         model.setEditable(false);
  66.         cpu = new JTextField();
  67.         cpu.setEditable(false);
  68.         ram = new JTextField();
  69.         ram.setEditable(false);
  70.         price = new JTextField();
  71.         price.setEditable(false);
  72.         content.add(new JLabel("Brand: "));
  73.         content.add(brand);
  74.         content.add(new JLabel("Model: "));
  75.         content.add(model);
  76.         content.add(new JLabel("CPU: "));
  77.         content.add(cpu);
  78.         content.add(new JLabel("RAM: "));
  79.         content.add(ram);
  80.         content.add(new JLabel("Price: "));
  81.         content.add(price);
  82.        
  83.         panel2.add(content);
  84.        
  85. //      addComponent(panel2, slika, 0, 0, 1, 5, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
  86. //      addComponent(panel2, new JLabel("Brand:"), 0, 3, 1, 1, GridBagConstraints.SOUTH, GridBagConstraints.HORIZONTAL);
  87. //      addComponent(panel2, brand, 0, 4, 1, 1, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL);
  88. //      addComponent(panel2, new JLabel("Model:"), 0, 5, 1, 1, GridBagConstraints.SOUTH, GridBagConstraints.HORIZONTAL);
  89. //      addComponent(panel2, model, 0, 6, 1, 1, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL);
  90. //      addComponent(panel2, new JLabel("CPU:"), 0, 7, 1, 1, GridBagConstraints.SOUTH, GridBagConstraints.HORIZONTAL);
  91. //      addComponent(panel2, cpu, 0, 8, 1, 1, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL);
  92. //      addComponent(panel2, new JLabel("RAM:"), 0, 9, 1, 1, GridBagConstraints.SOUTH, GridBagConstraints.HORIZONTAL);
  93. //      addComponent(panel2, ram, 0, 10, 1, 1, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL);
  94. //      addComponent(panel2, new JLabel("Price:"), 0, 11, 1, 1, GridBagConstraints.SOUTH, GridBagConstraints.HORIZONTAL);
  95. //      addComponent(panel2, price, 0, 12, 1, 1, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL);
  96.  
  97.        
  98.         getContentPane().add(panel2);
  99.        
  100.     }
  101.    
  102.     public void init(){
  103.         Computer cmp = computers[0];
  104.         brand.setText(cmp.getMarka());
  105.         model.setText(cmp.getModel());
  106.         ram.setText(cmp.getRam());
  107.         cpu.setText(cmp.getCpu());
  108.         price.setText(cmp.getCena());
  109.         ImageIcon set = new ImageIcon("src/sliki/"+cmp.getSlika());
  110.         Image im = set.getImage().getScaledInstance(slika.getWidth(), slika.getHeight()-200, Image.SCALE_SMOOTH);
  111.         set = new ImageIcon(im);
  112.         slika.setIcon(set);
  113.         lista.setSelectedValue(cmp, isEnabled());
  114.     }
  115.    
  116.     public void valueChanged(ListSelectionEvent e) {
  117.         if(e.getValueIsAdjusting() == false) {
  118.             if(lista.getSelectedIndex() != -1 ) {
  119.                 Computer cmp = (Computer) lista.getSelectedValue();
  120.                 brand.setText(cmp.getMarka());
  121.                 model.setText(cmp.getModel());
  122.                 ram.setText(cmp.getRam());
  123.                 cpu.setText(cmp.getCpu());
  124.                 price.setText(cmp.getCena());
  125.                 ImageIcon set = new ImageIcon("src/sliki/"+cmp.getSlika());
  126.                 Image im = set.getImage().getScaledInstance(slika.getWidth(), slika.getHeight(), Image.SCALE_SMOOTH);
  127.                 set = new ImageIcon(im);
  128.                 slika.setIcon(set);
  129.             }
  130.         }
  131.     }
  132.    
  133.     private static void addComponent(Container container, Component component, int gridx, int gridy, int gridwidth, int gridheight, int anchor, int fill) {
  134.         GridBagConstraints gbc = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, 1.0, 1.0, anchor, fill, insets, 0, 0);
  135.         gbc.weighty = 0.5;
  136.         container.add(component, gbc);
  137.     }
  138. }
  139.  
  140. public class MainClass {
  141.  
  142.     /**
  143.      * @param args
  144.      */
  145.     public static void main(String[] args) {
  146.         Forma forma = new Forma();
  147.         forma.setVisible(true);
  148.         forma.init();
  149.  
  150.     }
  151.  
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement