-2
나는 학생 프로그래머이며 프로젝트를 끝낼 수 있도록이 코드를 수정해야합니다.레이블에 이름을 지정하는 방법은 무엇입니까?
루프 레이블에 대해 고유 한 이름을 지정하는 다른 방법이 있습니까?
사용자가 라벨 중 하나를 클릭 할 때마다 텍스트의 색상이 변경되기 때문입니다. 모든 레이블이 아니라 사용자가 클릭 한 레이블 만 있습니다.
public void addComponentsToPane(final JPanel pane) {
if (RIGHT_TO_LEFT) {
pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}
pane.setLayout(new GridBagLayout());
c.insets = new Insets(20, 20, 5, 5);
c.anchor = GridBagConstraints.NORTH;
if (shouldFill) {
//natural height, maximum width
c.fill = GridBagConstraints.HORIZONTAL;
}
int i;
for (i = 1; i <= 80; i++) {
if (z == 14) {
y++;
x = 0;
z = 0;
}
try {
label = new JLabel("# " + i);
labels();
c.weightx = 0.5;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = x;
c.gridy = y;
pane.add(label, c);
x++;
z++;
set_x = x;
set_y = y;
} catch (Exception e) {
}
}
tableNum = i;
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
int UserInput = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter Number of Table:"));
int i;
for (i = tableNum; i <= (tableNum + UserInput) - 1; i++) {
if (z == 14) {
set_y++;
set_x = 0;
z = 0;
}
try {
label = new JLabel("# " + i);
labels();
c.weightx = 0.5;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = set_x;
c.gridy = set_y;
pane.add(label, c);
set_x++;
z++;
lbl[i] = label;
// lbl[i].setForeground(Color.red);
System.out.print(lbl[i].getText());
// set_x = x;
// set_y = y;
} catch (Exception ex) {
}
}
tableNum = i;
frame.revalidate();
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "PLease Input Digits Only", "Input Error", JOptionPane.ERROR_MESSAGE);
}
}
});
}
private void labels() {
ImageIcon icon = new ImageIcon(imagePath + labelIconName);
icon.getImage().flush();
label.setIcon(icon);
label.setBackground(Color.WHITE);
label.setPreferredSize(new Dimension(80, 80));
label.setFont(new Font("Serif", Font.PLAIN, 18));
label.setForeground(Color.black);
label.setVerticalTextPosition(SwingConstants.BOTTOM);
label.setHorizontalTextPosition(SwingConstants.CENTER);
// label.setBorder(BorderFactory.createBevelBorder(0));
label.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
// String[] option = {"Available", "Occupied", "Reserved"};
// choose = (String) JOptionPane.showInputDialog(this, "Table :"+label.getText() + , "Choose transaction", JOptionPane.INFORMATION_MESSAGE, null, option, option[0]);
System.out.print(label.getName());
centerScreen();
selectAllMenu();
jDialogMenu.show();
// frame.setDefaultCloseOperation(JInternalFrame.DO_NOTHING_ON_CLOSE);
}
});
}
Pls help 및 ASAP는 도움이 필요하지 않습니다. 존경심을 표시하고 가장 중요한 것은 시도한 것입니다. [자주 묻는 질문] (두 번째 메모에서 정확히 무엇을 원하는지 자세히 설명하면 도움이 될 것입니다. 솔직히 말해서, 내가 어떤 질문을했는지는 모르겠지만 ...) – ppeterka
당신은 무엇을 의미합니까? 'unique namea'로 작성 – exexzian
경미한 힌트 : 코드 샘플에서 관련성없는 코드를 제거하여 읽기 쉽도록해야합니다. (이 경우, 모든 레이아웃 코드와 대부분의 레이블 스타일링 등은 기본 설정 만 유지하고, 더미 텍스트를 추가하고, 작동해야하는 코드 부분을 추가하지만 그렇지 않습니다.) – millimoose