chessboard hw [message #1027858] |
Wed, 27 March 2013 14:07 |
uta f Messages: 1 Registered: March 2013 |
Junior Member |
|
|
For now I created a frame with gridlayout.
The problem is I can;t seem to figure out how to color the spaces between the squares, and I still need to add a tooltip to each square which should tell the mul of the specific square's row and col (rowxcol).
I don;t think I can do that for the frame so I am thinking about a panel
but the labels I add (as squares) are not shown...
this is my code:
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
import java.awt.Color;
import java.awt.GridLayout;
import java.util.Scanner;
public class Program {
/**
* @param args
*/
public static void main(String[] args) {
ShowGridLayout frame = new ShowGridLayout();
frame.setTitle("ShowGridLayout");
frame.setSize(300, 400);
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setAlwaysOnTop(true);
frame.setVisible(true);
}
}
class ShowGridLayout extends JFrame
{
public ShowGridLayout()
{
Scanner scan=new Scanner(System.in);
System.out.println("Please enter number of rows");
int rows=scan.nextInt();
System.out.println("Please enter number of columns");
int cols=scan.nextInt();
//Set GridLayout,rows,columns,gaps between components:horizontally,vertically
//setLayout(new GridLayout(rows, cols, 5, 5));
JPanel p1=new JPanel(new GridLayout(rows, cols));
JLabel label;
for(int i=0;i<rows;i++)
for(int j=0;j<cols;j++)
{
label=new JLabel(i+","+j);
label.setOpaque(true);
if((i+j)%2==0)
label.setBackground(Color.yellow);
else
label.setBackground(Color.blue);
p1.add(label);
}
// JLabel label=new JLabel(i+","+j);
// label.setOpaque(true);
//
// if((i+j)%2==0)
// label.setBackground(Color.yellow);
// else
// label.setBackground(Color.blue);
//
// add(label);
//}
}
}
in green is what I had previously (which worked) before I added the panel.
But I couldn;t add lines in between.
and yet again I am worried about the tooltip.
But adding the panel does not work and I don;t know why...
|
|
|
Re: chessboard hw [message #1028955 is a reply to message #1027858] |
Fri, 29 March 2013 00:49 |
Ed Merks Messages: 33141 Registered: July 2009 |
Senior Member |
|
|
This doesn't sound related to Eclipse at all. You're probably better to
try stackoverflow.
On 28/03/2013 11:52 AM, uta f wrote:
> For now I created a frame with gridlayout.
> The problem is I can;t seem to figure out how to color the spaces
> between the squares, and I still need to add a tooltip to each square
> which should tell the mul of the specific square's row and col (rowxcol).
>
> I don;t think I can do that for the frame so I am thinking about a panel
> but the labels I add (as squares) are not shown...
>
> this is my code:
>
> import javax.swing.JLabel;
> import javax.swing.JFrame;
> import javax.swing.JPanel;
> import javax.swing.border.LineBorder;
>
> import java.awt.Color;
> import java.awt.GridLayout;
> import java.util.Scanner;
>
> public class Program {
>
> /**
> * @param args
> */
> public static void main(String[] args) {
> ShowGridLayout frame = new ShowGridLayout();
> frame.setTitle("ShowGridLayout");
> frame.setSize(300, 400);
> frame.setLocationRelativeTo(null); // Center the frame
> frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
> frame.setAlwaysOnTop(true); frame.setVisible(true); }
>
> }
>
> class ShowGridLayout extends JFrame {
> public ShowGridLayout() {
>
> Scanner scan=new Scanner(System.in);
>
> System.out.println("Please enter number of rows");
> int rows=scan.nextInt();
>
> System.out.println("Please enter number of columns");
> int cols=scan.nextInt();
>
> //Set GridLayout,rows,columns,gaps between
> components:horizontally,vertically
>
> //setLayout(new GridLayout(rows, cols, 5, 5));
> JPanel p1=new JPanel(new GridLayout(rows, cols));
> JLabel label;
> for(int i=0;i<rows;i++)
> for(int j=0;j<cols;j++)
> {
> label=new JLabel(i+","+j);
> label.setOpaque(true);
>
> if((i+j)%2==0)
> label.setBackground(Color.yellow); else
> label.setBackground(Color.blue);
>
> p1.add(label);
>
> }
>
> // JLabel label=new JLabel(i+","+j);
> // label.setOpaque(true);
> //
> // if((i+j)%2==0)
> // label.setBackground(Color.yellow);
> // else
> // label.setBackground(Color.blue);
> //
> // add(label);
> //}
>
>
> }
> }
>
>
> in green is what I had previously (which worked) before I added the
> panel.
> But I couldn;t add lines in between.
> and yet again I am worried about the tooltip.
>
> But adding the panel does not work and I don;t know why...
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
|
Powered by
FUDForum. Page generated in 0.06251 seconds