0
격자 레이아웃 프레임에 5 개의 레이블이 있습니다. gridlayout 테두리 레이아웃을 배열 (예 : String gborders [] = { "North", "Center", "South", "East", "West"};)에서 위치 이름을 사용하여 변경하고 레이블을 열거하고 싶습니다. 위치. 가능한가?배열에서 값의 테두리 레이아웃 위치가 가능합니까?
는package w1;
import java.awt.*;
import javax.swing.border.*;
import javax.swing.*;
public class LayShow {
private static JLabel lLabel;
public static void main(String[] args) {
int t = 15;
String lmNames[] = {"Label 1", "Label 2",
"Label 3", "Label 4", "Label 5"};
String gborders[] = { "North", "Center", "South", "East", "West" };
Color colors[] = { new Color(11, 125, 155), new Color(155, 55, 200),
new Color(201, 245, 145), new Color(255, 255, 140),
new Color(161, 224, 224), new Color(11, 125, 155) };
Font fonts[] = {new Font("SansSerif", Font.BOLD, t),
new Font("Arial", Font.BOLD, t+1),
new Font("SansSerif", Font.PLAIN, t+2),
new Font("SansSerif", Font.ITALIC, t+3),
new Font("SansSerif", Font.ITALIC, t+4)};
JFrame frame = new JFrame("Frame");
frame.setLayout(new GridLayout(0, 2));
for (int i = 0; i < lmNames.length; i++) {
final Border
borderColor = BorderFactory.createLineBorder(colors[i+1]);
JPanel p = new JPanel();
p.setBackground(colors[i]);
p.setBorder(BorderFactory.createTitledBorder(borderColor , lmNames[i]));
lLabel = new JLabel("label number: " + (i+1));
p.add(lLabel);
lLabel.setFont(fonts[i]);
lLabel.setForeground(colors[i+1]);
lLabel.setToolTipText("ToolTip for label number: " + (i+1));
frame.add(p);
}
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
그래서 어떤 문제가 있습니까? – stark
lLabel은 정적 변수로 선언하지 말고 메서드의 로컬 변수로 선언해야합니다. 가능한 한 많이 비 정적 정적 변수를 피하도록 노력해야합니다. 그것들은 모든 악의 근원이며 흔히 메모리 누출로 이어집니다. –