[ Teslas @ 30.03.2009. 17:42 ] @
Potrebno je postaviti komponente pomocu GridBagLayout menadzera sto sam i uradio (Prozor sa imenom i prezimenom 2 labele i dva textfilda ima i dva dugmeta unesi i otkazi kada se pritisne unesi ceo prozor treba da se oboji u plavo a kada se klikne na otkazi da se zatvori prozor) Znam da to treba da se uradi pomocu CardLayouta da se naprave dve kartice ali nikako mi ne uspeva molim za pomoc?Nesto sam pokusavao ali za sada bezuspesno postavljam kod .Molim za pomoc. import java.awt.event.ItemEvent; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Prozor { JPanel cards; final static String Glavnipanel = ("prva"); final static String Unospanel = ("druga"); public static void addComponentsToPane( Container pane) { JPanel cards; JLabel label; JLabel label2; JTextField polje1; JTextField polje2; JButton button1; JButton button2; JPanel pCard = new JPanel(); pCard.setLayout(new GridBagLayout()); CardLayout c1= new CardLayout(); GridBagConstraints c = new GridBagConstraints(); label = new JLabel("Ime: "); c.gridx = 0; c.gridy = 0; c.insets= new Insets(0,20,0,0); pCard.add(label, c); label2 = new JLabel("Prezime: "); c.gridx = 0; c.gridy = 1; pCard.add(label2, c); c.insets= new Insets(0,20,0,0); polje1 = new JTextField(15); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 1; c.gridy = 0; c.insets= new Insets(10,0,5,40); pCard.add(polje1,c); polje2 = new JTextField(15); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 1; c.gridy = 1; c.insets= new Insets(10,0,5,40); pCard.add(polje2,c); button1= new JButton("Unesi"); c.gridx =1; c.gridy=3; c.insets= new Insets(5,10,10,160); pCard.add(button1,c); button1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent event) { // c.setBackground(Color.BLUE); }}); button2 = new JButton("Otkazi"); c.gridx =1; c.gridy=3; c.insets= new Insets(5,110,10,60); pCard.add(button2,c); button2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent event) { System.exit(0); } }); JPanel pCard1 = new JPanel(); pCard1.setBackground(Color.BLUE); cards = new JPanel(new CardLayout()); cards.add(pCard, "prva"); cards.add(pCard1,"druga"); pane.add(cards,BorderLayout.CENTER); } private static void createAndShowGUI() { //Create and set up the window. JFrame frame = new JFrame("[Proba]"); JFrame.setDefaultLookAndFeelDecorated(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(350,200); frame.setLocationRelativeTo(null); //Set up the content pane. addComponentsToPane(frame.getContentPane()); //Display the window. frame.pack(); frame.setVisible(true); } public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } } |