Programador Java Interfaz Gráfico GUI AWT Swing 1 Swing 2
JList Java Interfaz Gráfico GUI AWT Swing 1 Swing 2 |
|
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class Jlist extends JFrame { private JLabel jLabel1 = new JLabel(); private JList jList1 = new JList(); private DefaultListModel jList1Model = new DefaultListModel(); private JScrollPane jList1ScrollPane = new JScrollPane(jList1); private JLabel jLabel2 = new JLabel(); private JButton jButton1 = new JButton(); private ImageIcon jButton1Icon = new ImageIcon("../../la.png"); public Jlist(String title) { super(title); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); int frameWidth = 300; int frameHeight = 300; setSize(frameWidth, frameHeight); Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); int x = (d.width - getSize().width) / 2; int y = (d.height - getSize().height) / 2; setLocation(x, y); setResizable(false); Container cp = getContentPane(); cp.setLayout(null); cp.setBackground(new Color(0x34CB5A)); jLabel1.setBounds(72, 8, 147, 57); jLabel1.setText("JList"); jLabel1.setForeground(Color.CYAN); jLabel1.setFont(new Font("3D", Font.BOLD, 48)); cp.add(jLabel1); jList1.setModel(jList1Model); jList1ScrollPane.setBounds(48, 56, 169, 137); jList1Model.addElement("Rojo"); jList1Model.addElement("Amarillo"); jList1Model.addElement("Verde"); cp.add(jList1ScrollPane); jLabel2.setBounds(32, 208, 227, 25); jLabel2.setText("Selección de Color:"); cp.add(jLabel2); jButton1.setBounds(104, 232, 113, 49); jButton1.setText("OK"); jButton1.setMargin(new Insets(2, 2, 2, 2)); jButton1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { jButton1_ActionPerformed(evt); } }); jButton1.setIcon(jButton1Icon); cp.add(jButton1); setVisible(true); } public void jButton1_ActionPerformed(ActionEvent evt) { String mensaje; if (jList1.getSelectedIndex()==-1) { mensaje="No Seleccionaste un Color."; } else { mensaje="Color seleccionado es: "+jList1.getSelectedValue().toString(); } jLabel2.setText(mensaje); } public static void main(String[] args) { new Jlist("Jlist"); } } |
No hay comentarios:
Publicar un comentario