我有 4 个不同的 JLabel 和两个按钮,一个按钮应该将颜色向右更改为蓝色,另一个按钮将颜色改回
I have 4 different JLabels and two buttons one button should change the color to the right to blue and the other button changes the color back
我试图在单击按钮 (actionEvent) 时将颜色更改为蓝色标签。我有四个不同的标签和两个按钮。如果单击带有右侧箭头的按钮,则右侧的标签会从橙色变为蓝色,如果再次单击该按钮,则依此类推。如果单击左侧的按钮,则左侧的标签会从橙色变为蓝色,如果再次单击则依此类推。我不知道我是否在动作侦听器中做错了什么,但是当单击按钮时,happens.I 不会 post 本段下方的分配说明,因此希望它更有意义。
在本实验中,您将创建一个类似于下图的 GUI。在左侧,您有一个包含两个按钮的控制面板:一个带有向左箭头,一个带有向右箭头。每当您单击向右箭头时,蓝色方块向右移动(数字保持不变) 每当您单击向左箭头时,蓝色方块向左移动。如果蓝色方块在位置 1 上并单击左箭头,蓝色方块将移动到最右边(位置 4)右箭头类似。
Desired Output:
package guiLayout;
/**
* In this JFrame my goal is to be able to make the color of the labels
* change on the JButton click with an action listener.
*
* @author Kody Berry
* @since 7-4-2020
*/
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Color;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class LabGuiLayout extends JFrame {
// Fields
private static final long serialVersionUID = 1L;
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LabGuiLayout frame = new LabGuiLayout();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public LabGuiLayout() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 500, 200);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
// Creating instances of methods and adding it to JPannel.
JButton btnNewButton = moveLeftButton();
contentPane.add(btnNewButton);
JLabel lblNewLabel = labelOne();
contentPane.add(lblNewLabel);
JLabel lblNewLabel_1 = labelTwo();
contentPane.add(lblNewLabel_1);
JLabel lblNewLabel_2 = labelThree();
contentPane.add(lblNewLabel_2);
JButton button = moveRightButton();
contentPane.add(button);
JLabel label = labelFour();
contentPane.add(label);
JLabel lblNewLabel_3 = demoLayoutLabel();
contentPane.add(lblNewLabel_3);
}
/**
* Title Label.
*
* @return Returns lblNewLabel_3.
*/
private JLabel demoLayoutLabel() {
JLabel lblNewLabel_3 = new JLabel("Demo Layout");
lblNewLabel_3.setFont(new Font("Tahoma", Font.PLAIN, 23));
lblNewLabel_3.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel_3.setOpaque(true);
lblNewLabel_3.setBounds(0, 0, 490, 46);
return lblNewLabel_3;
}
/**
* Number 1 label.
*
* @return Returns lblNewLabel.
*/
private JLabel labelOne() {
JLabel lblNewLabel = new JLabel(" 1");
lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 26));
lblNewLabel.setOpaque(true);
lblNewLabel.setBounds(91, 45, 86, 93);
lblNewLabel.setBackground(Color.BLUE);
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
return lblNewLabel;
}
/**
* Number 2 label.
*
* @return Returns lblNewLabel_1.
*/
private JLabel labelTwo() {
JLabel lblNewLabel_1 = new JLabel("2");
lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 26));
lblNewLabel_1.setOpaque(true);
lblNewLabel_1.setBounds(187, 45, 86, 93);
lblNewLabel_1.setBackground(Color.ORANGE);
lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
return lblNewLabel_1;
}
/**
* Number 3 label.
*
* @return Returns lblNewLabel_2.
*/
private JLabel labelThree() {
JLabel lblNewLabel_2 = new JLabel("3");
lblNewLabel_2.setFont(new Font("Tahoma", Font.PLAIN, 26));
lblNewLabel_2.setOpaque(true);
lblNewLabel_2.setBounds(283, 45, 86, 93);
lblNewLabel_2.setBackground(Color.ORANGE);
lblNewLabel_2.setHorizontalAlignment(SwingConstants.CENTER);
return lblNewLabel_2;
}
/**
* Number 4 label.
*
* @return Returns label.
*/
private JLabel labelFour() {
JLabel label = new JLabel("4");
label.setFont(new Font("Tahoma", Font.PLAIN, 26));
label.setOpaque(true);
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setBackground(Color.ORANGE);
label.setBounds(379, 45, 86, 93);
return label;
}
/**
* Move to the left button that when clicked will change the button to the left
* to blue and the previous button to orange. Once the last label on the left is
* blue if the button is clicked again it will start over.
*
* @return Returns btnNewButton.
*/
private JButton moveLeftButton() {
// Array of labels.
JLabel[] jl = { labelOne(), labelTwo(), labelThree(), labelFour() };
JButton btnNewButton = new JButton("<--");
btnNewButton.addActionListener(new ActionListener() {
int clicks = 0;
public void actionPerformed(ActionEvent e) {
switch (++clicks) {
case 1:
jl[0].setBackground(Color.ORANGE);
jl[3].setBackground(Color.BLUE);
break;
case 2:
jl[3].setBackground(Color.ORANGE);
jl[2].setBackground(Color.BLUE);
break;
case 3:
jl[2].setBackground(Color.ORANGE);
jl[1].setBackground(Color.BLUE);
break;
case 4:
jl[1].setBackground(Color.BLUE);
jl[0].setBackground(Color.ORANGE);
break;
default:
clicks = 1;
break;
}
}
});
btnNewButton.setBounds(32, 45, 49, 23);
return btnNewButton;
}
/**
* Move to the right button that when clicked will change the button to the
* right to blue and the previous button to orange. Once the last label on the
* right is blue if the button is clicked again it will start over.
*
* @return Returns button.
*/
private JButton moveRightButton() {
// Array of labels.
JLabel[] jl = { labelOne(), labelTwo(), labelThree(), labelFour() };
JButton button = new JButton("-->");
button.addActionListener(new ActionListener() {
int clicks = 0;
public void actionPerformed(ActionEvent e) {
switch (++clicks) {
case 1:
jl[0].setBackground(Color.ORANGE);
jl[1].setBackground(Color.BLUE);
break;
case 2:
jl[1].setBackground(Color.ORANGE);
jl[2].setBackground(Color.BLUE);
break;
case 3:
jl[2].setBackground(Color.ORANGE);
jl[3].setBackground(Color.BLUE);
break;
case 4:
jl[0].setBackground(Color.BLUE);
jl[1].setBackground(Color.ORANGE);
break;
default:
clicks = 1;
break;
}
}
});
button.setBounds(32, 79, 49, 23);
return button;
}
}
JLabel lblNewLabel = labelOne();
contentPane.add(lblNewLabel);
您创建标签并将其添加到框架。
但是在 ActionListener 中你有:
JLabel[] jl = { labelOne(), labelTwo(), labelThree(), labelFour() };
为每个标签创建 4 个新实例。这些标签没有添加到框架中,因此更改它们的背景没有任何作用。
相反,您需要将数组定义为 class 的实例变量。然后你的 ActionListener 可以访问数组中的标签。
因此在构造函数中,当您创建标签并将其添加到框架时,您还需要将其添加到数组中。
我试图在单击按钮 (actionEvent) 时将颜色更改为蓝色标签。我有四个不同的标签和两个按钮。如果单击带有右侧箭头的按钮,则右侧的标签会从橙色变为蓝色,如果再次单击该按钮,则依此类推。如果单击左侧的按钮,则左侧的标签会从橙色变为蓝色,如果再次单击则依此类推。我不知道我是否在动作侦听器中做错了什么,但是当单击按钮时,happens.I 不会 post 本段下方的分配说明,因此希望它更有意义。
在本实验中,您将创建一个类似于下图的 GUI。在左侧,您有一个包含两个按钮的控制面板:一个带有向左箭头,一个带有向右箭头。每当您单击向右箭头时,蓝色方块向右移动(数字保持不变) 每当您单击向左箭头时,蓝色方块向左移动。如果蓝色方块在位置 1 上并单击左箭头,蓝色方块将移动到最右边(位置 4)右箭头类似。
Desired Output:
package guiLayout;
/**
* In this JFrame my goal is to be able to make the color of the labels
* change on the JButton click with an action listener.
*
* @author Kody Berry
* @since 7-4-2020
*/
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Color;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class LabGuiLayout extends JFrame {
// Fields
private static final long serialVersionUID = 1L;
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LabGuiLayout frame = new LabGuiLayout();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public LabGuiLayout() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 500, 200);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
// Creating instances of methods and adding it to JPannel.
JButton btnNewButton = moveLeftButton();
contentPane.add(btnNewButton);
JLabel lblNewLabel = labelOne();
contentPane.add(lblNewLabel);
JLabel lblNewLabel_1 = labelTwo();
contentPane.add(lblNewLabel_1);
JLabel lblNewLabel_2 = labelThree();
contentPane.add(lblNewLabel_2);
JButton button = moveRightButton();
contentPane.add(button);
JLabel label = labelFour();
contentPane.add(label);
JLabel lblNewLabel_3 = demoLayoutLabel();
contentPane.add(lblNewLabel_3);
}
/**
* Title Label.
*
* @return Returns lblNewLabel_3.
*/
private JLabel demoLayoutLabel() {
JLabel lblNewLabel_3 = new JLabel("Demo Layout");
lblNewLabel_3.setFont(new Font("Tahoma", Font.PLAIN, 23));
lblNewLabel_3.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel_3.setOpaque(true);
lblNewLabel_3.setBounds(0, 0, 490, 46);
return lblNewLabel_3;
}
/**
* Number 1 label.
*
* @return Returns lblNewLabel.
*/
private JLabel labelOne() {
JLabel lblNewLabel = new JLabel(" 1");
lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 26));
lblNewLabel.setOpaque(true);
lblNewLabel.setBounds(91, 45, 86, 93);
lblNewLabel.setBackground(Color.BLUE);
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
return lblNewLabel;
}
/**
* Number 2 label.
*
* @return Returns lblNewLabel_1.
*/
private JLabel labelTwo() {
JLabel lblNewLabel_1 = new JLabel("2");
lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 26));
lblNewLabel_1.setOpaque(true);
lblNewLabel_1.setBounds(187, 45, 86, 93);
lblNewLabel_1.setBackground(Color.ORANGE);
lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
return lblNewLabel_1;
}
/**
* Number 3 label.
*
* @return Returns lblNewLabel_2.
*/
private JLabel labelThree() {
JLabel lblNewLabel_2 = new JLabel("3");
lblNewLabel_2.setFont(new Font("Tahoma", Font.PLAIN, 26));
lblNewLabel_2.setOpaque(true);
lblNewLabel_2.setBounds(283, 45, 86, 93);
lblNewLabel_2.setBackground(Color.ORANGE);
lblNewLabel_2.setHorizontalAlignment(SwingConstants.CENTER);
return lblNewLabel_2;
}
/**
* Number 4 label.
*
* @return Returns label.
*/
private JLabel labelFour() {
JLabel label = new JLabel("4");
label.setFont(new Font("Tahoma", Font.PLAIN, 26));
label.setOpaque(true);
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setBackground(Color.ORANGE);
label.setBounds(379, 45, 86, 93);
return label;
}
/**
* Move to the left button that when clicked will change the button to the left
* to blue and the previous button to orange. Once the last label on the left is
* blue if the button is clicked again it will start over.
*
* @return Returns btnNewButton.
*/
private JButton moveLeftButton() {
// Array of labels.
JLabel[] jl = { labelOne(), labelTwo(), labelThree(), labelFour() };
JButton btnNewButton = new JButton("<--");
btnNewButton.addActionListener(new ActionListener() {
int clicks = 0;
public void actionPerformed(ActionEvent e) {
switch (++clicks) {
case 1:
jl[0].setBackground(Color.ORANGE);
jl[3].setBackground(Color.BLUE);
break;
case 2:
jl[3].setBackground(Color.ORANGE);
jl[2].setBackground(Color.BLUE);
break;
case 3:
jl[2].setBackground(Color.ORANGE);
jl[1].setBackground(Color.BLUE);
break;
case 4:
jl[1].setBackground(Color.BLUE);
jl[0].setBackground(Color.ORANGE);
break;
default:
clicks = 1;
break;
}
}
});
btnNewButton.setBounds(32, 45, 49, 23);
return btnNewButton;
}
/**
* Move to the right button that when clicked will change the button to the
* right to blue and the previous button to orange. Once the last label on the
* right is blue if the button is clicked again it will start over.
*
* @return Returns button.
*/
private JButton moveRightButton() {
// Array of labels.
JLabel[] jl = { labelOne(), labelTwo(), labelThree(), labelFour() };
JButton button = new JButton("-->");
button.addActionListener(new ActionListener() {
int clicks = 0;
public void actionPerformed(ActionEvent e) {
switch (++clicks) {
case 1:
jl[0].setBackground(Color.ORANGE);
jl[1].setBackground(Color.BLUE);
break;
case 2:
jl[1].setBackground(Color.ORANGE);
jl[2].setBackground(Color.BLUE);
break;
case 3:
jl[2].setBackground(Color.ORANGE);
jl[3].setBackground(Color.BLUE);
break;
case 4:
jl[0].setBackground(Color.BLUE);
jl[1].setBackground(Color.ORANGE);
break;
default:
clicks = 1;
break;
}
}
});
button.setBounds(32, 79, 49, 23);
return button;
}
}
JLabel lblNewLabel = labelOne();
contentPane.add(lblNewLabel);
您创建标签并将其添加到框架。
但是在 ActionListener 中你有:
JLabel[] jl = { labelOne(), labelTwo(), labelThree(), labelFour() };
为每个标签创建 4 个新实例。这些标签没有添加到框架中,因此更改它们的背景没有任何作用。
相反,您需要将数组定义为 class 的实例变量。然后你的 ActionListener 可以访问数组中的标签。
因此在构造函数中,当您创建标签并将其添加到框架时,您还需要将其添加到数组中。