我怎样才能使按钮与颜色成正方形?
How can I make buttons to make square with color?
编写一个程序,显示一个带有三个按钮的 window。每个按钮都有一个名称,如“红色”、“绿色”和“蓝色”。在这个window中,还有一个标签。标签包含一个图标。此图标必须是开头为空的 CompositeIcon。每次按下按钮时,您都会看到一个带有按钮颜色的方块,例如“按下蓝色按钮 -> window 上出现一个蓝色方块”。
到目前为止我有这个。我有三个带有颜色名称的按钮。我每次按下其中一个按钮时它都不起作用。我需要做什么?
代码:
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);
}
}
按钮本身可以工作,但您永远不会将 compositeicon
添加到框架中。因此什么都不显示
您需要做的就是创建一个您在 ActionListener
中更改的方形对象,例如:
final JPanel sqr = new JPanel();
JButton RedButton = new JButton("Red");
RedButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
sqr.setBackground(Color.RED);
}
});
并且不要忘记将 sqr
添加到框架
另外请注意,请避免使用像
这样的导入
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
在我的项目中它归结为
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;
一些 IDE 可以自动对您的导入进行排序,因此您无需再为最常见的导入而烦恼
编写一个程序,显示一个带有三个按钮的 window。每个按钮都有一个名称,如“红色”、“绿色”和“蓝色”。在这个window中,还有一个标签。标签包含一个图标。此图标必须是开头为空的 CompositeIcon。每次按下按钮时,您都会看到一个带有按钮颜色的方块,例如“按下蓝色按钮 -> window 上出现一个蓝色方块”。 到目前为止我有这个。我有三个带有颜色名称的按钮。我每次按下其中一个按钮时它都不起作用。我需要做什么?
代码:
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);
}
}
按钮本身可以工作,但您永远不会将 compositeicon
添加到框架中。因此什么都不显示
您需要做的就是创建一个您在 ActionListener
中更改的方形对象,例如:
final JPanel sqr = new JPanel();
JButton RedButton = new JButton("Red");
RedButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
sqr.setBackground(Color.RED);
}
});
并且不要忘记将 sqr
添加到框架
另外请注意,请避免使用像
这样的导入import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
在我的项目中它归结为
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;
一些 IDE 可以自动对您的导入进行排序,因此您无需再为最常见的导入而烦恼