GridLayout , /, , , .
...
- WrapLayout,

...
GridBagLayout, /, ,

... GridBagConstraints#insets,

... GridBagConstraints#weightx, GridBagConstraints#weightx GridBagConstraints#fill,

, GridLayout GridBagLayout
... ...
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.text.NumberFormat;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TexturePane extends JPanel {
public TexturePane(Color background) {
setBackground(background);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(20, 20);
}
}
public class TestPane extends JPanel {
public TestPane() {
int total = 10 * 10;
Color[] rowColors = new Color[]{
Color.RED,
Color.GREEN,
Color.BLUE,
Color.CYAN,
Color.MAGENTA,
Color.ORANGE,
Color.PINK,
Color.YELLOW,
Color.DARK_GRAY,
Color.GRAY,
};
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(1, 1, 1, 1);
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
for (int row = 0; row < 10; row++) {
gbc.gridy = row;
for (int col = 0; col < 10; col++) {
gbc.gridx = col;
double progress = (row * 10) + col;
Color color = blend(Color.BLACK, rowColors[row], col / 10d);
add(new TexturePane(color), gbc);
}
}
}
}
public static Color blend(Color color1, Color color2, double ratio) {
float r = (float) ratio;
float ir = (float) 1.0 - r;
float rgb1[] = new float[3];
float rgb2[] = new float[3];
color1.getColorComponents(rgb1);
color2.getColorComponents(rgb2);
float red = rgb1[0] * r + rgb2[0] * ir;
float green = rgb1[1] * r + rgb2[1] * ir;
float blue = rgb1[2] * r + rgb2[2] * ir;
if (red < 0) red = 0;
else if (red > 255) red = 255;
if (green < 0) green = 0;
else if (green > 255) green = 255;
if (blue < 0) blue = 0;
else if (blue > 255) blue = 255;
Color color = null;
try {
color = new Color(red, green, blue);
} catch (IllegalArgumentException exp) {
NumberFormat nf = NumberFormat.getNumberInstance();
System.out.println(nf.format(red) + "; " + nf.format(green) + "; " + nf.format(blue));
}
return color;
}
}
WrapLayout GridBagLayout , ,