-1

「緑」と「オレンジ」というラベルの付いた 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.");
}

}

4

2 に答える 2

3

とに使用BorderLayoutしました。フレームの底板です。JFrameFlowLayoutButtonPanelButtonPanel

ここに画像の説明を入力

frame = new JFrame();
frame.setLayout(new BorderLayout());

topPanel = new JPanel();
topPanel.add(new JLabel("Top Panel"));

middlepanel = new JPanel();
middlepanel.add(new JLabel("Middle Panel"));

bottomPanel = new JPanel();

bottomPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
bottomPanel.add(new JButton("Orange"));
bottomPanel.add(new JButton("Green"));

frame.add(topPanel, BorderLayout.NORTH);
frame.add(middlepanel, BorderLayout.CENTER);
frame.add(bottomPanel, BorderLayout.SOUTH);
于 2013-04-29T04:54:53.593 に答える