3つのボタンがあるウィンドウを表示するプログラムを作成します。各ボタンには、「赤」、「緑」、「青」などの名前が付いています。このウィンドウには、ラベルもあります。ラベルにはアイコンが含まれています。このアイコンは、最初は空のCompositeIconでなければなりません。ボタンを押すたびに、例のようにボタンの色で四角形が表示されます。「青色のボタンを押す - >青色の四角形がウィンドウに表示されます」 これまで私はこれを持っています。私は色の名前の3つのボタンがあります。私はそれが動作しないボタンのいずれかを押すたびに私は。私は何をする必要がありますか?色で四角いボタンを作るにはどうすればいいですか?
コード:
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);
}
}
ねえ、私はその同じ運動かどうかわからないんだけど、この上の外観を持っていけないので、いくつかのIDEが自動にあなたの輸入を並べ替えることができます。http:/ /stackoverflow.com/questions/19881700/buttons-and-icons-within-a-label-squareicon。それはあなたの仕事に非常によく似ています。 – endkugelfang