public void paintComponent(Graphics g)
{ g.setColor(foreground);
for(int i = 0; i < 20; i++)
for(int j = 0; j < 12; j++)
{
g.setColor(canvas[i][j].isColored() ? foreground : background);
g.fill3DRect(j * width, i * height, width, height, true);
} super.paintComponent(g);
}
{ g.setColor(foreground);
for(int i = 0; i < 20; i++)
for(int j = 0; j < 12; j++)
{
g.setColor(canvas[i][j].isColored() ? foreground : background);
g.fill3DRect(j * width, i * height, width, height, true);
} super.paintComponent(g);
}
import java.awt.*;
import javax.swing.JOptionPane;
public class Box implements Cloneable
{
private boolean isColored = true;
//indicates whether to show the box by the background of canvas
private Dimension dimension = new Dimension();
//include the width and height of box
/**
* construct a new Box by the default width and height which are specified in RussanSqaure
* @param isColored boolean. true indicates to show this box by the background of canvas
* */
public Box(boolean isColored)
{
this.isColored = isColored;
}
/**
* construct a new Box with the spcified isColored and width, height
* @param isColored boolean. true indicates to show this box by the background of canvas
* @param width int. spcify the width of each box
* @param height int. spcify the height of each box
* */
public Box(boolean isColored, int width, int height)
{
this.isColored = isColored;
this.dimension = new Dimension(width, height);
}
/**
* to detect whether this box is shown by the background of canvas
* @return true indicates this box will be shown by the background of canvas
* */
public boolean isColored()
{
return isColored;
}
/**
* to decide whether to set the color of box as the background of canvas or not
* @param isColored boolean. true to set the color of box as the background of canvas
* */
public void setColor(boolean isColored)
{
this.isColored = isColored;
}
/**
* to get the dimesion of box
* @param dimension of box
* */
public Dimension getDimension()
{
return this.dimension;
}
/**
* to custome the clone()method
* @param box Object
* */
public Object clone()
{
Object clonedBox = null;
try
{
clonedBox = super.clone();
}
catch(CloneNotSupportedException e)
{
JOptionPane.showConfirmDialog(null, "Clone is not supported", "Clone Not Supported" +
" Exception", JOptionPane.ERROR_MESSAGE);
}
return clonedBox;
} public static void main(String[] args) {
}
}
我按你的建议修改了,可是还是没有画出矩形来
import java.awt.*;
import java.awt.event.*;
public class Practice extends JPanel implements ComponentListener
{
private Box[][] canvas = new Box[20][12];
private Color foreground = Color.ORANGE;
private Color background = Color.BLACK;
private int width;
private int height; public Practice()
{
this.setLayout(new BorderLayout()); for(int i = 0; i < 20; i++)
for(int j = 0; j < 12; j++)
canvas[i][j] = new Box(false);
this.add(createButtonPanel(), BorderLayout.PAGE_END); width = getSize().width / 20;
height = getSize().height / 12;
this.addComponentListener(this);
}
public JPanel createButtonPanel()
{
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS)); //当用户点击show按钮时在JPanel中用foreground画出一个20*12的矩形矩阵
JButton show = new JButton("Show");
show.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("Show is clicked");
for(int i = 0; i < 20; i++)
for(int j = 0; j < 12; j++)
canvas[i][j].setColor(true);
repaint();
}
});
buttonPanel.add(show); //当用户点击show按钮时在JPanel中用foreground画出一个20*12的矩形矩阵
JButton hiden = new JButton("Hiden");
hiden.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("Hiden is clicked");
for(int i = 0; i < 20; i++)
for(int j = 0; j < 12; j++)
canvas[i][j].setColor(false);
repaint();
}
});
buttonPanel.add(hiden); return buttonPanel;
} public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(foreground);
for (int i = 0; i < 20; i++)
for (int j = 0; j < 12; j++) {
g.setColor(canvas[i][j].isColored() ? foreground : background);
g.fill3DRect(j * width, i * height, width, height, true);
}
} public static void createAndShowGUI()
{
//JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Paint in panel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.getContentPane().add("Center",new Practice());
//? frame.setContentPane(new Practice());
frame.setVisible(true);
} public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
public void componentResized(ComponentEvent e) {
System.out.println(width);
width = getSize().width / 20;
height = getSize().height / 12;
repaint();
}
public void componentMoved(ComponentEvent e) { }
public void componentShown(ComponentEvent e) { }
public void componentHidden(ComponentEvent e) { }
}
我按你说的修改了width和height,指定它们为20, 20
并把paintComponents()改成了paint()现在可以画出来了
可是却又另外一个问题
就是刚开始我的buttonPanel无法显示出来,要等到在按钮的位置点击一下按钮才会显示出来
然后马上又消失了.也就是说在panel变成看不到按钮了.
还有一个问题把width和height指定为20,20以后,panel上只有一部分能用于画方格.在画方格区域与两按钮之间还有一部分是空白区域.这个问题改如何调整.就是说如何才能让画方格区域为除两按钮区域外的其他区域.
还有为什么不能在buttonPanel中使用Box.createGlue()
完全按你上面给的代码做了修改以后可以按我预想的那样画出方格了.
但是不知道为什么出来的GUI最右边总有一点空白的区域,是不是 frame.setSize(500,500);
不太合适呢.我调调看.
谢了
zhang21cnboy(事了抚衣去,不留身与名)
好了结贴了.