Baixe o app para aproveitar ainda mais
Prévia do material em texto
UNESP/FEG/DMA Programação de Computadores II - Prof. Senne Exercício de Laboratório 13 public class Lab13 implements ActionListener { private JFrame janela; private JTextField total; private JLabel dados; private JTable tabela; private JButton simular; private JCheckBox catA,catB,catC; private JRadioButton sim,nao; private JMenuItem autor,limpar,fim; private int pneus = 0, cambio = 0, direcao = 0, pintura = 0; private ArrayList veiculos = new ArrayList(); public Lab13() { mostrarGUI(); limpar(); } private void mostrarGUI() { janela = new JFrame("Lab 13 - Simulação"); janela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //janela.setResizable(false); JMenuBar barra = new JMenuBar(); janela.setJMenuBar(barra); JMenu programa = new JMenu("Programa"); autor = new JMenuItem("Autor"); autor.addActionListener(this); limpar = new JMenuItem("Limpar"); limpar.addActionListener(this); fim = new JMenuItem("Fim"); fim.addActionListener(this); programa.add(autor); programa.add(limpar); programa.addSeparator(); programa.add(fim); barra.add(programa); JPanel pSup = new JPanel(new GridLayout(3,1)); JPanel p1 = new JPanel(new FlowLayout(FlowLayout.LEFT)); catA = new JCheckBox("A",true); catB = new JCheckBox("B",true); catC = new JCheckBox("C",true); p1.add(new JLabel("Tipo do Veículo: ")); p1.add(catA); p1.add(catB); p1.add(catC); JPanel p2 = new JPanel(new FlowLayout(FlowLayout.LEFT)); sim = new JRadioButton("Sim",true); nao = new JRadioButton("Não"); ButtonGroup bg = new ButtonGroup(); bg.add(sim); bg.add(nao); p2.add(new JLabel("Pneu Extra: ")); p2.add(sim); p2.add(nao); JPanel p3 = new JPanel(new FlowLayout(FlowLayout.LEFT)); total = new JTextField(3); p3.add(new JLabel("Número de Simulações: ")); p3.add(total); pSup.add(p1); pSup.add(p2); pSup.add(p3); JPanel pCen = new JPanel(new BorderLayout()); JPanel p4 = new JPanel(new FlowLayout(FlowLayout.LEFT)); p4.setBackground(Color.ORANGE); dados = new JLabel("Coleção:"); p4.add(dados); tabela = new JTable(4,2); tabela.setGridColor(Color.GRAY); tabela.setValueAt("Número Total de Pneus",0,0); tabela.setValueAt("Número Total de Câmbio Automático",1,0); tabela.setValueAt("Número Total de Direção Hidráulica",2,0); tabela.setValueAt("Número Total de Pintura Especial",3,0); pCen.add(p4,BorderLayout.NORTH); pCen.add(tabela,BorderLayout.CENTER); formatarTabela(); JPanel pInf = new JPanel(); pInf.setBackground(Color.LIGHT_GRAY); simular = new JButton("Simulação"); simular.addActionListener(this); pInf.add(simular); Container c = janela.getContentPane(); c.add(pSup,BorderLayout.NORTH); c.add(pCen,BorderLayout.CENTER); c.add(pInf,BorderLayout.SOUTH); janela.setSize(350,290); janela.setVisible(true); } private void formatarTabela() { tabela.setShowGrid(true); tabela.setGridColor(Color.LIGHT_GRAY); DefaultTableCellRenderer crEsq = new DefaultTableCellRenderer(); crEsq.setHorizontalAlignment(SwingConstants.LEFT); DefaultTableCellRenderer crCen = new DefaultTableCellRenderer(); crCen.setHorizontalAlignment(SwingConstants.CENTER); tabela.getColumnModel().getColumn(0).setCellRenderer(crEsq); tabela.getColumnModel().getColumn(1).setCellRenderer(crCen); tabela.getColumnModel().getColumn(0).setPreferredWidth(200); tabela.getColumnModel().getColumn(1).setPreferredWidth(20); tabela.validate(); } public int gerarAleatorio(int a, int b) { return ((int)(a + (b-a+1)*Math.random())); } private void autor() { JOptionPane.showMessageDialog(janela,"Autor: Nome do Aluno"); } private void simular() { int n = Integer.parseInt(total.getText()); for (int i = 0; i < n; i++) { int cat = gerarAleatorio(1,100); if (cat <= 30) { if (catA.isSelected()) { pneus = pneus + 2; if (sim.isSelected()) pneus++; } int valor = (int)veiculos.get(0); veiculos.set(0,valor+1); } else if (cat > 30 && cat <= 80) { if (catB.isSelected()) { pneus = pneus + 4; if (sim.isSelected()) pneus++; cambio++; direcao++; } int valor = (int)veiculos.get(1); veiculos.set(1,valor+1); } else if (cat > 80) { if (catC.isSelected()) { pneus = pneus + 4; if (sim.isSelected()) pneus++; cambio++; direcao++; pintura++; } int valor = (int)veiculos.get(2); veiculos.set(2,valor+1); } } resultados(); } private void resultados() { dados.setText("Coleção: " + veiculos); tabela.setValueAt(pneus,0,1); tabela.setValueAt(cambio,1,1); tabela.setValueAt(direcao,2,1); tabela.setValueAt(pintura,3,1); } private void limpar() { sim.setSelected(true); catA.setSelected(true); catB.setSelected(true); catC.setSelected(true); total.setText(""); dados.setText("Coleção: "); veiculos.clear(); veiculos.add(0); veiculos.add(0); veiculos.add(0); pneus = 0; cambio = 0; direcao = 0; pintura = 0; tabela.setValueAt("",0,1); tabela.setValueAt("",1,1); tabela.setValueAt("",2,1); tabela.setValueAt("",3,1); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == autor) { autor(); } else if (e.getSource() == simular) { simular(); } else if (e.getSource() == limpar) { limpar(); } else if (e.getSource() == fim) { System.exit(0); } } public static void main(String args[]) { new Lab13(); } }
Compartilhar