「緑」と「オレンジ」というラベルの付いた 2 つのボタンを表示するプログラムを作成します。
ユーザーが緑色のボタンをクリックすると、ウィンドウの背景が緑色に変わります。ユーザーがオレンジ色のボタンをクリックすると、ウィンドウの背景がオレンジ色に変わります。
JFrame
この GUIの を作成します。GUI は、デフォルトのレイアウト マネージャーを採用しています。アJPanel
が必要です。
パネル内に 2 つのボタンを配置し、ボーダー レイアウトの南の領域にパネルを追加します。
タイトル バーのテキストに注意してください。緑のボタンには、白いテキストと緑の背景が必要です。オレンジ色のボタンには、オレンジ色の背景に黒のテキストが必要です。
以下は私がこれまでに持っているものですが、うまくいかないようです。
public class LabAssign91 extends JFrame implements ActionListener{
private JPanel loc1Panel;
private JButton greenButton, orangeButton;
public LabAssign91()
{
super("Colored Buttons");
setLayout(new GridLayout(2, 2));
setSize(300,250);
setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(loc1Panel);
loc1Panel = new JPanel();
add(loc1Panel, BorderLayout.SOUTH);
greenButton = new JButton("Green");
greenButton.addActionListener(this);
loc1Panel.add(greenButton, BorderLayout.WEST);
greenButton.setBackground(Color.green);;
orangeButton = new JButton("Orange");
orangeButton.addActionListener(this);
loc1Panel.add(orangeButton, BorderLayout.EAST);
orangeButton.setBackground(Color.orange);
}
public static void main(String[] args) {
LabAssign91 app = new LabAssign91();
}
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
}