简体   繁体   English

如何制作按钮以使颜色方形?

[英]How can I make buttons to make square with color?

Write a program that shows a window with three button. 编写一个显示带有三个按钮的窗口的程序。 Each button has a name like “Red” , “Green” and “Blue”. 每个按钮的名称都类似“红色”,“绿色”和“蓝色”。 In this window, there is also a label. 在此窗口中,还有一个标签。 The label contains an icon. 标签包含一个图标。 This icon must be a CompositeIcon where in the beginning is empty. 此图标必须是CompositeIcon,其中开头为空。 Every time you press on a button you can see a square with the button color like for an example “press blue button -> a blue square appears on the window”. 每次按下按钮时,您都可以看到一个带有按钮颜色的正方形,例如“按下蓝色按钮->窗口上出现一个蓝色正方形”。 So far I have this. 到目前为止,我有这个。 I have the three buttons with their color name. 我有三个按钮及其颜色名称。 I every time I press one of the buttons it does not work. 我每次按其中一个按钮都无法正常工作。 What do I need to do? 我需要做什么?

Code: 码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ActionTester{

    public static void main(String[] args){
            JFrame frame = new JFrame();
            final JTextField textField = new JTextField();

            JButton RedButton = new JButton("Red");

            RedButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                            SquareIcon red = new SquareIcon(20,Color.RED);
                            CompositeIcon ci = new CompositeIcon();
                            ci.addIcon(red);
                    }
            });

            JButton GreenButton = new JButton("Green");

            GreenButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                            SquareIcon green = new SquareIcon(20,Color.GREEN);
                            CompositeIcon ci = new CompositeIcon();
                            ci.addIcon(green);
                    }
            });

            JButton BlueButton = new JButton("Blue");

            BlueButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                            SquareIcon blue = new SquareIcon(20,Color.BLUE);
                            CompositeIcon ci = new CompositeIcon();
                            ci.addIcon(blue);
                    }
            });

            frame.setLayout(new FlowLayout());
            frame.add(RedButton);
            frame.add(GreenButton);
            frame.add(BlueButton);
            frame.add(textField);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setVisible(true);
    }
}

All you need to do is to create one Square Object that you change in the ActionListener like: 您需要做的就是创建一个在ActionListener更改的Square对象,例如:

final JPanel sqr = new JPanel();

JButton RedButton = new JButton("Red");

RedButton.addActionListener(new ActionListener()
{
  @Override
  public void actionPerformed(ActionEvent e)
  {
    sqr.setBackground(Color.RED);
  }
});

And dont forget to add sqr to the frame 并且不要忘记将sqr添加到框架

On another note, please avoid using imports like 另外,请避免使用类似

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

In my project it came down to 在我的项目中,它归结为

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

Some IDEs can sort you imports automaticly so you dont need to bother with the most common imports anymore 一些IDE可以自动对您的导入进行排序,因此您不再需要担心最常见的导入

the buttons itself work but you never add the compositeicon to your frame. 按钮本身可以工作,但是您永远不会将compositeicon添加到框架中。 therefore nothing is displayed 因此什么也不显示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM