我有一个Swing
类,该类生成一些图形和2个按钮。提供了代码。
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.*;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
public class GraphicsDemo_noborders_ML {
/* GoKart Colors
String color1 = "#000000";
String color2 = "#66CCCC";
String color3 = "#B7D108";
String color4 = "#E61E3F";
String color5 = "#FBEC52";
String color6 = "#9973AD";
String color7 = "#F9A226";
String color8 = "#6299EB";
String colorBG= "#ffffff";
*/
/* GoKart Colors
String color1 = "#000000";
String color2 = "#ff6672";
String color3 = "#B7D999";
String color4 = "#E61999";
String color5 = "#FBEC52";
String color6 = "#997999";
String color7 = "#F9A226";
String color8 = "#6299EB";
String colorBG= "#ffffff";
*/
/* Blue Corn
String color1 = "#344152";
String color2 = "#344152";
String color3 = "#344152";
String color4 = "#344152";
String color5 = "#344152";
String color6 = "#344152";
String color7 = "#344152";
String color8 = "#344152";
String colorBG= "#ffffff";
*/
/* White
String color1 = "#ffffff";
String color2 = "#ffffff";
String color3 = "#ffffff";
String color4 = "#ffffff";
String color5 = "#ffffff";
String color6 = "#ffffff";
String color7 = "#ffffff";
String color8 = "#ffffff";
String colorBG= "#000000";
*/
private static final long serialVersionUID = 1L;
int sizeBig = 150;
final static int canvasW = 1300;
final static int canvasH = 750;
int initialS = 1;
int numberS = 40;
int smallsizeVAR = sizeBig / 3;
int border1 = 0;
Random rand;
/* Black */
String color1 = "#000000";
String color2 = "#000000";
String color3 = "#000000";
String color4 = "#000000";
String color5 = "#000000";
String color6 = "#000000";
String color7 = "#000000";
String color8 = "#000000";
String colorBG = "#ffffff";
public GraphicsDemo_noborders_ML() {
rand = new Random();
}
public void paint(final Graphics g) {
g.setColor(Color.decode(colorBG));
g.fillRect(0, 0, canvasW, canvasH);
while (initialS <= numberS) {
final int randX = rand.nextInt(canvasW + sizeBig) + (0 - sizeBig);
final int randY = rand.nextInt(canvasH + sizeBig) + (0 - sizeBig);
final int sizeRANDx = rand.nextInt(((sizeBig + 1) - smallsizeVAR) + 1) + smallsizeVAR;
final int sizeRANDy = sizeRANDx;
final String[] arr = {color2, color3, color4, color5, color6, color7, color8};
final Random random = new Random();
final int select = random.nextInt(arr.length);
g.setColor(Color.decode(arr[select]));
g.fillRect(randX, randY, sizeRANDx, sizeRANDy);
if (sizeRANDx > sizeRANDy) {
border1 = (sizeRANDx - ((4 * sizeRANDx) / 5)) / 2;
} else {
border1 = (sizeRANDy - ((4 * sizeRANDy) / 5)) / 2;
}
g.setColor(Color.decode(colorBG));
g.fillRect(randX + border1, randY + border1, sizeRANDx - (2 * border1), sizeRANDy - (2 * border1));
initialS++;
if (initialS == numberS + 1) {
g.setColor(Color.decode(arr[select]));
g.fillRect(canvasW / 8, canvasW / 8, (3 * sizeBig) / 2, (3 * sizeBig) / 2);
}
}
}
public static void main(final String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
final GraphicsDemo_noborders_ML demo = new GraphicsDemo_noborders_ML();
JFrame jFrame = new JFrame();
jFrame.setTitle("Latest Programic Art");
jFrame.setSize(canvasW, canvasH);
JButton save = new JButton("Save");
JButton run = new JButton("Run Again");
save.setBounds(50, 100, 100, 50);
run.setBounds(50, 500, 100, 50);
JPanel panel = new JPanel();
panel.add(save);
panel.add(run);
JComponent canvas = new JComponent() {
@Override
protected void paintComponent(Graphics g) {
demo.paint(g);
}
};
jFrame.getContentPane().add(panel);
jFrame.getContentPane().add(canvas);
jFrame.setVisible(true);
jFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
});
}
}
图形生成很好,但是,当我最小化窗口时,它就消失了。 其次,我看不到按钮,但是如果我提供了反向添加的顺序,
jFrame.getContentPane().add(canvas);
jFrame.getContentPane().add(panel);
我可以看到按钮,但看不到图纸。
如何更正代码?
答案 0 :(得分:1)
while (initialS <= numberS) {
每当Swing确定需要重新绘制组件时,都会调用paintComponent()方法。例如,当您还原框架时,将调用paintComponent()方法。
在您的情况下,您不会将“ initialS”变量重置为0,因此循环永远不会第二次执行。因此,您需要在进入循环之前重置变量。
save.setBounds(50, 100, 100, 50);
run.setBounds(50, 500, 100, 50);
JPanel panel = new JPanel();
panel.add(save);
panel.add(run);
setBounds(...)不执行任何操作。您将组件添加到JPanel。默认情况下,JPanel使用FlowLayout。布局管理器将根据布局管理器的规则重置边界。
不要尝试设置边界。让布局管理器完成其工作。
第二,我看不到按钮,但是如果我提供了反向添加的顺序,
JFrame的默认布局管理器是BorderLayout。如果不指定约束,则将组件添加到CENTER。问题是您只能将一个组件添加到CENTER,因此只有最后添加的组件可见。
代替尝试:
jFrame.getContentPane().add(canvas, BorderLayout.CENTER);
jFrame.getContentPane().add(panel, BorderLayout.PAGE_START);
查看两个组件。
此外,通常您会:
getPreferredSize()
方法以返回组件的大小,以便布局管理器可以正常工作。阅读Custom Painting的Swing教程中的部分,以获取更多信息和工作示例。