Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Container;
- import java.awt.Dimension;
- import java.awt.FlowLayout;
- import java.awt.GridBagConstraints;
- import java.awt.GridBagLayout;
- import java.awt.GridLayout;
- import java.awt.Image;
- import java.awt.Insets;
- import javax.swing.*;
- import javax.swing.event.ListSelectionEvent;
- import javax.swing.event.ListSelectionListener;
- class Forma extends JFrame implements ListSelectionListener{
- JPanel panel1;
- JPanel panel2;
- JList lista;
- JLabel slika;
- JTextField brand;
- JTextField model;
- JTextField cpu;
- JTextField ram;
- JTextField price;
- Computer tmp = new Computer("","","","","","");
- Computer [] computers = tmp.getComputers();
- private static final Insets insets= new Insets(0,0,0,0);
- public Forma(){
- super("Computers");
- setResizable(false);
- setLayout(new GridLayout(1,2));
- setSize(400, 500);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- addPanel1();
- addPanel2();
- }
- public void addPanel1(){
- panel1 = new JPanel();
- panel1.setLayout(new GridLayout(1,1));
- panel1.setBorder(BorderFactory.createTitledBorder("Computer list:"));
- lista = new JList(computers);
- lista.addListSelectionListener(this);
- panel1.add(lista);
- getContentPane().add(panel1);
- }
- public void addPanel2(){
- panel2 = new JPanel();
- panel2.setLayout(new FlowLayout());
- // panel2.setLayout(new GridBagLayout());
- slika = new JLabel();
- slika.setPreferredSize(new Dimension(200,170));
- panel2.add(slika);
- JPanel content = new JPanel();
- content.setSize(200, 300);
- content.setLayout(new GridLayout(10, 1));
- brand = new JTextField();
- brand.setEditable(false);
- model = new JTextField();
- model.setEditable(false);
- cpu = new JTextField();
- cpu.setEditable(false);
- ram = new JTextField();
- ram.setEditable(false);
- price = new JTextField();
- price.setEditable(false);
- content.add(new JLabel("Brand: "));
- content.add(brand);
- content.add(new JLabel("Model: "));
- content.add(model);
- content.add(new JLabel("CPU: "));
- content.add(cpu);
- content.add(new JLabel("RAM: "));
- content.add(ram);
- content.add(new JLabel("Price: "));
- content.add(price);
- panel2.add(content);
- // addComponent(panel2, slika, 0, 0, 1, 5, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
- // addComponent(panel2, new JLabel("Brand:"), 0, 3, 1, 1, GridBagConstraints.SOUTH, GridBagConstraints.HORIZONTAL);
- // addComponent(panel2, brand, 0, 4, 1, 1, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL);
- // addComponent(panel2, new JLabel("Model:"), 0, 5, 1, 1, GridBagConstraints.SOUTH, GridBagConstraints.HORIZONTAL);
- // addComponent(panel2, model, 0, 6, 1, 1, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL);
- // addComponent(panel2, new JLabel("CPU:"), 0, 7, 1, 1, GridBagConstraints.SOUTH, GridBagConstraints.HORIZONTAL);
- // addComponent(panel2, cpu, 0, 8, 1, 1, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL);
- // addComponent(panel2, new JLabel("RAM:"), 0, 9, 1, 1, GridBagConstraints.SOUTH, GridBagConstraints.HORIZONTAL);
- // addComponent(panel2, ram, 0, 10, 1, 1, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL);
- // addComponent(panel2, new JLabel("Price:"), 0, 11, 1, 1, GridBagConstraints.SOUTH, GridBagConstraints.HORIZONTAL);
- // addComponent(panel2, price, 0, 12, 1, 1, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL);
- getContentPane().add(panel2);
- }
- public void init(){
- Computer cmp = computers[0];
- brand.setText(cmp.getMarka());
- model.setText(cmp.getModel());
- ram.setText(cmp.getRam());
- cpu.setText(cmp.getCpu());
- price.setText(cmp.getCena());
- ImageIcon set = new ImageIcon("src/sliki/"+cmp.getSlika());
- Image im = set.getImage().getScaledInstance(slika.getWidth(), slika.getHeight()-200, Image.SCALE_SMOOTH);
- set = new ImageIcon(im);
- slika.setIcon(set);
- lista.setSelectedValue(cmp, isEnabled());
- }
- public void valueChanged(ListSelectionEvent e) {
- if(e.getValueIsAdjusting() == false) {
- if(lista.getSelectedIndex() != -1 ) {
- Computer cmp = (Computer) lista.getSelectedValue();
- brand.setText(cmp.getMarka());
- model.setText(cmp.getModel());
- ram.setText(cmp.getRam());
- cpu.setText(cmp.getCpu());
- price.setText(cmp.getCena());
- ImageIcon set = new ImageIcon("src/sliki/"+cmp.getSlika());
- Image im = set.getImage().getScaledInstance(slika.getWidth(), slika.getHeight(), Image.SCALE_SMOOTH);
- set = new ImageIcon(im);
- slika.setIcon(set);
- }
- }
- }
- private static void addComponent(Container container, Component component, int gridx, int gridy, int gridwidth, int gridheight, int anchor, int fill) {
- GridBagConstraints gbc = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, 1.0, 1.0, anchor, fill, insets, 0, 0);
- gbc.weighty = 0.5;
- container.add(component, gbc);
- }
- }
- public class MainClass {
- /**
- * @param args
- */
- public static void main(String[] args) {
- Forma forma = new Forma();
- forma.setVisible(true);
- forma.init();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement