Java 我有4个不同的JLabel和两个按钮,一个按钮应该将右侧的颜色更改为蓝色,另一个按钮将颜色更改回蓝色
我试图使它在一个按钮点击(动作事件)的颜色改变标签为蓝色。我有四个不同的标签和两个按钮。如果单击向右箭头的按钮,则如果再次单击按钮,则右侧的标签将从橙色变为蓝色,依此类推。如果单击左侧的按钮,则如果再次单击,左侧的标签将从橙色变为蓝色,依此类推。我不知道我在action listeners中是否做错了什么,但是当按钮被点击时,什么都没有发生。我将在这段下面发布作业说明,希望它更有意义 在本实验室中,您将创建一个与下图类似的GUI。在左侧,您有一个控制面板,其中包括两个按钮:一个带有左箭头,另一个带有右箭头。单击向右箭头时,蓝色平铺将向右移动(数字保持不变);单击向左箭头时,蓝色平铺将向左移动。如果蓝色平铺位于位置1,单击左箭头,则蓝色平铺将移动到最右侧(位置4),右箭头的情况类似Java 我有4个不同的JLabel和两个按钮,一个按钮应该将右侧的颜色更改为蓝色,另一个按钮将颜色更改回蓝色,java,swing,jpanel,jbutton,jlabel,Java,Swing,Jpanel,Jbutton,Jlabel,我试图使它在一个按钮点击(动作事件)的颜色改变标签为蓝色。我有四个不同的标签和两个按钮。如果单击向右箭头的按钮,则如果再次单击按钮,则右侧的标签将从橙色变为蓝色,依此类推。如果单击左侧的按钮,则如果再次单击,左侧的标签将从橙色变为蓝色,依此类推。我不知道我在action listeners中是否做错了什么,但是当按钮被点击时,什么都没有发生。我将在这段下面发布作业说明,希望它更有意义 在本实验室中,您将创建一个与下图类似的GUI。在左侧,您有一个控制面板,其中包括两个按钮:一个带有左箭头,另一个
package guiLayout;
/**
*在这个JFrame中,我的目标是能够制作标签的颜色
*使用操作侦听器单击JButton上的更改。
*
*@作者科迪·贝瑞
*@自2020年7月4日起
*/
导入java.awt.EventQueue;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.border.EmptyBorder;
导入javax.swing.JLabel;
导入javax.swing.SwingConstants;
导入java.awt.Color;
导入javax.swing.JButton;
导入java.awt.Font;
导入java.awt.event.ActionListener;
导入java.awt.event.ActionEvent;
公共类LabGuiLayout扩展了JFrame{
//田地
私有静态最终长serialVersionUID=1L;
私有JPanel内容窗格;
/**
*启动应用程序。
*/
公共静态void main(字符串[]args){
invokeLater(新的Runnable(){
公开募捐{
试一试{
LabGuiLayout框架=新LabGuiLayout();
frame.setVisible(true);
}捕获(例外e){
e、 printStackTrace();
}
}
});
}
/**
*创建框架。
*/
公共LabGuiLayout(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
立根(100100500200);
contentPane=newjpanel();
setboorder(新的EmptyBorder(5,5,5,5));
setContentPane(contentPane);
contentPane.setLayout(null);
//创建方法实例并将其添加到JPannel。
JButton btnNewButton=moveLeftButton();
contentPane.add(btnewbutton);
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(按钮);
JLabel label=labelFour();
contentPane.add(标签);
JLabel lblNewLabel_3=demoLayoutLabel();
contentPane.add(lblNewLabel_3);
}
/**
*标题标签。
*
*@return返回lblNewLabel_3。
*/
私有JLabel demoLayoutLabel(){
JLabel lblNewLabel_3=新JLabel(“演示布局”);
lblNewLabel_3.setFont(新字体(“Tahoma”,Font.PLAIN,23));
lblNewLabel_3.设置水平对齐(旋转常数中心);
lblNewLabel_3.设置不透明(真);
lblNewLabel_3.立根(0,0490,46);
返回lblNewLabel_3;
}
/**
*1号标签。
*
*@return返回lblNewLabel。
*/
私人JLabel labelOne(){
JLabel lblNewLabel=新JLabel(“1”);
lblNewLabel.setFont(新字体(“Tahoma”,Font.PLAIN,26));
lblNewLabel.setOpaque(true);
lblNewLabel.立根(91,45,86,93);
lblNewLabel.立根(颜色:蓝色);
lblNewLabel.SETHORISTALLING(旋转约束中心);
返回lblNewLabel;
}
/**
*2号标签。
*
*@return返回lblNewLabel_1。
*/
私人JLabel labelTwo(){
JLabel lblNewLabel_1=新JLabel(“2”);
lblNewLabel_1.setFont(新字体(“Tahoma”,Font.PLAIN,26));
lblNewLabel_1.设置不透明(真);
lblNewLabel_1.立根(187,45,86,93);
lblNewLabel_1.倒退(颜色为橙色);
lblNewLabel_1.设置水平对齐(旋转常数中心);
返回lblNewLabel_1;
}
/**
*3号标签。
*
*@return返回lblNewLabel_2。
*/
私人JLabel labelThree(){
JLabel lblNewLabel_2=新JLabel(“3”);
lblNewLabel_2.setFont(新字体(“Tahoma”,Font.PLAIN,26));
lblNewLabel_2.setOpaque(真);
lblNewLabel_2.立根(283,45,86,93);
lblNewLabel_2.倒退(颜色:橙色);
lblNewLabel_2.设置水平对齐(旋转常数中心);
返回lblNewLabel_2;
}
/**
*4号标签。
*
*@return返回标签。
*/
私人JLabel labelFour(){
JLabel标签=新的JLabel(“4”);
label.setFont(新字体(“Tahoma”,Font.PLAIN,26));
label.set不透明(true);
标签.设置水平对齐(旋转角度.中心);
标签.背景(颜色.橙色);
标签.立根(379,45,86,93);
退货标签;
}
/**
*向左移动按钮,单击该按钮时将向左更改按钮
*将“上一个”按钮设置为蓝色,将“上一个”按钮设置为橙色。一旦左侧的最后一个标签
*蓝色如果再次单击按钮,它将重新开始。
*
*@return返回btnNewButton。
*/
私有JButton moveLeftButton(){
//标签数组。
JLabel[]jl={labelOne(),labelTwo(),labelThree(),labelFour()};
JButton btnNewButton=新JButton(“”);
addActionListener(新建ActionListener()){
int=0;
已执行的公共无效操作(操作事件e){
开关(++点击){
案例1:
jl[0]。挫折背景(颜色:橙色);
jl[1]。挫折背景(颜色.蓝色);
打破
案例2:
jl[1
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);
JLabel[] jl = { labelOne(), labelTwo(), labelThree(), labelFour() };