Results 1 to 6 of 6
Thread: Pls help!!!
- 04-29-2011, 12:31 AM #1
Pls help!!!
I wrote these codes that i can select a number. this number defines number of rows and columns of gridlayout and number of buttons in panel. it works. but when selecting new number, the old one doesnt disappear and the new grid appears back side. please help. i have to do this for my homework
Java Code:import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class lightsout extends JFrame{ private String[] number = {"3", "4", "5", "6", "7", "8", "9", "10"}; private JComboBox jcb = new JComboBox(number); JPanel p2 = new JPanel(); private JPanel p1; JLabel jlb = new JLabel("Enter a number:"); public static void main(String[]args){ lightsout frame = new lightsout(); frame.setTitle("Lights Out"); frame.setSize(500,300); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public lightsout() { setDisplay(0); p2.setLayout(new BorderLayout(25,25)); p2.add(jlb, BorderLayout.WEST); p2.add(jcb, BorderLayout.CENTER); add(p2, BorderLayout.NORTH); jcb.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { setDisplay(jcb.getSelectedIndex()); } }); } public void setDisplay(int index) { p1 = new JPanel(); int n; n = Integer.parseInt(number[index]); p1.setLayout(new GridLayout(n,n,0,0)); for (int i = 1; i <= n*n; i++) { p1.add(new JButton()); } add(p1, BorderLayout.CENTER); } }Last edited by Fubarable; 04-29-2011 at 12:38 AM. Reason: Moderator Action: code tags added
-
In the setDisplay method, I would first remove p1 from the contentPane if p1 is not null, then create a new p1 as you're doing, add it to the contentPane, revalidate the contentPane (you'll need to cast it to a JPane first) and then repaint the contentPane.
Also, I added code tags added to your post above to help make it readable.
To the OP, in the future, to do this yourself, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.
Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:
Best of luckJava Code:[code] // your code goes here // notice how the top and bottom tags are different [/code]
- 04-29-2011, 11:31 AM #3
Sorry for code tags. i'm new in the forum.
I understand what you say but i dont know how i write it. if you write i will be very glad.
-
- 04-30-2011, 01:24 AM #5
thanks for suggestion. i made it :) i have to write Lights Out game. Now i going t add game rules. if i will have a problem, i write here. thanks again..
- 05-01-2011, 01:19 AM #6
i can change background of selected buton but i couldn't change its neighbour buttons. i cant use array.Java Code:import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class lightsout extends JFrame{ private JTextField jtf = new JTextField(15); private JPanel p3 = new JPanel(); JPanel p2 = new JPanel(); private JPanel p1; private Object[] matrix; JLabel jlb = new JLabel("Enter a number:"); public static void main(String[]args){ lightsout frame = new lightsout(); frame.setTitle("Lights Out"); frame.setSize(800,500); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public lightsout() { add(p3, BorderLayout.CENTER); p3.setLayout(new BorderLayout(5,5)); setDisplay("2"); p2.setLayout(new BorderLayout(25,25)); p2.add(jlb, BorderLayout.WEST); p2.add(jtf, BorderLayout.CENTER); p3.add(p2, BorderLayout.NORTH); jtf.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { setDisplay(jtf.getText()); } }); } public void setDisplay(String index) { if(p1 != null){ p3.remove(p1); matrix = null; } p1 = new JPanel(); int n; n = Integer.parseInt(index); matrix = new Object[n*n]; p1.setLayout(new GridLayout(n,n,0,0)); for(int i = 0; i < matrix.length; i++){ matrix[i] = new MyButton(Integer.toString(i)); } for(int i = 0; i < matrix.length; i++){ p1.add((MyButton)matrix[i]); } /*for (int i = 1; i <= n*n; i++) { p1.add(new MyButton(Integer.toString(i))); }*/ p3.add(p1, BorderLayout.CENTER); p3.revalidate(); p3.repaint(); } class MyButton extends JButton{ private ImageIcon blcIcon = new ImageIcon("black.jpg"); private ImageIcon prsIcon = new ImageIcon("pressed.jpg"); private ImageIcon whtIcon = new ImageIcon("white.jpg"); public MyButton(String s){ super.setText(s); setHorizontalTextPosition(CENTER); setForeground(Color.WHITE); setIcon(blcIcon); setPressedIcon(prsIcon); addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { toogle(); } }); } private void toogle(){ if(getIcon() == blcIcon){ setIcon(whtIcon); setForeground(Color.BLACK); } else if(getIcon() == whtIcon){ setIcon(blcIcon); setForeground(Color.WHITE); } } } }


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks